Sergey Kandaurov | 26b256c | 2014-09-16 14:22:27 +0400 | [diff] [blame^] | 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # (C) Sergey Kandaurov |
| 4 | # (C) Nginx, Inc. |
| 5 | |
| 6 | # Tests for http proxy module, proxy_next_upstream_tries |
| 7 | # and proxy_next_upstream_timeout directives. |
| 8 | |
| 9 | ############################################################################### |
| 10 | |
| 11 | use warnings; |
| 12 | use strict; |
| 13 | |
| 14 | use Test::More; |
| 15 | |
| 16 | BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 17 | |
| 18 | use lib 'lib'; |
| 19 | use Test::Nginx; |
| 20 | |
| 21 | ############################################################################### |
| 22 | |
| 23 | select STDERR; $| = 1; |
| 24 | select STDOUT; $| = 1; |
| 25 | |
| 26 | my $t = Test::Nginx->new()->has(qw/http proxy rewrite/); |
| 27 | |
| 28 | $t->write_file_expand('nginx.conf', <<'EOF'); |
| 29 | |
| 30 | %%TEST_GLOBALS%% |
| 31 | |
| 32 | daemon off; |
| 33 | |
| 34 | events { |
| 35 | } |
| 36 | |
| 37 | http { |
| 38 | %%TEST_GLOBALS_HTTP%% |
| 39 | |
| 40 | upstream u { |
| 41 | server 127.0.0.1:8081; |
| 42 | server 127.0.0.1:8081; |
| 43 | server 127.0.0.1:8081; |
| 44 | } |
| 45 | |
| 46 | upstream u2 { |
| 47 | server 127.0.0.1:8081; |
| 48 | server 127.0.0.1:8081 backup; |
| 49 | server 127.0.0.1:8081 backup; |
| 50 | } |
| 51 | |
| 52 | upstream u3 { |
| 53 | server 127.0.0.1:8082; |
| 54 | server 127.0.0.1:8082; |
| 55 | server 127.0.0.1:8082; |
| 56 | } |
| 57 | |
| 58 | upstream u4 { |
| 59 | server 127.0.0.1:8082; |
| 60 | server 127.0.0.1:8082 backup; |
| 61 | server 127.0.0.1:8082 backup; |
| 62 | } |
| 63 | |
| 64 | server { |
| 65 | listen 127.0.0.1:8080; |
| 66 | server_name localhost; |
| 67 | |
| 68 | proxy_next_upstream http_404; |
| 69 | proxy_intercept_errors on; |
| 70 | error_page 404 /404; |
| 71 | |
| 72 | location /tries { |
| 73 | proxy_pass http://u; |
| 74 | proxy_next_upstream_tries 2; |
| 75 | } |
| 76 | |
| 77 | location /tries/backup { |
| 78 | proxy_pass http://u2; |
| 79 | proxy_next_upstream_tries 2; |
| 80 | } |
| 81 | |
| 82 | location /timeout { |
| 83 | proxy_pass http://u3; |
| 84 | proxy_next_upstream_timeout 1500ms; |
| 85 | } |
| 86 | |
| 87 | location /timeout/backup { |
| 88 | proxy_pass http://u4; |
| 89 | proxy_next_upstream_timeout 1500ms; |
| 90 | } |
| 91 | |
| 92 | location /404 { |
| 93 | return 200 x${upstream_status}x; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | EOF |
| 99 | |
| 100 | $t->run_daemon(\&http_daemon, 8081); |
| 101 | $t->run_daemon(\&http_daemon, 8082); |
| 102 | $t->try_run('no proxy_next_upstream_tries')->plan(4); |
| 103 | |
| 104 | $t->waitforsocket('127.0.0.1:8081'); |
| 105 | $t->waitforsocket('127.0.0.1:8082'); |
| 106 | |
| 107 | ############################################################################### |
| 108 | |
| 109 | like(http_get('/tries'), qr/x404, 404x/, 'tries'); |
| 110 | like(http_get('/tries/backup'), qr/x404, 404x/, 'tries backup'); |
| 111 | |
| 112 | # two tries fit into 1.5s |
| 113 | |
| 114 | like(http_get('/timeout'), qr/x404, 404x/, 'timeout'); |
| 115 | like(http_get('/timeout/backup'), qr/x404, 404x/, 'timeout backup'); |
| 116 | |
| 117 | ############################################################################### |
| 118 | |
| 119 | sub http_daemon { |
| 120 | my ($port) = @_; |
| 121 | |
| 122 | my $server = IO::Socket::INET->new( |
| 123 | Proto => 'tcp', |
| 124 | LocalHost => '127.0.0.1', |
| 125 | LocalPort => $port, |
| 126 | Listen => 5, |
| 127 | Reuse => 1 |
| 128 | ) |
| 129 | or die "Can't create listening socket: $!\n"; |
| 130 | |
| 131 | local $SIG{PIPE} = 'IGNORE'; |
| 132 | |
| 133 | while (my $client = $server->accept()) { |
| 134 | $client->autoflush(1); |
| 135 | |
| 136 | my $headers = ''; |
| 137 | |
| 138 | while (<$client>) { |
| 139 | $headers .= $_; |
| 140 | last if (/^\x0d?\x0a?$/); |
| 141 | } |
| 142 | |
| 143 | next if $headers eq ''; |
| 144 | |
| 145 | if ($port == 8082) { |
| 146 | Test::Nginx::log_core('||', "$port: sleep(1)"); |
| 147 | select undef, undef, undef, 1; |
| 148 | } |
| 149 | |
| 150 | Test::Nginx::log_core('||', "$port: response, 404"); |
| 151 | print $client <<EOF; |
| 152 | HTTP/1.1 404 Not Found |
| 153 | Connection: close |
| 154 | |
| 155 | EOF |
| 156 | |
| 157 | } continue { |
| 158 | close $client; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | ############################################################################### |