X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FTest.pm;h=3af0f151f677f318a863b412b9482e04597869c5;hb=8b76bfcf2b7d98cd72fc7ba702a15684bdee06f0;hp=5fe2a0ca0d010b3b15ad81f900d74fa3a298438a;hpb=523d44ecdd184a0a56c8094c60fe545098e06cff;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Test.pm b/lib/Catalyst/Test.pm index 5fe2a0c..3af0f15 100644 --- a/lib/Catalyst/Test.pm +++ b/lib/Catalyst/Test.pm @@ -1,10 +1,31 @@ package Catalyst::Test; use strict; +use warnings; + +use Catalyst::Exception; +use Catalyst::Utils; use UNIVERSAL::require; +use HTTP::Headers; $ENV{CATALYST_ENGINE} = 'Test'; +# Bypass a HTTP::Headers bug +{ + no warnings 'redefine'; + + sub HTTP::Headers::new { + my $class = shift; + my $self = bless {}, $class; + if (@_) { + while ( my ( $field, $val ) = splice( @_, 0, 2 ) ) { + $self->push_header( $field, $val ); + } + } + return $self; + } +} + =head1 NAME Catalyst::Test - Test Catalyst applications @@ -20,19 +41,19 @@ Catalyst::Test - Test Catalyst applications get('index.html'); # Run tests against a remote server - CATALYST_SERVER='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; @@ -47,17 +68,19 @@ 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. - my $res =request('foo/bar?test=1'); + my $res = request('foo/bar?test=1'); =cut @@ -74,11 +97,7 @@ sub import { else { $class->require; - - unless ( $INC{'Test/Builder.pm'} ) { - die qq/Couldn't load "$class", "$@"/ if $@; - } - + die if $@ && $@ !~ /^Can't locate /; $class->import; $request = sub { $class->run(@_) }; @@ -93,28 +112,22 @@ sub import { my $agent; -sub remote_request { - my $request = shift; +=item remote_request - require LWP::UserAgent; +Do an actual remote request using LWP. - unless ( ref $request ) { +=cut - my $uri = ( $request =~ m/http/i ) - ? URI->new($request) - : URI->new( 'http://localhost' . $request ); +sub remote_request { - $request = $uri->canonical; - } + require LWP::UserAgent; - unless ( ref $request eq 'HTTP::Request' ) { - $request = HTTP::Request->new( 'GET', $request ); - } + my $request = Catalyst::Utils::request( shift(@_) ); my $server = URI->new( $ENV{CATALYST_SERVER} ); if ( $server->path =~ m|^(.+)?/$| ) { - $server->path("$1"); # need to be quoted + $server->path("$1"); # need to be quoted } $request->uri->scheme( $server->scheme ); @@ -123,18 +136,21 @@ sub remote_request { $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); } +=back + =head1 SEE ALSO L.