1 package Catalyst::Test;
8 use Catalyst::Exception;
12 use parent qw/Exporter/;
13 our @EXPORT=qw/&content_like &action_ok &action_redirect &action_notfound &contenttype_is/;
18 Catalyst::Test - Test Catalyst Applications
26 use Catalyst::Test 'TestApp';
27 request('index.html');
30 use HTTP::Request::Common;
31 my $response = request POST '/foo', [
36 # Run tests against a remote server
37 CATALYST_SERVER='http://localhost:3000/' prove -r -l lib/ t/
39 # Tests with inline apps need to use Catalyst::Engine::Test
45 my ( $self, $c ) = @_;
46 $c->res->output('bar');
53 use Catalyst::Test 'TestApp';
54 use Test::More tests => 1;
56 ok( get('/foo') =~ /bar/ );
60 This module allows you to make requests to a Catalyst application either without
61 a server, by simulating the environment of an HTTP request using
62 L<HTTP::Request::AsCGI> or remotely if you define the CATALYST_SERVER
63 environment variable. This module also adds a few catalyst
64 specific testing methods as displayed in the method section.
66 The </get> and </request> functions take either a URI or an L<HTTP::Request>
75 my $content = get('foo/bar?test=1');
77 Note that this method doesn't follow redirects, so to test for a
78 correctly redirecting page you'll need to use a combination of this
79 method and the L<request> method below:
81 my $res = request('/'); # redirects to /y
82 warn $res->header('location');
84 my $uri = URI->new($res->header('location'));
85 is ( $uri->path , '/y');
86 my $content = get($uri->path);
90 Returns a C<HTTP::Response> object.
92 my $res = request('foo/bar?test=1');
100 my ( $get, $request );
102 if ( $ENV{CATALYST_SERVER} ) {
103 $request = sub { remote_request(@_) };
104 $get = sub { remote_request(@_)->content };
106 $request = sub { Catalyst::Exception->throw("Must specify a test app: use Catalyst::Test 'TestApp'") };
109 unless( Class::Inspector->loaded( $class ) ) {
110 require Class::Inspector->filename( $class );
114 $request = sub { local_request( $class, @_ ) };
115 $get = sub { local_request( $class, @_ )->content };
119 my $caller = caller(0);
120 *{"$caller\::request"} = $request;
121 *{"$caller\::get"} = $get;
122 __PACKAGE__->export_to_level(1);
127 Simulate a request using L<HTTP::Request::AsCGI>.
134 require HTTP::Request::AsCGI;
136 my $request = Catalyst::Utils::request( shift(@_) );
137 my $cgi = HTTP::Request::AsCGI->new( $request, %ENV )->setup;
139 $class->handle_request;
141 return $cgi->restore->response;
146 =head2 remote_request
148 Do an actual remote request using LWP.
154 require LWP::UserAgent;
156 my $request = Catalyst::Utils::request( shift(@_) );
157 my $server = URI->new( $ENV{CATALYST_SERVER} );
159 if ( $server->path =~ m|^(.+)?/$| ) {
161 $server->path("$path") if $path; # need to be quoted
164 # the request path needs to be sanitised if $server is using a
165 # non-root path due to potential overlap between request path and
168 # If request path is '/', we have to add a trailing slash to the
170 my $add_trailing = $request->uri->path eq '/';
172 my @sp = split '/', $server->path;
173 my @rp = split '/', $request->uri->path;
174 shift @sp;shift @rp; # leading /
176 foreach my $sp (@sp) {
177 $sp eq $rp[0] ? shift @rp : last
180 $request->uri->path(join '/', @rp);
182 if ( $add_trailing ) {
183 $request->uri->path( $request->uri->path . '/' );
187 $request->uri->scheme( $server->scheme );
188 $request->uri->host( $server->host );
189 $request->uri->port( $server->port );
190 $request->uri->path( $server->path . $request->uri->path );
194 $agent = LWP::UserAgent->new(
203 return $agent->request($request);
208 Fetches the given url and check that the request was successful
210 =head2 action_redirect
212 Fetches the given url and check that the request was a redirect
214 =head2 action_notfound
216 Fetches the given url and check that the request was not found
220 Fetches the given url and matches the content against it.
222 =head2 contenttype_is
224 Check for given mime type
229 my $caller=caller(0);
231 my $get=*{"$caller\::get"};
233 return Test::More->builder->like(&$get($action),@_);
237 my $caller=caller(0);
239 my $request=*{"$caller\::request"};
241 return Test::More->builder->ok(&$request($action)->is_success, @_);
244 sub action_redirect {
245 my $caller=caller(0);
247 my $request=*{"$caller\::request"};
249 return Test::More->builder->ok(&$request($action)->is_redirect,@_);
253 sub action_notfound {
254 my $caller=caller(0);
256 my $request=*{"$caller\::request"};
258 return Test::More->builder->is_eq(&$request($action)->code,404,@_);
264 my $caller=caller(0);
266 my $request=*{"$caller\::request"};
268 my $res=&$request($action);
269 return Test::More->builder->is_eq(scalar($res->content_type),@_);
274 L<Catalyst>, L<Test::WWW::Mechanize::Catalyst>,
275 L<Test::WWW::Selenium::Catalyst>, L<Test::More>, L<HTTP::Request::Common>
279 Catalyst Contributors, see Catalyst.pm
283 This program is free software, you can redistribute it and/or modify it under
284 the same terms as Perl itself.