1 package Catalyst::Test;
6 use Catalyst::Exception;
12 Catalyst::Test - Test Catalyst Applications
20 use Catalyst::Test 'TestApp';
21 request('index.html');
24 # Run tests against a remote server
25 CATALYST_SERVER='http://localhost:3000/' prove -r -l lib/ t/
27 # Tests with inline apps need to use Catalyst::Engine::Test
33 my ( $self, $c ) = @_;
34 $c->res->output('bar');
41 use Test::More tests => 1;
42 use Catalyst::Test 'TestApp';
44 ok( get('/foo') =~ /bar/ );
48 Test Catalyst Applications.
56 my $content = get('foo/bar?test=1');
58 Note that this method doesn't follow redirects, so to test for a
59 correctly redirecting page you'll need to use a combination of this
60 method and the L<request> method below:
62 my $res = request('/'); # redirects to /y
63 warn $res->header('location');
65 my $uri = URI->new($res->header('location'));
66 is ( $uri->path , '/y');
67 my $content = get($uri->path);
71 Returns a C<HTTP::Response> object.
73 my $res = request('foo/bar?test=1');
81 my ( $get, $request );
83 if ( $ENV{CATALYST_SERVER} ) {
84 $request = sub { remote_request(@_) };
85 $get = sub { remote_request(@_)->content };
87 $request = sub { Catalyst::Exception->throw("Must specify a test app: use Catalyst::Test 'TestApp'") };
90 unless( Class::Inspector->loaded( $class ) ) {
91 require Class::Inspector->filename( $class );
95 $request = sub { local_request( $class, @_ ) };
96 $get = sub { local_request( $class, @_ )->content };
100 my $caller = caller(0);
101 *{"$caller\::request"} = $request;
102 *{"$caller\::get"} = $get;
112 require HTTP::Request::AsCGI;
114 my $request = Catalyst::Utils::request( shift(@_) );
115 my $cgi = HTTP::Request::AsCGI->new( $request, %ENV )->setup;
117 $class->handle_request;
119 return $cgi->restore->response;
124 =head2 remote_request
126 Do an actual remote request using LWP.
132 require LWP::UserAgent;
134 my $request = Catalyst::Utils::request( shift(@_) );
135 my $server = URI->new( $ENV{CATALYST_SERVER} );
137 if ( $server->path =~ m|^(.+)?/$| ) {
138 $server->path("$1"); # need to be quoted
141 # the request path needs to be sanitised if $server is using a
142 # non-root path due to potential overlap between request path and
145 # If request path is '/', we have to add a trailing slash to the
147 my $add_trailing = $request->uri->path eq '/';
149 my @sp = split '/', $server->path;
150 my @rp = split '/', $request->uri->path;
151 shift @sp;shift @rp; # leading /
153 foreach my $sp (@sp) {
154 $sp eq $rp[0] ? shift @rp : last
157 $request->uri->path(join '/', @rp);
159 if ( $add_trailing ) {
160 $request->uri->path( $request->uri->path . '/' );
164 $request->uri->scheme( $server->scheme );
165 $request->uri->host( $server->host );
166 $request->uri->port( $server->port );
167 $request->uri->path( $server->path . $request->uri->path );
171 $agent = LWP::UserAgent->new(
180 return $agent->request($request);
189 Sebastian Riedel, C<sri@cpan.org>
193 This program is free software, you can redistribute it and/or modify it under
194 the same terms as Perl itself.