X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FTest.pm;h=821b6e314aeaa51e2b2f627bd3eb8450c5ad5cf8;hb=3640641e13deb51a427e7c0ecf13ade30bfa6db7;hp=3b319ed2b23d1e518d65af94244acea349906dfe;hpb=94f74acdfa62a94db80eb08f12078f0e0d513fc3;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Test.pm b/lib/Catalyst/Test.pm index 3b319ed..821b6e3 100644 --- a/lib/Catalyst/Test.pm +++ b/lib/Catalyst/Test.pm @@ -4,10 +4,13 @@ use strict; use warnings; use Test::More (); +use Plack::Test; +use Plack::Middleware::OverrideEnv; use Catalyst::Exception; use Catalyst::Utils; use Class::MOP; use Sub::Exporter; +use Carp; my $build_exports = sub { my ($self, $meth, $args, $defaults) = @_; @@ -17,15 +20,17 @@ my $build_exports = sub { if ( $ENV{CATALYST_SERVER} ) { $request = sub { remote_request(@_) }; - } elsif (! $class) { - $request = sub { Catalyst::Exception->throw("Must specify a test app: use Catalyst::Test 'TestApp'") }; + } elsif (!$class) { + $request = sub { croak "Must specify a test app: use Catalyst::Test 'TestApp'"; } } else { unless (Class::MOP::is_class_loaded($class)) { Class::MOP::load_class($class); } $class->import; - $request = sub { local_request( $class, @_ ) }; + my $app = $class->engine->_build_psgi_app($class); + + $request = sub { local_request( $app, @_ ) }; } my $get = sub { $request->(@_)->content }; @@ -156,8 +161,8 @@ L or remotely if you define the CATALYST_SERVER environment variable. This module also adds a few Catalyst-specific testing methods as displayed in the method section. -The L and L functions take either a URI or an L -object. +The L and L +functions take either a URI or an L object. =head1 INLINE TESTS WILL NO LONGER WORK @@ -187,7 +192,7 @@ Returns the content. Note that this method doesn't follow redirects, so to test for a correctly redirecting page you'll need to use a combination of this -method and the L method below: +method and the L method below: my $res = request('/'); # redirects to /y warn $res->header('location'); @@ -208,7 +213,7 @@ header configuration; currently only supports setting 'host' value. =head2 ($res, $c) = ctx_request( ... ); -Works exactly like L, except it also returns the Catalyst context object, +Works exactly like L, except it also returns the Catalyst context object, C<$c>. Note that this only works for local requests. =head2 $res = Catalyst::Test::local_request( $AppClass, $url ); @@ -218,17 +223,22 @@ Simulate a request using L. =cut sub local_request { - my $class = shift; - - require HTTP::Request::AsCGI; - - my $request = Catalyst::Utils::request( shift(@_) ); - _customize_request($request, @_); - my $cgi = HTTP::Request::AsCGI->new( $request, %ENV )->setup; - - $class->handle_request( env => \%ENV ); - - return $cgi->restore->response; + my $app = shift; + + my $request = Catalyst::Utils::request(shift); + my %extra_env; + _customize_request($request, \%extra_env, @_); + + my $ret; + test_psgi + app => Plack::Middleware::OverrideEnv->wrap( + $app, env_override => \%extra_env, + ), + client => sub { + $ret = shift->($request); + }; + + return $ret; } my $agent; @@ -301,11 +311,16 @@ sub remote_request { sub _customize_request { my $request = shift; + my $extra_env = shift; my $opts = pop(@_) || {}; $opts = {} unless ref($opts) eq 'HASH'; if ( my $host = exists $opts->{host} ? $opts->{host} : $default_host ) { $request->header( 'Host' => $host ); } + + if (my $extra = $opts->{extra_env}) { + @{ $extra_env }{keys %{ $extra }} = values %{ $extra }; + } } =head2 action_ok