1 package Catalyst::Test;
7 use Catalyst::Exception;
12 my $build_exports = sub {
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),@_);
63 my $import = Sub::Exporter::build_exporter({
64 groups => [ all => $build_exports ],
70 my ($self, $class, $opts) = @_;
71 $import->($self, '-all' => { class => $class });
72 $opts = {} unless ref $opts eq 'HASH';
73 $default_host = $opts->{default_host} if exists $opts->{default_host};
79 Catalyst::Test - Test Catalyst Applications
87 use Catalyst::Test 'TestApp';
88 request('index.html');
91 use HTTP::Request::Common;
92 my $response = request POST '/foo', [
97 # Run tests against a remote server
98 CATALYST_SERVER='http://localhost:3000/' prove -r -l lib/ t/
100 # Tests with inline apps need to use Catalyst::Engine::Test
106 my ( $self, $c ) = @_;
107 $c->res->output('bar');
110 __PACKAGE__->setup();
114 use Catalyst::Test 'TestApp';
115 use Test::More tests => 1;
117 ok( get('/foo') =~ /bar/ );
120 use Catalyst::Test 'MyApp', { default_host => 'myapp.com' };
121 like( get('/whichhost'), qr/served by myapp.com/ );
122 like( get( '/whichhost', { host => 'yourapp.com' } ), qr/served by yourapp.com/ );
124 local $Catalyst::Test::default_host = 'otherapp.com';
125 like( get('/whichhost'), qr/served by otherapp.com/ );
130 This module allows you to make requests to a Catalyst application either without
131 a server, by simulating the environment of an HTTP request using
132 L<HTTP::Request::AsCGI> or remotely if you define the CATALYST_SERVER
133 environment variable. This module also adds a few catalyst
134 specific testing methods as displayed in the method section.
136 The </get> and </request> functions take either a URI or an L<HTTP::Request>
145 my $content = get('foo/bar?test=1');
147 Note that this method doesn't follow redirects, so to test for a
148 correctly redirecting page you'll need to use a combination of this
149 method and the L<request> method below:
151 my $res = request('/'); # redirects to /y
152 warn $res->header('location');
154 my $uri = URI->new($res->header('location'));
155 is ( $uri->path , '/y');
156 my $content = get($uri->path);
160 Returns a C<HTTP::Response> object. Accepts an optional hashref for request
161 header configuration; currently only supports setting 'host' value.
163 my $res = request('foo/bar?test=1');
164 my $virtual_res = request('foo/bar?test=1', {host => 'virtualhost.com'});
168 Simulate a request using L<HTTP::Request::AsCGI>.
175 require HTTP::Request::AsCGI;
177 my $request = Catalyst::Utils::request( shift(@_) );
178 _customize_request($request, @_);
179 my $cgi = HTTP::Request::AsCGI->new( $request, %ENV )->setup;
181 $class->handle_request;
183 return $cgi->restore->response;
188 =head2 remote_request
190 Do an actual remote request using LWP.
196 require LWP::UserAgent;
198 my $request = Catalyst::Utils::request( shift(@_) );
199 my $server = URI->new( $ENV{CATALYST_SERVER} );
201 _customize_request($request, @_);
203 if ( $server->path =~ m|^(.+)?/$| ) {
205 $server->path("$path") if $path; # need to be quoted
208 # the request path needs to be sanitised if $server is using a
209 # non-root path due to potential overlap between request path and
212 # If request path is '/', we have to add a trailing slash to the
214 my $add_trailing = $request->uri->path eq '/';
216 my @sp = split '/', $server->path;
217 my @rp = split '/', $request->uri->path;
218 shift @sp;shift @rp; # leading /
220 foreach my $sp (@sp) {
221 $sp eq $rp[0] ? shift @rp : last
224 $request->uri->path(join '/', @rp);
226 if ( $add_trailing ) {
227 $request->uri->path( $request->uri->path . '/' );
231 $request->uri->scheme( $server->scheme );
232 $request->uri->host( $server->host );
233 $request->uri->port( $server->port );
234 $request->uri->path( $server->path . $request->uri->path );
238 $agent = LWP::UserAgent->new(
243 # work around newer LWP max_redirect 0 bug
244 # http://rt.cpan.org/Ticket/Display.html?id=40260
245 requests_redirectable => [],
251 return $agent->request($request);
254 sub _customize_request {
256 my $opts = pop(@_) || {};
257 $opts = {} unless ref($opts) eq 'HASH';
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.