1 package Catalyst::Test;
7 use Catalyst::Exception;
13 my ($self, $meth, $args, $defaults) = @_;
16 my $class = $args->{class};
18 if ( $ENV{CATALYST_SERVER} ) {
19 $request = sub { remote_request(@_) };
21 $request = sub { Catalyst::Exception->throw("Must specify a test app: use Catalyst::Test 'TestApp'") };
23 unless (Class::MOP::is_class_loaded($class)) {
24 Class::MOP::load_class($class);
28 $request = sub { local_request( $class, @_ ) };
31 my $get = sub { $request->(@_)->content };
38 return Test::More->builder->like($get->($action),@_);
42 return Test::More->builder->ok($request->($action)->is_success, @_);
44 action_redirect => sub {
46 return Test::More->builder->ok($request->($action)->is_redirect,@_);
48 action_notfound => sub {
50 return Test::More->builder->is_eq($request->($action)->code,404,@_);
52 contenttype_is => sub {
54 my $res = $request->($action);
55 return Test::More->builder->is_eq(scalar($res->content_type),@_);
64 my $import = Sub::Exporter::build_exporter({
65 groups => [ all => \&build_exports ],
71 my ($self, $class, $opts) = @_;
72 $import->($self, '-all' => { class => $class });
73 $opts = {} unless ref $opts eq 'HASH';
74 $default_host = $opts->{default_host} if exists $opts->{default_host};
80 Catalyst::Test - Test Catalyst Applications
88 use Catalyst::Test 'TestApp';
89 request('index.html');
92 use HTTP::Request::Common;
93 my $response = request POST '/foo', [
98 # Run tests against a remote server
99 CATALYST_SERVER='http://localhost:3000/' prove -r -l lib/ t/
101 # Tests with inline apps need to use Catalyst::Engine::Test
107 my ( $self, $c ) = @_;
108 $c->res->output('bar');
111 __PACKAGE__->setup();
115 use Catalyst::Test 'TestApp';
116 use Test::More tests => 1;
118 ok( get('/foo') =~ /bar/ );
121 use Catalyst::Test 'MyApp', { default_host => 'myapp.com' };
122 like( get('/whichhost'), qr/served by myapp.com/ );
123 like( get( '/whichhost', { host => 'yourapp.com' } ), qr/served by yourapp.com/ );
125 local $Catalyst::Test::default_host = 'otherapp.com';
126 like( get('/whichhost'), qr/served by otherapp.com/ );
131 This module allows you to make requests to a Catalyst application either without
132 a server, by simulating the environment of an HTTP request using
133 L<HTTP::Request::AsCGI> or remotely if you define the CATALYST_SERVER
134 environment variable. This module also adds a few catalyst
135 specific testing methods as displayed in the method section.
137 The </get> and </request> functions take either a URI or an L<HTTP::Request>
146 my $content = get('foo/bar?test=1');
148 Note that this method doesn't follow redirects, so to test for a
149 correctly redirecting page you'll need to use a combination of this
150 method and the L<request> method below:
152 my $res = request('/'); # redirects to /y
153 warn $res->header('location');
155 my $uri = URI->new($res->header('location'));
156 is ( $uri->path , '/y');
157 my $content = get($uri->path);
161 Returns a C<HTTP::Response> object. Accepts an optional hashref for request
162 header configuration; currently only supports setting 'host' value.
164 my $res = request('foo/bar?test=1');
165 my $virtual_res = request('foo/bar?test=1', {host => 'virtualhost.com'});
169 Simulate a request using L<HTTP::Request::AsCGI>.
176 require HTTP::Request::AsCGI;
178 my $request = Catalyst::Utils::request( shift(@_) );
179 _customize_request($request, @_);
180 my $cgi = HTTP::Request::AsCGI->new( $request, %ENV )->setup;
182 $class->handle_request;
184 return $cgi->restore->response;
189 =head2 remote_request
191 Do an actual remote request using LWP.
197 require LWP::UserAgent;
199 my $request = Catalyst::Utils::request( shift(@_) );
200 my $server = URI->new( $ENV{CATALYST_SERVER} );
202 _customize_request($request, @_);
204 if ( $server->path =~ m|^(.+)?/$| ) {
206 $server->path("$path") if $path; # need to be quoted
209 # the request path needs to be sanitised if $server is using a
210 # non-root path due to potential overlap between request path and
213 # If request path is '/', we have to add a trailing slash to the
215 my $add_trailing = $request->uri->path eq '/';
217 my @sp = split '/', $server->path;
218 my @rp = split '/', $request->uri->path;
219 shift @sp;shift @rp; # leading /
221 foreach my $sp (@sp) {
222 $sp eq $rp[0] ? shift @rp : last
225 $request->uri->path(join '/', @rp);
227 if ( $add_trailing ) {
228 $request->uri->path( $request->uri->path . '/' );
232 $request->uri->scheme( $server->scheme );
233 $request->uri->host( $server->host );
234 $request->uri->port( $server->port );
235 $request->uri->path( $server->path . $request->uri->path );
239 $agent = LWP::UserAgent->new(
244 # work around newer LWP max_redirect 0 bug
245 # http://rt.cpan.org/Ticket/Display.html?id=40260
246 requests_redirectable => [],
252 return $agent->request($request);
255 sub _customize_request {
257 my $opts = pop(@_) || {};
258 if ( my $host = exists $opts->{host} ? $opts->{host} : $default_host ) {
259 $request->header( 'Host' => $host );
265 Fetches the given url and check that the request was successful
267 =head2 action_redirect
269 Fetches the given url and check that the request was a redirect
271 =head2 action_notfound
273 Fetches the given url and check that the request was not found
277 Fetches the given url and matches the content against it.
279 =head2 contenttype_is
281 Check for given mime type
285 L<Catalyst>, L<Test::WWW::Mechanize::Catalyst>,
286 L<Test::WWW::Selenium::Catalyst>, L<Test::More>, L<HTTP::Request::Common>
290 Catalyst Contributors, see Catalyst.pm
294 This program is free software, you can redistribute it and/or modify it under
295 the same terms as Perl itself.