comparison proxy.t @ 1419:16a8962ff246

Tests: upstream_response_length_variable tests, proxy.t merged. This includes basic tests for $upstream_response_length, $upstream_bytes_received, $upstream_bytes_sent variables all residing in ngx_http_upstream_response_length_variable(). Tests added to proxy_variables.t, they're skipped on stable. Existing tests in proxy_variables.t are merged into proxy.t.
author Sergey Kandaurov <pluknet@nginx.com>
date Tue, 18 Dec 2018 17:54:39 +0300
parents 766bcbb632ee
children fc8341cf92a3
comparison
equal deleted inserted replaced
1418:f3422a4fe349 1419:16a8962ff246
21 ############################################################################### 21 ###############################################################################
22 22
23 select STDERR; $| = 1; 23 select STDERR; $| = 1;
24 select STDOUT; $| = 1; 24 select STDOUT; $| = 1;
25 25
26 my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(7); 26 my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(31);
27 27
28 $t->write_file_expand('nginx.conf', <<'EOF'); 28 $t->write_file_expand('nginx.conf', <<'EOF');
29 29
30 %%TEST_GLOBALS%% 30 %%TEST_GLOBALS%%
31 31
34 events { 34 events {
35 } 35 }
36 36
37 http { 37 http {
38 %%TEST_GLOBALS_HTTP%% 38 %%TEST_GLOBALS_HTTP%%
39
40 log_format time '$upstream_connect_time:$upstream_header_time:'
41 '$upstream_response_time';
39 42
40 upstream u { 43 upstream u {
41 server 127.0.0.1:8081; 44 server 127.0.0.1:8081;
42 } 45 }
43 46
47 upstream u2 {
48 server 127.0.0.1:8081;
49 server 127.0.0.1:8081;
50 }
51
44 server { 52 server {
45 listen 127.0.0.1:8080; 53 listen 127.0.0.1:8080;
46 server_name localhost; 54 server_name localhost;
55
56 add_header X-Connect $upstream_connect_time;
57 add_header X-Header $upstream_header_time;
58 add_header X-Response $upstream_response_time;
47 59
48 location / { 60 location / {
49 proxy_pass http://127.0.0.1:8081; 61 proxy_pass http://127.0.0.1:8081;
50 proxy_read_timeout 1s; 62 proxy_read_timeout 1s;
51 proxy_connect_timeout 2s; 63 proxy_connect_timeout 2s;
59 71
60 location /timeout { 72 location /timeout {
61 proxy_pass http://127.0.0.1:8081; 73 proxy_pass http://127.0.0.1:8081;
62 proxy_connect_timeout 2s; 74 proxy_connect_timeout 2s;
63 } 75 }
76
77 location /time/ {
78 proxy_pass http://127.0.0.1:8081/;
79 access_log %%TESTDIR%%/time.log time;
80 }
81
82 location /pnu {
83 proxy_pass http://u2/bad;
84 }
85
86 location /vars {
87 proxy_pass http://127.0.0.1:8080/stub;
88
89 add_header X-Proxy-Host $proxy_host;
90 add_header X-Proxy-Port $proxy_port;
91 add_header X-Proxy-Forwarded $proxy_add_x_forwarded_for;
92 }
93
94 location /stub { }
64 } 95 }
65 } 96 }
66 97
67 EOF 98 EOF
68 99
100 $t->write_file('stub', '');
69 $t->run_daemon(\&http_daemon); 101 $t->run_daemon(\&http_daemon);
70 $t->run()->waitforsocket('127.0.0.1:' . port(8081)); 102 $t->run()->waitforsocket('127.0.0.1:' . port(8081));
71 103
72 ############################################################################### 104 ###############################################################################
73 105
81 like(http_get('/var?b=u/'), qr/SEE-THIS/, 'proxy with variables to upstream'); 113 like(http_get('/var?b=u/'), qr/SEE-THIS/, 'proxy with variables to upstream');
82 ok(http_get("/var?b=[::]"), 'proxy with variables - no ipv6 port'); 114 ok(http_get("/var?b=[::]"), 'proxy with variables - no ipv6 port');
83 115
84 like(http_get('/timeout'), qr/200 OK/, 'proxy connect timeout'); 116 like(http_get('/timeout'), qr/200 OK/, 'proxy connect timeout');
85 117
86 ############################################################################### 118 my $re = qr/(\d\.\d{3})/;
119 my $p0 = port(8080);
120 my ($ct, $ht, $rt, $ct2, $ht2, $rt2, $ct3, $ht3, $rt3);
121
122 like(http_get('/vars'), qr/X-Proxy-Host:\s127\.0\.0\.1:$p0/, 'proxy_host');
123 like(http_get('/vars'), qr/X-Proxy-Port:\s$p0/, 'proxy_port');
124 like(http_xff('/vars', '192.0.2.1'), qr/X-Proxy-Forwarded:.*192\.0\.2\.1/,
125 'proxy_add_x_forwarded_for');
126
127 ($ct, $ht) = get('/time/header');
128 cmp_ok($ct, '<', 1, 'connect time - slow response header');
129 cmp_ok($ht, '>=', 1, 'header time - slow response header');
130
131 ($ct, $ht) = get('/time/body');
132 cmp_ok($ct, '<', 1, 'connect time - slow response body');
133 cmp_ok($ht, '<', 1, 'header time - slow response body');
134
135 my $s = http_get('/time/header', start => 1);
136 select undef, undef, undef, 0.4;
137 close ($s);
138
139 # expect no header time in 1st (bad) upstream, no (yet) response time in 2nd
140
141 $re = qr/(\d\.\d{3}|-)/;
142 ($ct, $ct2, $ht, $ht2, $rt, $rt2) = get('/pnu', many => 1);
143 cmp_ok($ct, '<', 1, 'connect time - next');
144 cmp_ok($ct2, '<', 1, 'connect time - next 2');
145
146 TODO: {
147 local $TODO = 'not yet' unless $t->has_version('1.15.7');
148
149 is($ht, '-', 'header time - next');
150
151 }
152
153 cmp_ok($ht2, '<', 1, 'header time - next 2');
154 cmp_ok($rt, '>=', 1, 'response time - next');
155
156 TODO: {
157 local $TODO = 'not yet' unless $t->has_version('1.15.7');
158
159 is($rt2, '-', 'response time - next 2');
160
161 }
162
163 $t->stop();
164
165 ($ct, $ht, $rt, $ct2, $ht2, $rt2, $ct3, $ht3, $rt3)
166 = $t->read_file('time.log') =~ /^$re:$re:$re\n$re:$re:$re\n$re:$re:$re$/;
167
168 cmp_ok($ct, '<', 1, 'connect time log - slow response header');
169 cmp_ok($ct2, '<', 1, 'connect time log - slow response body');
170
171 TODO: {
172 local $TODO = 'not yet' unless $t->has_version('1.15.7');
173
174 isnt($ct3, '-', 'connect time log - client close set');
175
176 }
177
178 $ct3 = 0 if $ct3 eq '-';
179 cmp_ok($ct3, '<', 1, 'connect time log - client close');
180
181 cmp_ok($ht, '>=', 1, 'header time log - slow response header');
182 cmp_ok($ht2, '<', 1, 'header time log - slow response body');
183 is($ht3, '-', 'header time log - client close');
184
185 cmp_ok($rt, '>=', 1, 'response time log - slow response header');
186 cmp_ok($rt2, '>=', 1, 'response time log - slow response body');
187
188 TODO: {
189 local $TODO = 'not yet' unless $t->has_version('1.15.7');
190
191 isnt($rt3, '-', 'response time log - client close set');
192 $rt3 = 0 if $rt3 eq '-';
193 cmp_ok($rt3, '>', $ct3, 'response time log - client close');
194
195 }
196
197 ###############################################################################
198
199 sub get {
200 my ($uri, %extra) = @_;
201 my $re = $extra{many} ? qr/$re, $re?/ : $re;
202 my $r = http_get($uri);
203 $r =~ /X-Connect: $re/, $r =~ /X-Header: $re/, $r =~ /X-Response: $re/;
204 }
205
206 sub http_xff {
207 my ($uri, $xff) = @_;
208 return http(<<EOF);
209 GET $uri HTTP/1.0
210 Host: localhost
211 X-Forwarded-For: $xff
212
213 EOF
214 }
87 215
88 sub http_daemon { 216 sub http_daemon {
217 my $once = 1;
89 my $server = IO::Socket::INET->new( 218 my $server = IO::Socket::INET->new(
90 Proto => 'tcp', 219 Proto => 'tcp',
91 LocalHost => '127.0.0.1:' . port(8081), 220 LocalHost => '127.0.0.1:' . port(8081),
92 Listen => 5, 221 Listen => 5,
93 Reuse => 1 222 Reuse => 1
137 HTTP/1.1 200 OK 266 HTTP/1.1 200 OK
138 Connection: close 267 Connection: close
139 268
140 EOF 269 EOF
141 270
271 } elsif ($uri eq '/bad') {
272
273 if ($once) {
274 $once = 0;
275 select undef, undef, undef, 1.1;
276 next;
277 }
278
279 print $client <<EOF;
280 HTTP/1.1 200 OK
281 Connection: close
282
283 SEE-THIS-AND-THIS
284 EOF
285
286 } elsif ($uri eq '/header') {
287 select undef, undef, undef, 1.1;
288
289 print $client <<EOF;
290 HTTP/1.1 200 OK
291 Connection: close
292
293 SEE-THIS-AND-THIS;
294 EOF
295
296 } elsif ($uri eq '/body') {
297
298 print $client <<EOF;
299 HTTP/1.1 200 OK
300 Connection: close
301
302 SEE-THIS-
303 EOF
304
305 select undef, undef, undef, 1.1;
306 print $client 'AND-THIS';
307
142 } else { 308 } else {
143 309
144 print $client <<"EOF"; 310 print $client <<"EOF";
145 HTTP/1.1 404 Not Found 311 HTTP/1.1 404 Not Found
146 Connection: close 312 Connection: close