diff 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
line wrap: on
line diff
--- a/lib/Test/Nginx.pm
+++ b/lib/Test/Nginx.pm
@@ -12,7 +12,7 @@ use strict;
 use base qw/ Exporter /;
 
 our @EXPORT = qw/ log_in log_out http http_get http_head /;
-our @EXPORT_OK = qw/ http_gzip_request http_gzip_like /;
+our @EXPORT_OK = qw/ http_gzip_request http_gzip_like http_start http_end /;
 our %EXPORT_TAGS = (
 	gzip => [ qw/ http_gzip_request http_gzip_like / ]
 );
@@ -455,14 +455,23 @@ EOF
 
 sub http($;%) {
 	my ($request, %extra) = @_;
-	my $reply;
+
+	my $s = http_start($request, %extra);
+
+	return $s if $extra{start} or !defined $s;
+	return http_end($s);
+}
+
+sub http_start($;%) {
+	my ($request, %extra) = @_;
+	my $s;
 
 	eval {
 		local $SIG{ALRM} = sub { die "timeout\n" };
 		local $SIG{PIPE} = sub { die "sigpipe\n" };
 		alarm(5);
 
-		my $s = $extra{socket} || IO::Socket::INET->new(
+		$s = $extra{socket} || IO::Socket::INET->new(
 			Proto => 'tcp',
 			PeerAddr => '127.0.0.1:8080'
 		)
@@ -479,6 +488,26 @@ sub http($;%) {
 			$s->print($extra{body});
 		}
 
+		alarm(0);
+	};
+	alarm(0);
+	if ($@) {
+		log_in("died: $@");
+		return undef;
+	}
+
+	return $s;
+}
+
+sub http_end($;%) {
+	my ($s) = @_;
+	my $reply;
+
+	eval {
+		local $SIG{ALRM} = sub { die "timeout\n" };
+		local $SIG{PIPE} = sub { die "sigpipe\n" };
+		alarm(5);
+
 		local $/;
 		$reply = $s->getline();