X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FTest.pm;h=97de28fe76c2873e5fdf779df084397df9bdcc8b;hb=b6898a9f489a6e5946a4640ec049ada70f47a48a;hp=824c57797c4faf00dca8908aeff4d5a753fc91b7;hpb=c10563323dadc3194743c5e460840380e8c07703;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Test.pm b/lib/Catalyst/Test.pm index 824c577..97de28f 100644 --- a/lib/Catalyst/Test.pm +++ b/lib/Catalyst/Test.pm @@ -2,16 +2,11 @@ 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 +22,25 @@ Catalyst::Test - Test Catalyst applications request('index.html'); get('index.html'); - # Request - perl -MCatalyst::Test=MyApp -e1 index.html + # 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,15 +62,6 @@ 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; if ( $class = shift ) { @@ -66,55 +69,12 @@ sub import { 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 }; - } -} -sub request { - 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 = ''; - $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: $!"); + no strict 'refs'; + my $caller = caller(0); + *{"$caller\::request"} = sub { $class->run(@_) }; + *{"$caller\::get"} = sub { $class->run(@_)->content }; } - open( STDOUT, '>', \$output ); - $class->handler; - %ENV = %clean; - return HTTP::Response->parse($output); } =head1 SEE ALSO