X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FTest.pm;h=4ca73ee7a04877c8081831f978984df8dc082d1c;hb=d96e14c21fb544597f459aaaad969c34af7f2d1f;hp=824c57797c4faf00dca8908aeff4d5a753fc91b7;hpb=c10563323dadc3194743c5e460840380e8c07703;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Test.pm b/lib/Catalyst/Test.pm index 824c577..4ca73ee 100644 --- a/lib/Catalyst/Test.pm +++ b/lib/Catalyst/Test.pm @@ -2,16 +2,8 @@ package Catalyst::Test; use strict; use UNIVERSAL::require; -use IO::File; -use HTTP::Request; -use HTTP::Response; -use Socket; -use URI; -require Catalyst; - -my $class; -$ENV{CATALYST_ENGINE} = 'CGI'; +$ENV{CATALYST_ENGINE} = 'Test'; =head1 NAME @@ -27,8 +19,27 @@ Catalyst::Test - Test Catalyst applications request('index.html'); get('index.html'); - # Request - perl -MCatalyst::Test=MyApp -e1 index.html + # Run tests against a remote server + CATALYST_SERVER='http://localhost:3000/' prove -l lib/ t/ + + # Tests with inline apps need to use Catalyst::Engine::Test + package TestApp; + + use Catalyst qw[-Engine=Test]; + + __PACKAGE__->action( + foo => sub { + my ( $self, $c ) = @_; + $c->res->output('bar'); + } + ); + + package main; + + use Test::More tests => 1; + use Catalyst::Test 'TestApp'; + + ok( get('/foo') =~ /bar/ ); =head1 DESCRIPTION @@ -50,71 +61,64 @@ Returns a C object. =cut -{ - no warnings; - CHECK { - if ( ( caller(0) )[1] eq '-e' ) { - print request( $ARGV[0] || 'http://localhost' )->content; - } +sub import { + my $self = shift; + my $class = shift; + + my ( $get, $request ); + + if ( $ENV{CATALYST_SERVER} ) { + $request = sub { remote_request(@_) }; + $get = sub { remote_request(@_)->content }; } -} -sub import { - my $self = shift; - if ( $class = shift ) { + else { $class->require; + unless ( $INC{'Test/Builder.pm'} ) { die qq/Couldn't load "$class", "$@"/ if $@; } - my $caller = caller(0); - no strict 'refs'; - *{"$caller\::request"} = \&request; - *{"$caller\::get"} = sub { request(@_)->content }; + + $class->import; + + $request = sub { $class->run(@_) }; + $get = sub { $class->run(@_)->content }; } + + no strict 'refs'; + my $caller = caller(0); + *{"$caller\::request"} = $request; + *{"$caller\::get"} = $get; } -sub request { +sub remote_request { my $request = shift; + + require LWP::UserAgent; + + my $remote = URI->new( $ENV{CATALYST_SERVER} ); + unless ( ref $request ) { - $request = URI->new( $request, 'http' ); + + my $uri = + ( $request =~ m/http/i ) + ? URI->new($request) + : URI->new( 'http://localhost' . $request ); + + $request = $uri->canonical; } + + $request->scheme( $remote->scheme ); + $request->host( $remote->host ); + $request->port( $remote->port ); + unless ( ref $request eq 'HTTP::Request' ) { $request = HTTP::Request->new( 'GET', $request ); } - local ( *STDIN, *STDOUT ); - my %clean = %ENV; - my $output = ''; - $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{PATH_INFO} ||= $request->uri->path || '/'; - $ENV{SCRIPT_NAME} ||= '/'; - $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); + + my $agent = LWP::UserAgent->new; + + return $agent->request($request); } =head1 SEE ALSO