Mercurial > hg > nginx-tests
comparison http_resolver.t @ 438:60888e2c3f5a
Tests: new http_start() and http_end() functions.
When used together, they allow to break an http request into two separate
send/receive phases and are used to run long requests asynchronously.
An http() "start" extra flag introduced as a convenience shortcut.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Fri, 18 Jul 2014 13:19:55 +0400 |
parents | 4984dac109db |
children | 0bc79a098213 |
comparison
equal
deleted
inserted
replaced
437:8b4a6b8691eb | 438:60888e2c3f5a |
---|---|
13 use Test::More; | 13 use Test::More; |
14 | 14 |
15 BEGIN { use FindBin; chdir($FindBin::Bin); } | 15 BEGIN { use FindBin; chdir($FindBin::Bin); } |
16 | 16 |
17 use lib 'lib'; | 17 use lib 'lib'; |
18 use Test::Nginx; | 18 use Test::Nginx qw/ :DEFAULT http_end /; |
19 | 19 |
20 ############################################################################### | 20 ############################################################################### |
21 | 21 |
22 select STDERR; $| = 1; | 22 select STDERR; $| = 1; |
23 select STDOUT; $| = 1; | 23 select STDOUT; $| = 1; |
92 | 92 |
93 ############################################################################### | 93 ############################################################################### |
94 | 94 |
95 # schedule resend test, which takes abound 5 seconds to complete | 95 # schedule resend test, which takes abound 5 seconds to complete |
96 | 96 |
97 my $s = http_start('id.example.net', '/resend'); | 97 my $s = http_host_header('id.example.net', '/resend', start => 1); |
98 | 98 |
99 like(http_host_header('a.example.net', '/'), qr/200 OK/, 'A'); | 99 like(http_host_header('a.example.net', '/'), qr/200 OK/, 'A'); |
100 | 100 |
101 # ensure that resolver serves queries from cache in a case-insensitive manner | 101 # ensure that resolver serves queries from cache in a case-insensitive manner |
102 # we check this by marking 2nd and subsequent queries on backend with SERVFAIL | 102 # we check this by marking 2nd and subsequent queries on backend with SERVFAIL |
229 } | 229 } |
230 | 230 |
231 ############################################################################### | 231 ############################################################################### |
232 | 232 |
233 sub http_host_header { | 233 sub http_host_header { |
234 my ($host, $uri) = @_; | 234 my ($host, $uri, %extra) = @_; |
235 return http(<<EOF); | 235 return http(<<EOF, %extra); |
236 GET $uri HTTP/1.0 | 236 GET $uri HTTP/1.0 |
237 Host: $host | 237 Host: $host |
238 | 238 |
239 EOF | 239 EOF |
240 } | 240 } |
244 return http(<<EOF); | 244 return http(<<EOF); |
245 GET $uri HTTP/1.0 | 245 GET $uri HTTP/1.0 |
246 X-Name: $host | 246 X-Name: $host |
247 | 247 |
248 EOF | 248 EOF |
249 } | |
250 | |
251 sub http_start { | |
252 my ($host, $uri) = @_; | |
253 | |
254 my $s; | |
255 my $request = <<EOF; | |
256 GET $uri HTTP/1.0 | |
257 Host: $host | |
258 | |
259 EOF | |
260 | |
261 eval { | |
262 local $SIG{ALRM} = sub { die "timeout\n" }; | |
263 local $SIG{PIPE} = sub { die "sigpipe\n" }; | |
264 alarm(5); | |
265 $s = IO::Socket::INET->new( | |
266 Proto => 'tcp', | |
267 PeerAddr => '127.0.0.1:8080' | |
268 ); | |
269 log_out($request); | |
270 $s->print($request); | |
271 alarm(0); | |
272 }; | |
273 alarm(0); | |
274 if ($@) { | |
275 log_in("died: $@"); | |
276 return undef; | |
277 } | |
278 return $s; | |
279 } | |
280 | |
281 sub http_end { | |
282 my ($s) = @_; | |
283 my $reply; | |
284 | |
285 eval { | |
286 local $SIG{ALRM} = sub { die "timeout\n" }; | |
287 local $SIG{PIPE} = sub { die "sigpipe\n" }; | |
288 alarm(3); | |
289 local $/; | |
290 $reply = $s->getline(); | |
291 log_in($reply); | |
292 alarm(0); | |
293 }; | |
294 alarm(0); | |
295 if ($@) { | |
296 log_in("died: $@"); | |
297 return undef; | |
298 } | |
299 return $reply; | |
300 } | 249 } |
301 | 250 |
302 ############################################################################### | 251 ############################################################################### |
303 | 252 |
304 sub reply_handler { | 253 sub reply_handler { |