Add test for looping DispatchType::Chained->list
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Test.pm
1 package Catalyst::Test;
2
3 use strict;
4 use warnings;
5
6 use Catalyst::Exception;
7 use Catalyst::Utils;
8 use Class::Inspector;
9
10 =head1 NAME
11
12 Catalyst::Test - Test Catalyst Applications
13
14 =head1 SYNOPSIS
15
16     # Helper
17     script/test.pl
18
19     # Tests
20     use Catalyst::Test 'TestApp';
21     request('index.html');
22     get('index.html');
23
24     use HTTP::Request::Common;
25     my $response = request POST '/foo', [
26         bar => 'baz',
27         something => 'else'
28     ];
29
30     # Run tests against a remote server
31     CATALYST_SERVER='http://localhost:3000/' prove -r -l lib/ t/
32
33     # Tests with inline apps need to use Catalyst::Engine::Test
34     package TestApp;
35
36     use Catalyst;
37
38     sub foo : Global {
39             my ( $self, $c ) = @_;
40             $c->res->output('bar');
41     }
42
43     __PACKAGE__->setup();
44
45     package main;
46
47     use Test::More tests => 1;
48     use Catalyst::Test 'TestApp';
49
50     ok( get('/foo') =~ /bar/ );
51
52 =head1 DESCRIPTION
53
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
57 environment variable.
58
59 The </get> and </request> functions take either a URI or an L<HTTP::Request>
60 object.
61
62 =head2 METHODS
63
64 =head2 get
65
66 Returns the content.
67
68     my $content = get('foo/bar?test=1');
69
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:
73
74     my $res = request('/'); # redirects to /y
75     warn $res->header('location');
76     use URI;
77     my $uri = URI->new($res->header('location'));
78     is ( $uri->path , '/y');
79     my $content = get($uri->path);
80
81 =head2 request
82
83 Returns a C<HTTP::Response> object.
84
85     my $res = request('foo/bar?test=1');
86
87 =cut
88
89 sub import {
90     my $self  = shift;
91     my $class = shift;
92
93     my ( $get, $request );
94
95     if ( $ENV{CATALYST_SERVER} ) {
96         $request = sub { remote_request(@_) };
97         $get     = sub { remote_request(@_)->content };
98     } elsif (! $class) {
99         $request = sub { Catalyst::Exception->throw("Must specify a test app: use Catalyst::Test 'TestApp'") };
100         $get     = $request;
101     } else {
102         unless( Class::Inspector->loaded( $class ) ) {
103             require Class::Inspector->filename( $class );
104         }
105         $class->import;
106
107         $request = sub { local_request( $class, @_ ) };
108         $get     = sub { local_request( $class, @_ )->content };
109     }
110
111     no strict 'refs';
112     my $caller = caller(0);
113     *{"$caller\::request"} = $request;
114     *{"$caller\::get"}     = $get;
115 }
116
117 =head2 local_request
118
119 Simulate a request using L<HTTP::Request::AsCGI>.
120
121 =cut
122
123 sub local_request {
124     my $class = shift;
125
126     require HTTP::Request::AsCGI;
127
128     my $request = Catalyst::Utils::request( shift(@_) );
129     my $cgi     = HTTP::Request::AsCGI->new( $request, %ENV )->setup;
130
131     $class->handle_request;
132
133     return $cgi->restore->response;
134 }
135
136 my $agent;
137
138 =head2 remote_request
139
140 Do an actual remote request using LWP.
141
142 =cut
143
144 sub remote_request {
145
146     require LWP::UserAgent;
147
148     my $request = Catalyst::Utils::request( shift(@_) );
149     my $server  = URI->new( $ENV{CATALYST_SERVER} );
150
151     if ( $server->path =~ m|^(.+)?/$| ) {
152         my $path = $1;
153         $server->path("$path") if $path;    # need to be quoted
154     }
155
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
158     # response path.
159     if ($server->path) {
160         # If request path is '/', we have to add a trailing slash to the
161         # final request URI
162         my $add_trailing = $request->uri->path eq '/';
163         
164         my @sp = split '/', $server->path;
165         my @rp = split '/', $request->uri->path;
166         shift @sp;shift @rp; # leading /
167         if (@rp) {
168             foreach my $sp (@sp) {
169                 $sp eq $rp[0] ? shift @rp : last
170             }
171         }
172         $request->uri->path(join '/', @rp);
173         
174         if ( $add_trailing ) {
175             $request->uri->path( $request->uri->path . '/' );
176         }
177     }
178
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 );
183
184     unless ($agent) {
185
186         $agent = LWP::UserAgent->new(
187             keep_alive   => 1,
188             max_redirect => 0,
189             timeout      => 60,
190             
191             # work around newer LWP max_redirect 0 bug
192             # http://rt.cpan.org/Ticket/Display.html?id=40260
193             requests_redirectable => [],
194         );
195
196         $agent->env_proxy;
197     }
198
199     return $agent->request($request);
200 }
201
202 =head1 SEE ALSO
203
204 L<Catalyst>, L<Test::WWW::Mechanize::Catalyst>,
205 L<Test::WWW::Selenium::Catalyst>, L<Test::More>, L<HTTP::Request::Common>
206
207 =head1 AUTHORS
208
209 Catalyst Contributors, see Catalyst.pm
210
211 =head1 COPYRIGHT
212
213 This program is free software, you can redistribute it and/or modify it under
214 the same terms as Perl itself.
215
216 =cut
217
218 1;