Mercurial > hg > nginx-tests
comparison proxy_variables.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 | a1a3164faec8 |
children | 144c6ce732e4 |
comparison
equal
deleted
inserted
replaced
1418:f3422a4fe349 | 1419:16a8962ff246 |
---|---|
20 ############################################################################### | 20 ############################################################################### |
21 | 21 |
22 select STDERR; $| = 1; | 22 select STDERR; $| = 1; |
23 select STDOUT; $| = 1; | 23 select STDOUT; $| = 1; |
24 | 24 |
25 my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(24) | 25 my $t = Test::Nginx->new()->has(qw/http proxy/) |
26 ->write_file_expand('nginx.conf', <<'EOF'); | 26 ->write_file_expand('nginx.conf', <<'EOF'); |
27 | 27 |
28 %%TEST_GLOBALS%% | 28 %%TEST_GLOBALS%% |
29 | 29 |
30 daemon off; | 30 daemon off; |
33 } | 33 } |
34 | 34 |
35 http { | 35 http { |
36 %%TEST_GLOBALS_HTTP%% | 36 %%TEST_GLOBALS_HTTP%% |
37 | 37 |
38 upstream u { | 38 log_format u $uri:$upstream_response_length:$upstream_bytes_received: |
39 server 127.0.0.1:8081; | 39 $upstream_bytes_sent:$upstream_http_x_len; |
40 server 127.0.0.1:8081; | |
41 } | |
42 | |
43 log_format time '$upstream_connect_time:$upstream_header_time:' | |
44 '$upstream_response_time'; | |
45 | 40 |
46 server { | 41 server { |
47 listen 127.0.0.1:8080; | 42 listen 127.0.0.1:8080; |
48 server_name localhost; | 43 server_name localhost; |
49 | 44 |
50 add_header X-Connect $upstream_connect_time; | |
51 add_header X-Header $upstream_header_time; | |
52 add_header X-Response $upstream_response_time; | |
53 | |
54 location / { | 45 location / { |
55 proxy_pass http://127.0.0.1:8081; | 46 proxy_pass http://127.0.0.1:8081; |
56 access_log %%TESTDIR%%/time.log time; | 47 access_log %%TESTDIR%%/test.log u; |
57 } | 48 } |
58 | |
59 location /pnu { | |
60 proxy_pass http://u/bad; | |
61 } | |
62 | |
63 location /vars { | |
64 proxy_pass http://127.0.0.1:8080/stub; | |
65 | |
66 add_header X-Proxy-Host $proxy_host; | |
67 add_header X-Proxy-Port $proxy_port; | |
68 add_header X-Proxy-Forwarded $proxy_add_x_forwarded_for; | |
69 } | |
70 | |
71 location /stub { } | |
72 } | 49 } |
73 } | 50 } |
74 | 51 |
75 EOF | 52 EOF |
76 | 53 |
77 $t->write_file('stub', ''); | |
78 $t->run_daemon(\&http_daemon, port(8081)); | 54 $t->run_daemon(\&http_daemon, port(8081)); |
79 $t->run(); | 55 $t->try_run('upstream_bytes_sent')->plan(4); |
80 | 56 |
81 $t->waitforsocket('127.0.0.1:' . port(8081)); | 57 $t->waitforsocket('127.0.0.1:' . port(8081)); |
82 | 58 |
83 ############################################################################### | 59 ############################################################################### |
84 | 60 |
85 my $re = qr/(\d\.\d{3})/; | 61 my $r; |
86 my $p0 = port(8080); | |
87 my ($ct, $ht, $rt, $ct2, $ht2, $rt2, $ct3, $ht3, $rt3); | |
88 | 62 |
89 like(http_get('/vars'), qr/X-Proxy-Host:\s127\.0\.0\.1:$p0/, 'proxy_host'); | 63 my ($l1) = ($r = http_get('/')) =~ /X-Len: (\d+)/; |
90 like(http_get('/vars'), qr/X-Proxy-Port:\s$p0/, 'proxy_port'); | 64 like($r, qr/SEE-THIS/, 'proxy request'); |
91 like(http_xff('/vars', '192.0.2.1'), qr/X-Proxy-Forwarded:.*192\.0\.2\.1/, | |
92 'proxy_add_x_forwarded_for'); | |
93 | 65 |
94 ($ct, $ht) = get('/header'); | 66 my ($l2) = ($r = http_get('/multi')) =~ /X-Len: (\d+)/; |
95 cmp_ok($ct, '<', 1, 'connect time - slow response header'); | 67 like($r, qr/AND-THIS/, 'proxy request with multiple packets'); |
96 cmp_ok($ht, '>=', 1, 'header time - slow response header'); | |
97 | |
98 ($ct, $ht) = get('/body'); | |
99 cmp_ok($ct, '<', 1, 'connect time - slow response body'); | |
100 cmp_ok($ht, '<', 1, 'header time - slow response body'); | |
101 | |
102 my $s = http_get('/header_close', start => 1); | |
103 select undef, undef, undef, 0.4; | |
104 close ($s); | |
105 | |
106 # expect no header time in 1st (bad) upstream, no (yet) response time in 2nd | |
107 | |
108 $re = qr/(\d\.\d{3}|-)/; | |
109 ($ct, $ct2, $ht, $ht2, $rt, $rt2) = get('/pnu', many => 1); | |
110 cmp_ok($ct, '<', 1, 'connect time - next'); | |
111 cmp_ok($ct2, '<', 1, 'connect time - next 2'); | |
112 | |
113 TODO: { | |
114 local $TODO = 'not yet' unless $t->has_version('1.15.7'); | |
115 | |
116 is($ht, '-', 'header time - next'); | |
117 | |
118 } | |
119 | |
120 cmp_ok($ht2, '<', 1, 'header time - next 2'); | |
121 cmp_ok($rt, '>=', 1, 'response time - next'); | |
122 | |
123 TODO: { | |
124 local $TODO = 'not yet' unless $t->has_version('1.15.7'); | |
125 | |
126 is($rt2, '-', 'response time - next 2'); | |
127 | |
128 } | |
129 | 68 |
130 $t->stop(); | 69 $t->stop(); |
131 | 70 |
132 ($ct, $ht, $rt, $ct2, $ht2, $rt2, $ct3, $ht3, $rt3) | 71 my $f = $t->read_file('test.log'); |
133 = $t->read_file('time.log') =~ /^$re:$re:$re\n$re:$re:$re\n$re:$re:$re$/; | 72 Test::Nginx::log_core('||', $f); |
134 | 73 |
135 cmp_ok($ct, '<', 1, 'connect time log - slow response header'); | 74 like($f, qr!^/:23:68:$l1:$l1!m, 'log - response length'); |
136 cmp_ok($ct2, '<', 1, 'connect time log - slow response body'); | 75 like($f, qr!^/multi:32:77:$l2:$l2!m, 'log - response length - multi packets'); |
137 | |
138 TODO: { | |
139 local $TODO = 'not yet' unless $t->has_version('1.15.7'); | |
140 | |
141 isnt($ct3, '-', 'connect time log - client close set'); | |
142 | |
143 } | |
144 | |
145 $ct3 = 0 if $ct3 eq '-'; | |
146 cmp_ok($ct3, '<', 1, 'connect time log - client close'); | |
147 | |
148 cmp_ok($ht, '>=', 1, 'header time log - slow response header'); | |
149 cmp_ok($ht2, '<', 1, 'header time log - slow response body'); | |
150 is($ht3, '-', 'header time log - client close'); | |
151 | |
152 cmp_ok($rt, '>=', 1, 'response time log - slow response header'); | |
153 cmp_ok($rt2, '>=', 1, 'response time log - slow response body'); | |
154 | |
155 TODO: { | |
156 local $TODO = 'not yet' unless $t->has_version('1.15.7'); | |
157 | |
158 isnt($rt3, '-', 'response time log - client close set'); | |
159 $rt3 = 0 if $rt3 eq '-'; | |
160 cmp_ok($rt3, '>', $ct3, 'response time log - client close'); | |
161 | |
162 } | |
163 | 76 |
164 ############################################################################### | 77 ############################################################################### |
165 | 78 |
166 sub get { | |
167 my ($uri, %extra) = @_; | |
168 my $re = $extra{many} ? qr/$re, $re?/ : $re; | |
169 my $r = http_get($uri); | |
170 $r =~ /X-Connect: $re/, $r =~ /X-Header: $re/, $r =~ /X-Response: $re/; | |
171 } | |
172 | |
173 sub http_xff { | |
174 my ($uri, $xff) = @_; | |
175 return http(<<EOF); | |
176 GET $uri HTTP/1.0 | |
177 Host: localhost | |
178 X-Forwarded-For: $xff | |
179 | |
180 EOF | |
181 } | |
182 | |
183 sub http_daemon { | 79 sub http_daemon { |
184 my ($port) = @_; | 80 my ($port) = @_; |
185 my $once = 1; | |
186 | |
187 my $server = IO::Socket::INET->new( | 81 my $server = IO::Socket::INET->new( |
188 Proto => 'tcp', | 82 Proto => 'tcp', |
189 LocalHost => '127.0.0.1', | 83 LocalHost => '127.0.0.1', |
190 LocalPort => $port, | 84 LocalPort => $port, |
191 Listen => 5, | 85 Listen => 5, |
205 $headers .= $_; | 99 $headers .= $_; |
206 last if (/^\x0d?\x0a?$/); | 100 last if (/^\x0d?\x0a?$/); |
207 } | 101 } |
208 | 102 |
209 $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i; | 103 $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i; |
210 next unless defined $uri; | 104 my $len = length($headers); |
211 | 105 |
212 if ($uri =~ 'bad' && $once) { | 106 if ($uri eq '/') { |
213 $once = 0; | 107 print $client <<"EOF"; |
214 select undef, undef, undef, 1.1; | 108 HTTP/1.1 200 OK |
215 next; | 109 Connection: close |
110 X-Len: $len | |
111 | |
112 EOF | |
113 print $client "TEST-OK-IF-YOU-SEE-THIS" | |
114 unless $headers =~ /^HEAD/i; | |
115 | |
116 } elsif ($uri eq '/multi') { | |
117 | |
118 print $client <<"EOF"; | |
119 HTTP/1.1 200 OK | |
120 Connection: close | |
121 X-Len: $len | |
122 | |
123 TEST-OK-IF-YOU-SEE-THIS | |
124 EOF | |
125 | |
126 select undef, undef, undef, 0.1; | |
127 print $client 'AND-THIS'; | |
216 } | 128 } |
217 | 129 |
218 if ($uri =~ 'header') { | 130 close $client; |
219 select undef, undef, undef, 1.1; | |
220 } | |
221 | |
222 print $client <<EOF; | |
223 HTTP/1.1 200 OK | |
224 Connection: close | |
225 | |
226 SEE-THIS- | |
227 EOF | |
228 | |
229 if ($uri =~ 'body') { | |
230 select undef, undef, undef, 1.1; | |
231 } | |
232 | |
233 print $client 'AND-THIS'; | |
234 } | 131 } |
235 } | 132 } |
236 | 133 |
237 ############################################################################### | 134 ############################################################################### |