X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=blobdiff_plain;f=lib%2FCatalyst%2FTest.pm;h=023b8e51c1f7ec58b0f97131ead9c5687e68862a;hp=b0776e49483945ee6830977661c3d40c05ecd7ab;hb=68eb5874bcda8eed45d92481248a77533944671a;hpb=45374ac6977e9464410a8c7518fb26ab812258cb diff --git a/lib/Catalyst/Test.pm b/lib/Catalyst/Test.pm index b0776e4..023b8e5 100644 --- a/lib/Catalyst/Test.pm +++ b/lib/Catalyst/Test.pm @@ -1,6 +1,9 @@ package Catalyst::Test; use strict; + +use Catalyst::Exception; +use Catalyst::Utils; use UNIVERSAL::require; $ENV{CATALYST_ENGINE} = 'Test'; @@ -20,19 +23,19 @@ Catalyst::Test - Test Catalyst applications get('index.html'); # Run tests against a remote server - CATALYST_REMOTE='http://localhost:3000/' prove -l lib/ t/ + CATALYST_SERVER='http://localhost:3000/' prove -r -l lib/ t/ # Tests with inline apps need to use Catalyst::Engine::Test package TestApp; use Catalyst qw[-Engine=Test]; - __PACKAGE__->action( - foo => sub { + sub foo : Global { my ( $self, $c ) = @_; $c->res->output('bar'); - } - ); + } + + __PACKAGE__->setup(); package main; @@ -41,20 +44,21 @@ Catalyst::Test - Test Catalyst applications ok( get('/foo') =~ /bar/ ); - =head1 DESCRIPTION Test Catalyst applications. =head2 METHODS -=head3 get +=over 4 + +=item get Returns the content. my $content = get('foo/bar?test=1'); -=head3 request +=item request Returns a C object. @@ -63,21 +67,19 @@ Returns a C object. =cut sub import { - my $self = shift; + my $self = shift; my $class = shift; my ( $get, $request ); - if ( $ENV{CATALYST_REMOTE} ) { + if ( $ENV{CATALYST_SERVER} ) { $request = sub { remote_request(@_) }; $get = sub { remote_request(@_)->content }; } else { $class->require; - unless ( $INC{'Test/Builder.pm'} ) { - die qq/Couldn't load "$class", "$@"/ if $@; - } + $class->import; $request = sub { $class->run(@_) }; $get = sub { $class->run(@_)->content }; @@ -89,35 +91,47 @@ sub import { *{"$caller\::get"} = $get; } +my $agent; + +=item remote_request + +Do an actual remote request using LWP. + +=cut + sub remote_request { - my $request = shift; require LWP::UserAgent; - my $remote = URI->new( $ENV{CATALYST_REMOTE} ); + my $request = Catalyst::Utils::request( shift(@_) ); - unless ( ref $request ) { + my $server = URI->new( $ENV{CATALYST_SERVER} ); - my $uri = ( $request =~ m/http/i ) - ? URI->new($request) - : URI->new( 'http://localhost' . $request ); - - $request = $uri->canonical; + if ( $server->path =~ m|^(.+)?/$| ) { + $server->path("$1"); # need to be quoted } - $request->scheme( $remote->scheme ); - $request->host( $remote->host ); - $request->port( $remote->port ); + $request->uri->scheme( $server->scheme ); + $request->uri->host( $server->host ); + $request->uri->port( $server->port ); + $request->uri->path( $server->path . $request->uri->path ); - unless ( ref $request eq 'HTTP::Request' ) { - $request = HTTP::Request->new( 'GET', $request ); - } + unless ($agent) { - my $agent = LWP::UserAgent->new; + $agent = LWP::UserAgent->new( + keep_alive => 1, + max_redirect => 0, + timeout => 60, + ); + + $agent->env_proxy; + } return $agent->request($request); } +=back + =head1 SEE ALSO L.