[Catalyst-commits] r7458 - trunk/Catalyst-Engine-HTTP-Prefork/t
andyg at dev.catalyst.perl.org
andyg at dev.catalyst.perl.org
Tue Feb 26 05:24:50 GMT 2008
Author: andyg
Date: 2008-02-26 05:24:49 +0000 (Tue, 26 Feb 2008)
New Revision: 7458
Added:
trunk/Catalyst-Engine-HTTP-Prefork/t/live_http11_request_chunked.t
trunk/Catalyst-Engine-HTTP-Prefork/t/live_http11_request_pipelined.t
trunk/Catalyst-Engine-HTTP-Prefork/t/live_http11_response_chunked.t
trunk/Catalyst-Engine-HTTP-Prefork/t/live_http11_response_keepalive.t
Log:
HTTP::Prefork, added some HTTP/1.1 tests
Added: trunk/Catalyst-Engine-HTTP-Prefork/t/live_http11_request_chunked.t
===================================================================
--- trunk/Catalyst-Engine-HTTP-Prefork/t/live_http11_request_chunked.t (rev 0)
+++ trunk/Catalyst-Engine-HTTP-Prefork/t/live_http11_request_chunked.t 2008-02-26 05:24:49 UTC (rev 7458)
@@ -0,0 +1,47 @@
+#!perl
+
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use Test::More tests => 5;
+use Catalyst::Test 'TestApp';
+
+use Catalyst::Request;
+use Data::Dump qw(dump);
+use HTTP::Request;
+
+{
+ my $creq;
+ my $expected = {
+ one => 'foo',
+ two => 'bar',
+ };
+
+ my $params = 'one=foo&two=bar';
+ my $interval = int( length($params) / 4 );
+ my $offset = 0;
+
+ my $request = HTTP::Request->new( POST => 'http://localhost/dump/request' );
+ $request->content_type( 'application/x-www-form-urlencoded' );
+
+ # This returns a bit of $params each time, LWP will make it chunked
+ # The content will become this:
+ # 3\r\none\r\n3\r\n=fo\r\n3\r\no&t\r\n3\r\nwo=\r\n3\r\nbar\r\n0\r\n\r\n
+ $request->content( sub {
+ return if $offset >= length($params);
+ my $chunk = substr $params, $offset, $interval;
+ $offset += $interval;
+ return $chunk;
+ } );
+
+ ok( my $response = request($request), 'Chunked request' );
+
+ ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
+ is( $creq->method, 'POST', 'Request method is POST' );
+ is( $creq->header('Transfer-Encoding'), 'chunked', 'Transfer-Encoding header is chunked' );
+ is_deeply( $creq->{parameters}, $expected, 'Parameters ok' );
+}
+
Property changes on: trunk/Catalyst-Engine-HTTP-Prefork/t/live_http11_request_chunked.t
___________________________________________________________________
Name: svn:keywords
+ Id
Added: trunk/Catalyst-Engine-HTTP-Prefork/t/live_http11_request_pipelined.t
===================================================================
--- trunk/Catalyst-Engine-HTTP-Prefork/t/live_http11_request_pipelined.t (rev 0)
+++ trunk/Catalyst-Engine-HTTP-Prefork/t/live_http11_request_pipelined.t 2008-02-26 05:24:49 UTC (rev 7458)
@@ -0,0 +1,95 @@
+#!perl
+
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use Test::More tests => 10;
+use Catalyst::Test 'TestApp';
+
+use Catalyst::Request;
+use IO::Select;
+use IO::Socket qw(:crlf);
+use IO::Socket::INET;
+use HTTP::Response;
+use URI;
+
+{
+ my $server = URI->new( $ENV{CATALYST_SERVER} || 'http://localhost' );
+ my $base = $server->host . ':' . $server->port;
+
+ my @reqs = (
+ "http://$base/dump/request?req=1",
+ "http://$base/dump/request?req=2",
+ "http://$base/dump/request?req=3",
+ "http://$base/dump/request?req=4",
+ );
+
+ # Make first request normally, we then reuse the keep-alive connection
+ # to pipeline the next 3 requests
+ my $sock = IO::Socket::INET->new(
+ PeerAddr => $server->host,
+ PeerPort => $server->port,
+ Proto => 'tcp',
+ ReuseAddr => 1,
+ Timeout => 2,
+ ) or die "Cannot connect to $server";
+
+ # Send request
+ syswrite $sock, construct_request( shift @reqs );
+
+ # Read/parse response
+ sysread $sock, my $buf, 64 * 1024;
+ my $response = HTTP::Response->parse($buf);
+
+ is( $response->code, 200, 'Response ok' );
+ is( $response->header('Connection'), 'keep-alive', 'Keep-alive header ok' );
+
+ my $creq;
+ ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
+ is( $creq->{parameters}->{req}, 1, 'request 1 ok' );
+
+ # Send next 3 requests pipelined
+ syswrite $sock, construct_request( @reqs );
+
+ # Read all responses into one big buffer
+ my $pipebuf;
+ my $sel = IO::Select->new($sock);
+ while ( $sel->can_read(1) ) {
+ my $n = sysread $sock, my $buf2, 64 * 1024;
+ last unless $n;
+ $pipebuf .= $buf2;
+ }
+
+ my $count = 2;
+ for my $resp ( split m{HTTP/1.1 200 OK}, $pipebuf ) {
+ next unless $resp;
+
+ $resp = 'HTTP/1.1 200 OK' . $resp;
+
+ my $response = HTTP::Response->parse($resp);
+
+ my $creq;
+ ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
+ is( $creq->{parameters}->{req}, $count, "request $count ok" );
+
+ $count++;
+ }
+}
+
+sub construct_request {
+ my @urls = @_;
+
+ my $req;
+
+ for my $url ( @urls ) {
+ my $uri = URI->new($url);
+ $req
+ .= 'GET ' . $uri->path_query . ' HTTP/1.1' . $CRLF
+ . 'Host: ' . $uri->host . ':' . $uri->port . $CRLF . $CRLF;
+ }
+
+ return $req;
+}
\ No newline at end of file
Property changes on: trunk/Catalyst-Engine-HTTP-Prefork/t/live_http11_request_pipelined.t
___________________________________________________________________
Name: svn:keywords
+ Id
Added: trunk/Catalyst-Engine-HTTP-Prefork/t/live_http11_response_chunked.t
===================================================================
--- trunk/Catalyst-Engine-HTTP-Prefork/t/live_http11_response_chunked.t (rev 0)
+++ trunk/Catalyst-Engine-HTTP-Prefork/t/live_http11_response_chunked.t 2008-02-26 05:24:49 UTC (rev 7458)
@@ -0,0 +1,67 @@
+#!perl
+
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use Test::More tests => 3;
+use Catalyst::Test 'TestApp';
+
+use Catalyst::Request;
+use Data::Dump qw(dump);
+use IO::Select;
+use IO::Socket qw(:crlf);
+use IO::Socket::INET;
+use HTTP::Response;
+use URI;
+
+{
+ my $server = URI->new( $ENV{CATALYST_SERVER} || 'http://localhost' );
+ my $base = $server->host . ':' . $server->port;
+
+ my $sock = IO::Socket::INET->new(
+ PeerAddr => $server->host,
+ PeerPort => $server->port,
+ Proto => 'tcp',
+ ReuseAddr => 1,
+ Timeout => 2,
+ ) or die "Cannot connect to $server";
+
+ # Send request
+ syswrite $sock, construct_request( "http://$base/streaming" );
+
+ # Read all responses into one big buffer
+ my $buf;
+ my $sel = IO::Select->new($sock);
+ while ( $sel->can_read(1) ) {
+ my $n = sysread $sock, my $buf2, 64 * 1024;
+ last unless $n;
+ $buf .= $buf2;
+ }
+
+ my $response = HTTP::Response->parse($buf);
+
+ is( $response->code, 200, 'Response ok' );
+ is( $response->header('Transfer-Encoding'), 'chunked', 'Transfer-Encoding header chunked' );
+
+ my $expect = "4\r\nfoo\n\r\n4\r\nbar\n\r\n4\r\nbaz\n\r\n0\r\n\r\n";
+
+ is( $response->content, $expect, 'Chunked content ok' );
+}
+
+sub construct_request {
+ my @urls = @_;
+
+ my $req;
+
+ for my $url ( @urls ) {
+ my $uri = URI->new($url);
+ $req
+ .= 'GET ' . $uri->path_query . ' HTTP/1.1' . $CRLF
+ . 'Host: ' . $uri->host . ':' . $uri->port . $CRLF . $CRLF;
+ }
+
+ return $req;
+}
\ No newline at end of file
Property changes on: trunk/Catalyst-Engine-HTTP-Prefork/t/live_http11_response_chunked.t
___________________________________________________________________
Name: svn:keywords
+ Id
Added: trunk/Catalyst-Engine-HTTP-Prefork/t/live_http11_response_keepalive.t
===================================================================
--- trunk/Catalyst-Engine-HTTP-Prefork/t/live_http11_response_keepalive.t (rev 0)
+++ trunk/Catalyst-Engine-HTTP-Prefork/t/live_http11_response_keepalive.t 2008-02-26 05:24:49 UTC (rev 7458)
@@ -0,0 +1,121 @@
+#!perl
+
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use Test::More tests => 4;
+use Catalyst::Test 'TestApp';
+
+use Catalyst::Request;
+use Data::Dump qw(dump);
+use IO::Select;
+use IO::Socket qw(:crlf);
+use IO::Socket::INET;
+use HTTP::Response;
+use URI;
+
+# With LWP it is difficult or impossible to change the Connection header
+# or use HTTP/1.0, so manually create some requests
+
+my $server = URI->new( $ENV{CATALYST_SERVER} || 'http://localhost' );
+my $base = $server->host . ':' . $server->port;
+
+# Test normal HTTP/1.1 request, should return Connection: keep-alive
+{
+ my $sock = IO::Socket::INET->new(
+ PeerAddr => $server->host,
+ PeerPort => $server->port,
+ Proto => 'tcp',
+ ReuseAddr => 1,
+ Timeout => 2,
+ ) or die "Cannot connect to $server";
+
+ # Send request
+ syswrite $sock, construct_request( "http://$base/dump/request", '1.1', 'keep-alive' );
+
+ # Read/parse response
+ sysread $sock, my $buf, 64 * 1024;
+ my $response = HTTP::Response->parse($buf);
+
+ is( $response->header('Connection'), 'keep-alive', 'HTTP/1.1, keep-alive ok' );
+}
+
+# Test HTTP/1.1 with Connection: close, should return Connection: close
+{
+ my $sock = IO::Socket::INET->new(
+ PeerAddr => $server->host,
+ PeerPort => $server->port,
+ Proto => 'tcp',
+ ReuseAddr => 1,
+ Timeout => 2,
+ ) or die "Cannot connect to $server";
+
+ # Send request
+ syswrite $sock, construct_request( "http://$base/dump/request", '1.1', 'close' );
+
+ # Read/parse response
+ sysread $sock, my $buf, 64 * 1024;
+ my $response = HTTP::Response->parse($buf);
+
+ is( $response->header('Connection'), 'close', 'HTTP/1.1, close ok' );
+}
+
+# Test HTTP/1.0 with Connection: Keep-Alive header, should return Connection: keep-alive
+{
+ my $sock = IO::Socket::INET->new(
+ PeerAddr => $server->host,
+ PeerPort => $server->port,
+ Proto => 'tcp',
+ ReuseAddr => 1,
+ Timeout => 2,
+ ) or die "Cannot connect to $server";
+
+ # Send request
+ syswrite $sock, construct_request( "http://$base/dump/request", '1.0', 'keep-alive' );
+
+ # Read/parse response
+ sysread $sock, my $buf, 64 * 1024;
+ my $response = HTTP::Response->parse($buf);
+
+ is( $response->header('Connection'), 'keep-alive', 'HTTP/1.0, keep-alive ok' );
+}
+
+# Test HTTP/1.0 with no Connection header, should return Connection: close
+{
+ my $sock = IO::Socket::INET->new(
+ PeerAddr => $server->host,
+ PeerPort => $server->port,
+ Proto => 'tcp',
+ ReuseAddr => 1,
+ Timeout => 2,
+ ) or die "Cannot connect to $server";
+
+ # Send request
+ syswrite $sock, construct_request( "http://$base/dump/request", '1.0', '' );
+
+ # Read/parse response
+ sysread $sock, my $buf, 64 * 1024;
+ my $response = HTTP::Response->parse($buf);
+
+ is( $response->header('Connection'), 'close', 'HTTP/1.0, no Connection header ok' );
+}
+
+sub construct_request {
+ my ( $url, $protocol, $connection ) = @_;
+
+ my $uri = URI->new($url);
+ my $req
+ = 'GET ' . $uri->path_query . ' HTTP/' . $protocol . $CRLF
+ . 'Host: ' . $uri->host . ':' . $uri->port . $CRLF;
+
+ if ( $connection ) {
+ $req .= "Connection: $connection" . $CRLF;
+ }
+
+ $req .= $CRLF;
+
+ return $req;
+}
Property changes on: trunk/Catalyst-Engine-HTTP-Prefork/t/live_http11_response_keepalive.t
___________________________________________________________________
Name: svn:keywords
+ Id
More information about the Catalyst-commits
mailing list