blob: 32fc544c3b6d6038c3d1e13c74c23a0cd1fc17b4 [file] [log] [blame]
Sergey Kandaurov26b256c2014-09-16 14:22:27 +04001#!/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
11use warnings;
12use strict;
13
14use Test::More;
15
16BEGIN { use FindBin; chdir($FindBin::Bin); }
17
18use lib 'lib';
19use Test::Nginx;
20
21###############################################################################
22
23select STDERR; $| = 1;
24select STDOUT; $| = 1;
25
26my $t = Test::Nginx->new()->has(qw/http proxy rewrite/);
27
28$t->write_file_expand('nginx.conf', <<'EOF');
29
30%%TEST_GLOBALS%%
31
32daemon off;
33
34events {
35}
36
37http {
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
98EOF
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
109like(http_get('/tries'), qr/x404, 404x/, 'tries');
110like(http_get('/tries/backup'), qr/x404, 404x/, 'tries backup');
111
112# two tries fit into 1.5s
113
114like(http_get('/timeout'), qr/x404, 404x/, 'timeout');
115like(http_get('/timeout/backup'), qr/x404, 404x/, 'timeout backup');
116
117###############################################################################
118
119sub 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;
152HTTP/1.1 404 Not Found
153Connection: close
154
155EOF
156
157 } continue {
158 close $client;
159 }
160}
161
162###############################################################################