X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FTest.pm;h=5fe2a0ca0d010b3b15ad81f900d74fa3a298438a;hb=523d44ecdd184a0a56c8094c60fe545098e06cff;hp=f19b76f53f030815a42e81c334747d42a715acea;hpb=d8c1e61236972c22452ef3f5ec9407181ad74ea0;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Test.pm b/lib/Catalyst/Test.pm index f19b76f..5fe2a0c 100644 --- a/lib/Catalyst/Test.pm +++ b/lib/Catalyst/Test.pm @@ -19,6 +19,9 @@ Catalyst::Test - Test Catalyst applications request('index.html'); get('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; @@ -38,7 +41,6 @@ Catalyst::Test - Test Catalyst applications ok( get('/foo') =~ /bar/ ); - =head1 DESCRIPTION Test Catalyst applications. @@ -60,18 +62,77 @@ Returns a C object. =cut sub import { - my $self = shift; - if ( my $class = shift ) { + my $self = shift; + my $class = shift; + + my ( $get, $request ); + + 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 $@; } - no strict 'refs'; - my $caller = caller(0); - *{"$caller\::request"} = sub { $class->run(@_) }; - *{"$caller\::get"} = sub { $class->run(@_)->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; +} + +my $agent; + +sub remote_request { + my $request = shift; + + require LWP::UserAgent; + + unless ( ref $request ) { + + my $uri = ( $request =~ m/http/i ) + ? URI->new($request) + : URI->new( 'http://localhost' . $request ); + + $request = $uri->canonical; + } + + unless ( ref $request eq 'HTTP::Request' ) { + $request = HTTP::Request->new( 'GET', $request ); + } + + my $server = URI->new( $ENV{CATALYST_SERVER} ); + + if ( $server->path =~ m|^(.+)?/$| ) { + $server->path("$1"); # need to be quoted + } + + $request->uri->scheme( $server->scheme ); + $request->uri->host( $server->host ); + $request->uri->port( $server->port ); + $request->uri->path( $server->path . $request->uri->path ); + + unless ($agent) { + $agent = LWP::UserAgent->new( + # cookie_jar => {}, + keep_alive => 1, + max_redirect => 0, + timeout => 60, + ); + $agent->env_proxy; + } + + return $agent->request($request); } =head1 SEE ALSO