1 package Catalyst::Test;
6 use Catalyst::Exception;
12 Catalyst::Test - Test Catalyst Applications
20 use Catalyst::Test 'TestApp';
21 request('index.html');
24 use HTTP::Request::Common;
25 my $response = request POST '/foo', [
30 # Run tests against a remote server
31 CATALYST_SERVER='http://localhost:3000/' prove -r -l lib/ t/
33 # Tests with inline apps need to use Catalyst::Engine::Test
39 my ( $self, $c ) = @_;
40 $c->res->output('bar');
47 use Test::More tests => 1;
48 use Catalyst::Test 'TestApp';
50 ok( get('/foo') =~ /bar/ );
54 This module allows you to make requests to a Catalyst application either without
55 a server, by simulating the environment of an HTTP request using
56 L<HTTP::Request::AsCGI> or remotely if you define the CATALYST_SERVER
59 The </get> and </request> functions take either a URI or an L<HTTP::Request>
68 my $content = get('foo/bar?test=1');
70 Note that this method doesn't follow redirects, so to test for a
71 correctly redirecting page you'll need to use a combination of this
72 method and the L<request> method below:
74 my $res = request('/'); # redirects to /y
75 warn $res->header('location');
77 my $uri = URI->new($res->header('location'));
78 is ( $uri->path , '/y');
79 my $content = get($uri->path);
83 Returns a C<HTTP::Response> object.
85 my $res = request('foo/bar?test=1');
93 my ( $get, $request );
95 if ( $ENV{CATALYST_SERVER} ) {
96 $request = sub { remote_request(@_) };
97 $get = sub { remote_request(@_)->content };
99 $request = sub { Catalyst::Exception->throw("Must specify a test app: use Catalyst::Test 'TestApp'") };
102 unless( Class::Inspector->loaded( $class ) ) {
103 require Class::Inspector->filename( $class );
107 $request = sub { local_request( $class, @_ ) };
108 $get = sub { local_request( $class, @_ )->content };
112 my $caller = caller(0);
113 *{"$caller\::request"} = $request;
114 *{"$caller\::get"} = $get;
119 Simulate a request using L<HTTP::Request::AsCGI>.
126 require HTTP::Request::AsCGI;
128 my $request = Catalyst::Utils::request( shift(@_) );
129 my $cgi = HTTP::Request::AsCGI->new( $request, %ENV )->setup;
131 $class->handle_request;
133 return $cgi->restore->response;
138 =head2 remote_request
140 Do an actual remote request using LWP.
146 require LWP::UserAgent;
148 my $request = Catalyst::Utils::request( shift(@_) );
149 my $server = URI->new( $ENV{CATALYST_SERVER} );
151 if ( $server->path =~ m|^(.+)?/$| ) {
153 $server->path("$path") if $path; # need to be quoted
156 # the request path needs to be sanitised if $server is using a
157 # non-root path due to potential overlap between request path and
160 # If request path is '/', we have to add a trailing slash to the
162 my $add_trailing = $request->uri->path eq '/';
164 my @sp = split '/', $server->path;
165 my @rp = split '/', $request->uri->path;
166 shift @sp;shift @rp; # leading /
168 foreach my $sp (@sp) {
169 $sp eq $rp[0] ? shift @rp : last
172 $request->uri->path(join '/', @rp);
174 if ( $add_trailing ) {
175 $request->uri->path( $request->uri->path . '/' );
179 $request->uri->scheme( $server->scheme );
180 $request->uri->host( $server->host );
181 $request->uri->port( $server->port );
182 $request->uri->path( $server->path . $request->uri->path );
186 $agent = LWP::UserAgent->new(
191 # work around newer LWP max_redirect 0 bug
192 # http://rt.cpan.org/Ticket/Display.html?id=40260
193 requests_redirectable => [],
199 return $agent->request($request);
204 L<Catalyst>, L<Test::WWW::Mechanize::Catalyst>,
205 L<Test::WWW::Selenium::Catalyst>, L<Test::More>, L<HTTP::Request::Common>
209 Catalyst Contributors, see Catalyst.pm
213 This program is free software, you can redistribute it and/or modify it under
214 the same terms as Perl itself.