1 package Catalyst::Test;
5 use Catalyst::Exception;
11 my $import = Sub::Exporter::build_exporter({
12 groups => [ all => \&build_exports ],
17 my ($self, $class) = @_;
18 $import->($self, '-all' => { class => $class });
23 my ($self, $meth, $args, $defaults) = @_;
26 my $class = $args->{class};
28 if ( $ENV{CATALYST_SERVER} ) {
29 $request = sub { remote_request(@_) };
31 $request = sub { Catalyst::Exception->throw("Must specify a test app: use Catalyst::Test 'TestApp'") };
33 unless( Class::Inspector->loaded( $class ) ) {
34 require Class::Inspector->filename( $class );
38 $request = sub { local_request( $class, @_ ) };
41 my $get = sub { $request->(@_)->content };
48 return Test::More->builder->like($get->($action),@_);
52 return Test::More->builder->ok($request->($action)->is_success, @_);
54 action_redirect => sub {
56 return Test::More->builder->ok($request->($action)->is_redirect,@_);
58 action_notfound => sub {
60 return Test::More->builder->is_eq($request->($action)->code,404,@_);
62 contenttype_is => sub {
64 my $res = $request->($action);
65 return Test::More->builder->is_eq(scalar($res->content_type),@_);
72 Catalyst::Test - Test Catalyst Applications
80 use Catalyst::Test 'TestApp';
81 request('index.html');
84 use HTTP::Request::Common;
85 my $response = request POST '/foo', [
90 # Run tests against a remote server
91 CATALYST_SERVER='http://localhost:3000/' prove -r -l lib/ t/
93 # Tests with inline apps need to use Catalyst::Engine::Test
99 my ( $self, $c ) = @_;
100 $c->res->output('bar');
103 __PACKAGE__->setup();
107 use Catalyst::Test 'TestApp';
108 use Test::More tests => 1;
110 ok( get('/foo') =~ /bar/ );
114 This module allows you to make requests to a Catalyst application either without
115 a server, by simulating the environment of an HTTP request using
116 L<HTTP::Request::AsCGI> or remotely if you define the CATALYST_SERVER
117 environment variable. This module also adds a few catalyst
118 specific testing methods as displayed in the method section.
120 The </get> and </request> functions take either a URI or an L<HTTP::Request>
129 my $content = get('foo/bar?test=1');
131 Note that this method doesn't follow redirects, so to test for a
132 correctly redirecting page you'll need to use a combination of this
133 method and the L<request> method below:
135 my $res = request('/'); # redirects to /y
136 warn $res->header('location');
138 my $uri = URI->new($res->header('location'));
139 is ( $uri->path , '/y');
140 my $content = get($uri->path);
144 Returns a C<HTTP::Response> object.
146 my $res = request('foo/bar?test=1');
150 Simulate a request using L<HTTP::Request::AsCGI>.
157 require HTTP::Request::AsCGI;
159 my $request = Catalyst::Utils::request( shift(@_) );
160 my $cgi = HTTP::Request::AsCGI->new( $request, %ENV )->setup;
162 $class->handle_request;
164 return $cgi->restore->response;
169 =head2 remote_request
171 Do an actual remote request using LWP.
177 require LWP::UserAgent;
179 my $request = Catalyst::Utils::request( shift(@_) );
180 my $server = URI->new( $ENV{CATALYST_SERVER} );
182 if ( $server->path =~ m|^(.+)?/$| ) {
184 $server->path("$path") if $path; # need to be quoted
187 # the request path needs to be sanitised if $server is using a
188 # non-root path due to potential overlap between request path and
191 # If request path is '/', we have to add a trailing slash to the
193 my $add_trailing = $request->uri->path eq '/';
195 my @sp = split '/', $server->path;
196 my @rp = split '/', $request->uri->path;
197 shift @sp;shift @rp; # leading /
199 foreach my $sp (@sp) {
200 $sp eq $rp[0] ? shift @rp : last
203 $request->uri->path(join '/', @rp);
205 if ( $add_trailing ) {
206 $request->uri->path( $request->uri->path . '/' );
210 $request->uri->scheme( $server->scheme );
211 $request->uri->host( $server->host );
212 $request->uri->port( $server->port );
213 $request->uri->path( $server->path . $request->uri->path );
217 $agent = LWP::UserAgent->new(
226 return $agent->request($request);
231 Fetches the given url and check that the request was successful
233 =head2 action_redirect
235 Fetches the given url and check that the request was a redirect
237 =head2 action_notfound
239 Fetches the given url and check that the request was not found
243 Fetches the given url and matches the content against it.
245 =head2 contenttype_is
247 Check for given mime type
251 L<Catalyst>, L<Test::WWW::Mechanize::Catalyst>,
252 L<Test::WWW::Selenium::Catalyst>, L<Test::More>, L<HTTP::Request::Common>
256 Catalyst Contributors, see Catalyst.pm
260 This program is free software, you can redistribute it and/or modify it under
261 the same terms as Perl itself.