comparison lib/Test/Nginx.pm @ 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 020c4e47ecac
children 0342957cca37
comparison
equal deleted inserted replaced
437:8b4a6b8691eb 438:60888e2c3f5a
10 use strict; 10 use strict;
11 11
12 use base qw/ Exporter /; 12 use base qw/ Exporter /;
13 13
14 our @EXPORT = qw/ log_in log_out http http_get http_head /; 14 our @EXPORT = qw/ log_in log_out http http_get http_head /;
15 our @EXPORT_OK = qw/ http_gzip_request http_gzip_like /; 15 our @EXPORT_OK = qw/ http_gzip_request http_gzip_like http_start http_end /;
16 our %EXPORT_TAGS = ( 16 our %EXPORT_TAGS = (
17 gzip => [ qw/ http_gzip_request http_gzip_like / ] 17 gzip => [ qw/ http_gzip_request http_gzip_like / ]
18 ); 18 );
19 19
20 ############################################################################### 20 ###############################################################################
453 EOF 453 EOF
454 } 454 }
455 455
456 sub http($;%) { 456 sub http($;%) {
457 my ($request, %extra) = @_; 457 my ($request, %extra) = @_;
458 my $reply; 458
459 my $s = http_start($request, %extra);
460
461 return $s if $extra{start} or !defined $s;
462 return http_end($s);
463 }
464
465 sub http_start($;%) {
466 my ($request, %extra) = @_;
467 my $s;
459 468
460 eval { 469 eval {
461 local $SIG{ALRM} = sub { die "timeout\n" }; 470 local $SIG{ALRM} = sub { die "timeout\n" };
462 local $SIG{PIPE} = sub { die "sigpipe\n" }; 471 local $SIG{PIPE} = sub { die "sigpipe\n" };
463 alarm(5); 472 alarm(5);
464 473
465 my $s = $extra{socket} || IO::Socket::INET->new( 474 $s = $extra{socket} || IO::Socket::INET->new(
466 Proto => 'tcp', 475 Proto => 'tcp',
467 PeerAddr => '127.0.0.1:8080' 476 PeerAddr => '127.0.0.1:8080'
468 ) 477 )
469 or die "Can't connect to nginx: $!\n"; 478 or die "Can't connect to nginx: $!\n";
470 479
476 485
477 if ($extra{body}) { 486 if ($extra{body}) {
478 log_out($extra{body}); 487 log_out($extra{body});
479 $s->print($extra{body}); 488 $s->print($extra{body});
480 } 489 }
490
491 alarm(0);
492 };
493 alarm(0);
494 if ($@) {
495 log_in("died: $@");
496 return undef;
497 }
498
499 return $s;
500 }
501
502 sub http_end($;%) {
503 my ($s) = @_;
504 my $reply;
505
506 eval {
507 local $SIG{ALRM} = sub { die "timeout\n" };
508 local $SIG{PIPE} = sub { die "sigpipe\n" };
509 alarm(5);
481 510
482 local $/; 511 local $/;
483 $reply = $s->getline(); 512 $reply = $s->getline();
484 513
485 alarm(0); 514 alarm(0);