1 package Catalyst::Test;
5 use Catalyst::Exception;
11 my ($self, $meth, $args, $defaults) = @_;
14 my $class = $args->{class};
16 if ( $ENV{CATALYST_SERVER} ) {
17 $request = sub { remote_request(@_) };
19 $request = sub { Catalyst::Exception->throw("Must specify a test app: use Catalyst::Test 'TestApp'") };
21 unless (Class::MOP::is_class_loaded($class)) {
22 Class::MOP::load_class($class);
26 $request = sub { local_request( $class, @_ ) };
29 my $get = sub { $request->(@_)->content };
36 return Test::More->builder->like($get->($action),@_);
40 return Test::More->builder->ok($request->($action)->is_success, @_);
42 action_redirect => sub {
44 return Test::More->builder->ok($request->($action)->is_redirect,@_);
46 action_notfound => sub {
48 return Test::More->builder->is_eq($request->($action)->code,404,@_);
50 contenttype_is => sub {
52 my $res = $request->($action);
53 return Test::More->builder->is_eq(scalar($res->content_type),@_);
61 my $import = Sub::Exporter::build_exporter({
62 groups => [ all => \&build_exports ],
67 my ($self, $class) = @_;
68 $import->($self, '-all' => { class => $class });
74 Catalyst::Test - Test Catalyst Applications
82 use Catalyst::Test 'TestApp';
83 request('index.html');
86 use HTTP::Request::Common;
87 my $response = request POST '/foo', [
92 # Run tests against a remote server
93 CATALYST_SERVER='http://localhost:3000/' prove -r -l lib/ t/
95 # Tests with inline apps need to use Catalyst::Engine::Test
101 my ( $self, $c ) = @_;
102 $c->res->output('bar');
105 __PACKAGE__->setup();
109 use Catalyst::Test 'TestApp';
110 use Test::More tests => 1;
112 ok( get('/foo') =~ /bar/ );
116 This module allows you to make requests to a Catalyst application either without
117 a server, by simulating the environment of an HTTP request using
118 L<HTTP::Request::AsCGI> or remotely if you define the CATALYST_SERVER
119 environment variable. This module also adds a few catalyst
120 specific testing methods as displayed in the method section.
122 The </get> and </request> functions take either a URI or an L<HTTP::Request>
131 my $content = get('foo/bar?test=1');
133 Note that this method doesn't follow redirects, so to test for a
134 correctly redirecting page you'll need to use a combination of this
135 method and the L<request> method below:
137 my $res = request('/'); # redirects to /y
138 warn $res->header('location');
140 my $uri = URI->new($res->header('location'));
141 is ( $uri->path , '/y');
142 my $content = get($uri->path);
146 Returns a C<HTTP::Response> object.
148 my $res = request('foo/bar?test=1');
152 Simulate a request using L<HTTP::Request::AsCGI>.
159 require HTTP::Request::AsCGI;
161 my $request = Catalyst::Utils::request( shift(@_) );
162 my $cgi = HTTP::Request::AsCGI->new( $request, %ENV )->setup;
164 $class->handle_request;
166 return $cgi->restore->response;
171 =head2 remote_request
173 Do an actual remote request using LWP.
179 require LWP::UserAgent;
181 my $request = Catalyst::Utils::request( shift(@_) );
182 my $server = URI->new( $ENV{CATALYST_SERVER} );
184 if ( $server->path =~ m|^(.+)?/$| ) {
186 $server->path("$path") if $path; # need to be quoted
189 # the request path needs to be sanitised if $server is using a
190 # non-root path due to potential overlap between request path and
193 # If request path is '/', we have to add a trailing slash to the
195 my $add_trailing = $request->uri->path eq '/';
197 my @sp = split '/', $server->path;
198 my @rp = split '/', $request->uri->path;
199 shift @sp;shift @rp; # leading /
201 foreach my $sp (@sp) {
202 $sp eq $rp[0] ? shift @rp : last
205 $request->uri->path(join '/', @rp);
207 if ( $add_trailing ) {
208 $request->uri->path( $request->uri->path . '/' );
212 $request->uri->scheme( $server->scheme );
213 $request->uri->host( $server->host );
214 $request->uri->port( $server->port );
215 $request->uri->path( $server->path . $request->uri->path );
219 $agent = LWP::UserAgent->new(
228 return $agent->request($request);
233 Fetches the given url and check that the request was successful
235 =head2 action_redirect
237 Fetches the given url and check that the request was a redirect
239 =head2 action_notfound
241 Fetches the given url and check that the request was not found
245 Fetches the given url and matches the content against it.
247 =head2 contenttype_is
249 Check for given mime type
253 L<Catalyst>, L<Test::WWW::Mechanize::Catalyst>,
254 L<Test::WWW::Selenium::Catalyst>, L<Test::More>, L<HTTP::Request::Common>
258 Catalyst Contributors, see Catalyst.pm
262 This program is free software, you can redistribute it and/or modify it under
263 the same terms as Perl itself.