X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FTest.pm;h=0d2acc9e3a667557f8d40a2d6f1edbf040995bed;hb=7c48ba15d6cf5328b7773e998e3cffd6a0890e03;hp=fd53e180b2883d69b6a2273dd0d1354a80636ad0;hpb=bc024080353d9982272a134e49e8601ed20c1a50;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Test.pm b/lib/Catalyst/Test.pm index fd53e18..0d2acc9 100644 --- a/lib/Catalyst/Test.pm +++ b/lib/Catalyst/Test.pm @@ -2,6 +2,8 @@ package Catalyst::Test; use strict; use UNIVERSAL::require; +use IO::File; +use HTTP::Request; use HTTP::Response; use Socket; use URI; @@ -18,6 +20,11 @@ Catalyst::Test - Test Catalyst applications =head1 SYNOPSIS + # Helper + script/cgi-server.pl + script/server.pl + script/test.pl + # Tests use Catalyst::Test 'TestApp'; request('index.html'); @@ -74,17 +81,43 @@ sub import { } sub request { - my $uri = shift; - local *STDOUT; + my $request = shift; + unless ( ref $request ) { + $request = URI->new( $request, 'http' ); + } + unless ( ref $request eq 'HTTP::Request' ) { + $request = HTTP::Request->new( 'GET', $request ); + } + local ( *STDIN, *STDOUT ); + my %clean = %ENV; my $output = ''; - open STDOUT, '>', \$output; - $uri = URI->new($uri); - my %clean = %ENV; - $ENV{REQUEST_METHOD} ||= 'GET'; - $ENV{HTTP_HOST} ||= $uri->authority || 'localhost'; - $ENV{SCRIPT_NAME} ||= $uri->path || '/'; - $ENV{QUERY_STRING} ||= $uri->query || ''; - $ENV{CONTENT_TYPE} ||= 'text/plain'; + $ENV{CONTENT_TYPE} ||= $request->header('Content-Type') || ''; + $ENV{CONTENT_LENGTH} ||= $request->header('Content-Length') || ''; + $ENV{GATEWAY_INTERFACE} ||= 'CGI/1.1'; + $ENV{HTTP_USER_AGENT} ||= 'Catalyst'; + $ENV{HTTP_HOST} ||= $request->uri->host || 'localhost'; + $ENV{QUERY_STRING} ||= $request->uri->query || ''; + $ENV{REQUEST_METHOD} ||= $request->method; + $ENV{SCRIPT_NAME} ||= $request->uri->path || '/'; + $ENV{SERVER_NAME} ||= $request->uri->host || 'localhost'; + $ENV{SERVER_PORT} ||= $request->uri->port; + $ENV{SERVER_PROTOCOL} ||= 'HTTP/1.1'; + + for my $field ( $request->header_field_names ) { + if ( $field =~ /^Content-(Length|Type)$/ ) { + next; + } + $field =~ s/-/_/g; + $ENV{ 'HTTP_' . uc($field) } = $request->header($field); + } + if ( $request->content_length ) { + my $body = IO::File->new_tmpfile; + $body->print( $request->content ) or die $!; + $body->seek( 0, SEEK_SET ) or die $!; + open( STDIN, "<&=", $body->fileno ) + or die("Failed to dup \$body: $!"); + } + open( STDOUT, '>', \$output ); $class->handler; %ENV = %clean; return HTTP::Response->parse($output);