Commit | Line | Data |
fc7ec1d9 |
1 | package Catalyst::Test; |
2 | |
d9d04ded |
3 | use strict; |
4 | use warnings; |
b474372a |
5 | use Test::More (); |
e8d0f69a |
6 | |
5203d720 |
7 | use Plack::Test; |
a2f2cde9 |
8 | use Catalyst::Exception; |
d837e1a7 |
9 | use Catalyst::Utils; |
7dd4f037 |
10 | use Class::MOP; |
87cbe5e6 |
11 | use Sub::Exporter; |
5203d720 |
12 | use Carp; |
87cbe5e6 |
13 | |
e0a78010 |
14 | my $build_exports = sub { |
87cbe5e6 |
15 | my ($self, $meth, $args, $defaults) = @_; |
e8d0f69a |
16 | |
87cbe5e6 |
17 | my $request; |
18 | my $class = $args->{class}; |
19 | |
20 | if ( $ENV{CATALYST_SERVER} ) { |
21 | $request = sub { remote_request(@_) }; |
f4b9686b |
22 | } elsif (!$class) { |
23 | $request = sub { croak "Must specify a test app: use Catalyst::Test 'TestApp'"; } |
87cbe5e6 |
24 | } else { |
7dd4f037 |
25 | unless (Class::MOP::is_class_loaded($class)) { |
26 | Class::MOP::load_class($class); |
87cbe5e6 |
27 | } |
28 | $class->import; |
29 | |
22a5833d |
30 | my $app = $class->psgi_app; |
5203d720 |
31 | |
32 | $request = sub { local_request( $app, @_ ) }; |
87cbe5e6 |
33 | } |
34 | |
35 | my $get = sub { $request->(@_)->content }; |
36 | |
4fbc0e85 |
37 | my $ctx_request = sub { |
702729f5 |
38 | my $me = ref $self || $self; |
269194b4 |
39 | |
4fbc0e85 |
40 | ### throw an exception if ctx_request is being used against a remote |
269194b4 |
41 | ### server |
42 | Catalyst::Exception->throw("$me only works with local requests, not remote") |
43 | if $ENV{CATALYST_SERVER}; |
44 | |
ba151d0d |
45 | ### check explicitly for the class here, or the Cat->meta call will blow |
46 | ### up in our face |
47 | Catalyst::Exception->throw("Must specify a test app: use Catalyst::Test 'TestApp'") unless $class; |
48 | |
269194b4 |
49 | ### place holder for $c after the request finishes; reset every time |
50 | ### requests are done. |
51 | my $c; |
52 | |
53 | ### hook into 'dispatch' -- the function gets called after all plugins |
54 | ### have done their work, and it's an easy place to capture $c. |
269194b4 |
55 | |
cf1fb734 |
56 | my $meta = Class::MOP::get_metaclass_by_name($class); |
ba151d0d |
57 | $meta->make_mutable; |
58 | $meta->add_after_method_modifier( "dispatch", sub { |
59 | $c = shift; |
60 | }); |
cf1fb734 |
61 | $meta->make_immutable( replace_constructor => 1 ); |
94f74acd |
62 | Class::C3::reinitialize(); # Fixes RT#46459, I've failed to write a test for how/why, but it does. |
269194b4 |
63 | ### do the request; C::T::request will know about the class name, and |
64 | ### we've already stopped it from doing remote requests above. |
65 | my $res = $request->( @_ ); |
66 | |
67 | ### return both values |
68 | return ( $res, $c ); |
69 | }; |
70 | |
87cbe5e6 |
71 | return { |
4fbc0e85 |
72 | request => $request, |
73 | get => $get, |
74 | ctx_request => $ctx_request, |
87cbe5e6 |
75 | content_like => sub { |
76 | my $action = shift; |
77 | return Test::More->builder->like($get->($action),@_); |
78 | }, |
79 | action_ok => sub { |
80 | my $action = shift; |
81 | return Test::More->builder->ok($request->($action)->is_success, @_); |
82 | }, |
83 | action_redirect => sub { |
84 | my $action = shift; |
85 | return Test::More->builder->ok($request->($action)->is_redirect,@_); |
86 | }, |
87 | action_notfound => sub { |
88 | my $action = shift; |
89 | return Test::More->builder->is_eq($request->($action)->code,404,@_); |
90 | }, |
91 | contenttype_is => sub { |
92 | my $action = shift; |
93 | my $res = $request->($action); |
94 | return Test::More->builder->is_eq(scalar($res->content_type),@_); |
95 | }, |
96 | }; |
e0a78010 |
97 | }; |
e8d0f69a |
98 | |
d9d04ded |
99 | our $default_host; |
6e6df63d |
100 | |
101 | { |
102 | my $import = Sub::Exporter::build_exporter({ |
e0a78010 |
103 | groups => [ all => $build_exports ], |
6e6df63d |
104 | into_level => 1, |
105 | }); |
106 | |
d9d04ded |
107 | |
6e6df63d |
108 | sub import { |
d9d04ded |
109 | my ($self, $class, $opts) = @_; |
955d6da6 |
110 | Carp::carp( |
111 | qq{Importing Catalyst::Test without an application name is deprecated:\n |
112 | Instead of saying: use Catalyst::Test; |
113 | say: use Catalyst::Test (); # If you don't want to import a test app right now. |
114 | or say: use Catalyst::Test 'MyApp'; # If you do want to import a test app.\n\n}) |
115 | unless $class; |
6e6df63d |
116 | $import->($self, '-all' => { class => $class }); |
d258fcb2 |
117 | $opts = {} unless ref $opts eq 'HASH'; |
d9d04ded |
118 | $default_host = $opts->{default_host} if exists $opts->{default_host}; |
269194b4 |
119 | return 1; |
6e6df63d |
120 | } |
121 | } |
122 | |
fc7ec1d9 |
123 | =head1 NAME |
124 | |
8d2fa70c |
125 | Catalyst::Test - Test Catalyst Applications |
fc7ec1d9 |
126 | |
127 | =head1 SYNOPSIS |
128 | |
49faa307 |
129 | # Helper |
49faa307 |
130 | script/test.pl |
131 | |
fc7ec1d9 |
132 | # Tests |
133 | use Catalyst::Test 'TestApp'; |
26dd6d9f |
134 | my $content = get('index.html'); # Content as string |
135 | my $response = request('index.html'); # HTTP::Response object |
4fbc0e85 |
136 | my($res, $c) = ctx_request('index.html'); # HTTP::Response & context object |
fc7ec1d9 |
137 | |
2f381252 |
138 | use HTTP::Request::Common; |
139 | my $response = request POST '/foo', [ |
140 | bar => 'baz', |
141 | something => 'else' |
142 | ]; |
143 | |
45374ac6 |
144 | # Run tests against a remote server |
21465c88 |
145 | CATALYST_SERVER='http://localhost:3000/' prove -r -l lib/ t/ |
45374ac6 |
146 | |
b6898a9f |
147 | use Catalyst::Test 'TestApp'; |
e8d0f69a |
148 | use Test::More tests => 1; |
b6898a9f |
149 | |
150 | ok( get('/foo') =~ /bar/ ); |
151 | |
d9d04ded |
152 | # mock virtual hosts |
153 | use Catalyst::Test 'MyApp', { default_host => 'myapp.com' }; |
154 | like( get('/whichhost'), qr/served by myapp.com/ ); |
155 | like( get( '/whichhost', { host => 'yourapp.com' } ), qr/served by yourapp.com/ ); |
156 | { |
157 | local $Catalyst::Test::default_host = 'otherapp.com'; |
158 | like( get('/whichhost'), qr/served by otherapp.com/ ); |
159 | } |
160 | |
fc7ec1d9 |
161 | =head1 DESCRIPTION |
162 | |
2f381252 |
163 | This module allows you to make requests to a Catalyst application either without |
164 | a server, by simulating the environment of an HTTP request using |
165 | L<HTTP::Request::AsCGI> or remotely if you define the CATALYST_SERVER |
0eb98177 |
166 | environment variable. This module also adds a few Catalyst-specific |
167 | testing methods as displayed in the method section. |
2f381252 |
168 | |
f98f669b |
169 | The L<get|/"$content = get( ... )"> and L<request|/"$res = request( ... );"> |
170 | functions take either a URI or an L<HTTP::Request> object. |
fc7ec1d9 |
171 | |
5f2e949d |
172 | =head1 INLINE TESTS WILL NO LONGER WORK |
173 | |
174 | While it used to be possible to inline a whole testapp into a C<.t> file for a |
175 | distribution, this will no longer work. |
176 | |
177 | The convention is to place your L<Catalyst> test apps into C<t/lib> in your |
178 | distribution. E.g.: C<t/lib/TestApp.pm>, C<t/lib/TestApp/Controller/Root.pm>, |
179 | etc.. Multiple test apps can be used in this way. |
180 | |
181 | Then write your C<.t> files like so: |
182 | |
183 | use strict; |
184 | use warnings; |
185 | use FindBin '$Bin'; |
186 | use lib "$Bin/lib"; |
187 | use Test::More tests => 6; |
188 | use Catalyst::Test 'TestApp'; |
189 | |
03f7a71b |
190 | =head1 METHODS |
fc7ec1d9 |
191 | |
26dd6d9f |
192 | =head2 $content = get( ... ) |
fc7ec1d9 |
193 | |
194 | Returns the content. |
195 | |
196 | my $content = get('foo/bar?test=1'); |
197 | |
f13fc03f |
198 | Note that this method doesn't follow redirects, so to test for a |
199 | correctly redirecting page you'll need to use a combination of this |
f98f669b |
200 | method and the L<request|/"$res = request( ... );"> method below: |
f13fc03f |
201 | |
202 | my $res = request('/'); # redirects to /y |
203 | warn $res->header('location'); |
204 | use URI; |
205 | my $uri = URI->new($res->header('location')); |
206 | is ( $uri->path , '/y'); |
207 | my $content = get($uri->path); |
208 | |
8fa9321c |
209 | Note also that the content is returned as raw bytes, without any attempt |
210 | to decode it into characters. |
211 | |
26dd6d9f |
212 | =head2 $res = request( ... ); |
fc7ec1d9 |
213 | |
0eb98177 |
214 | Returns an L<HTTP::Response> object. Accepts an optional hashref for request |
d9d04ded |
215 | header configuration; currently only supports setting 'host' value. |
fc7ec1d9 |
216 | |
795117cf |
217 | my $res = request('foo/bar?test=1'); |
d9d04ded |
218 | my $virtual_res = request('foo/bar?test=1', {host => 'virtualhost.com'}); |
fc7ec1d9 |
219 | |
26dd6d9f |
220 | =head1 FUNCTIONS |
221 | |
f2e13bbd |
222 | =head2 ($res, $c) = ctx_request( ... ); |
26dd6d9f |
223 | |
f98f669b |
224 | Works exactly like L<request|/"$res = request( ... );">, except it also returns the Catalyst context object, |
51a75afc |
225 | C<$c>. Note that this only works for local requests. |
26dd6d9f |
226 | |
26dd6d9f |
227 | =head2 $res = Catalyst::Test::local_request( $AppClass, $url ); |
0f895006 |
228 | |
2f381252 |
229 | Simulate a request using L<HTTP::Request::AsCGI>. |
230 | |
0f895006 |
231 | =cut |
232 | |
233 | sub local_request { |
5203d720 |
234 | my $app = shift; |
0f895006 |
235 | |
5203d720 |
236 | my $request = Catalyst::Utils::request(shift); |
65791fc5 |
237 | my %extra_env; |
238 | _customize_request($request, \%extra_env, @_); |
5203d720 |
239 | |
240 | my $ret; |
65791fc5 |
241 | test_psgi |
d87ef823 |
242 | app => sub { $app->({ %{ $_[0] }, %extra_env }) }, |
243 | client => sub { $ret = shift->($request) }; |
5203d720 |
244 | |
245 | return $ret; |
0f895006 |
246 | } |
247 | |
523d44ec |
248 | my $agent; |
249 | |
26dd6d9f |
250 | =head2 $res = Catalyst::Test::remote_request( $url ); |
bea4160a |
251 | |
b77e7869 |
252 | Do an actual remote request using LWP. |
bea4160a |
253 | |
254 | =cut |
255 | |
45374ac6 |
256 | sub remote_request { |
45374ac6 |
257 | |
68eb5874 |
258 | require LWP::UserAgent; |
259 | |
d837e1a7 |
260 | my $request = Catalyst::Utils::request( shift(@_) ); |
0f895006 |
261 | my $server = URI->new( $ENV{CATALYST_SERVER} ); |
523d44ec |
262 | |
d9d04ded |
263 | _customize_request($request, @_); |
264 | |
523d44ec |
265 | if ( $server->path =~ m|^(.+)?/$| ) { |
890e8d18 |
266 | my $path = $1; |
267 | $server->path("$path") if $path; # need to be quoted |
f4c0f6f7 |
268 | } |
cdae055a |
269 | |
270 | # the request path needs to be sanitised if $server is using a |
271 | # non-root path due to potential overlap between request path and |
272 | # response path. |
273 | if ($server->path) { |
f4c0f6f7 |
274 | # If request path is '/', we have to add a trailing slash to the |
275 | # final request URI |
276 | my $add_trailing = $request->uri->path eq '/'; |
0eb98177 |
277 | |
cdae055a |
278 | my @sp = split '/', $server->path; |
279 | my @rp = split '/', $request->uri->path; |
280 | shift @sp;shift @rp; # leading / |
281 | if (@rp) { |
282 | foreach my $sp (@sp) { |
a7daf37e |
283 | $sp eq $rp[0] ? shift @rp : last |
cdae055a |
284 | } |
285 | } |
286 | $request->uri->path(join '/', @rp); |
0eb98177 |
287 | |
f4c0f6f7 |
288 | if ( $add_trailing ) { |
289 | $request->uri->path( $request->uri->path . '/' ); |
290 | } |
523d44ec |
291 | } |
292 | |
293 | $request->uri->scheme( $server->scheme ); |
294 | $request->uri->host( $server->host ); |
295 | $request->uri->port( $server->port ); |
296 | $request->uri->path( $server->path . $request->uri->path ); |
297 | |
68eb5874 |
298 | unless ($agent) { |
9ffadf88 |
299 | |
d837e1a7 |
300 | $agent = LWP::UserAgent->new( |
523d44ec |
301 | keep_alive => 1, |
302 | max_redirect => 0, |
303 | timeout => 60, |
0eb98177 |
304 | |
d11e0c1d |
305 | # work around newer LWP max_redirect 0 bug |
306 | # http://rt.cpan.org/Ticket/Display.html?id=40260 |
307 | requests_redirectable => [], |
523d44ec |
308 | ); |
d837e1a7 |
309 | |
523d44ec |
310 | $agent->env_proxy; |
311 | } |
45374ac6 |
312 | |
313 | return $agent->request($request); |
fc7ec1d9 |
314 | } |
315 | |
d9d04ded |
316 | sub _customize_request { |
317 | my $request = shift; |
65791fc5 |
318 | my $extra_env = shift; |
d9d04ded |
319 | my $opts = pop(@_) || {}; |
4348c28b |
320 | $opts = {} unless ref($opts) eq 'HASH'; |
d9d04ded |
321 | if ( my $host = exists $opts->{host} ? $opts->{host} : $default_host ) { |
322 | $request->header( 'Host' => $host ); |
323 | } |
65791fc5 |
324 | |
325 | if (my $extra = $opts->{extra_env}) { |
326 | @{ $extra_env }{keys %{ $extra }} = values %{ $extra }; |
327 | } |
d9d04ded |
328 | } |
329 | |
e8d0f69a |
330 | =head2 action_ok |
331 | |
0eb98177 |
332 | Fetches the given URL and checks that the request was successful. |
e8d0f69a |
333 | |
334 | =head2 action_redirect |
335 | |
0eb98177 |
336 | Fetches the given URL and checks that the request was a redirect. |
e8d0f69a |
337 | |
338 | =head2 action_notfound |
339 | |
0eb98177 |
340 | Fetches the given URL and checks that the request was not found. |
341 | |
342 | =head2 content_like( $url, $regexp [, $test_name] ) |
e8d0f69a |
343 | |
0eb98177 |
344 | Fetches the given URL and returns whether the content matches the regexp. |
e8d0f69a |
345 | |
0eb98177 |
346 | =head2 contenttype_is |
e8d0f69a |
347 | |
0eb98177 |
348 | Check for given MIME type. |
e8d0f69a |
349 | |
fc7ec1d9 |
350 | =head1 SEE ALSO |
351 | |
2f381252 |
352 | L<Catalyst>, L<Test::WWW::Mechanize::Catalyst>, |
353 | L<Test::WWW::Selenium::Catalyst>, L<Test::More>, L<HTTP::Request::Common> |
fc7ec1d9 |
354 | |
2f381252 |
355 | =head1 AUTHORS |
fc7ec1d9 |
356 | |
2f381252 |
357 | Catalyst Contributors, see Catalyst.pm |
fc7ec1d9 |
358 | |
359 | =head1 COPYRIGHT |
360 | |
536bee89 |
361 | This library is free software. You can redistribute it and/or modify it under |
fc7ec1d9 |
362 | the same terms as Perl itself. |
363 | |
364 | =cut |
365 | |
366 | 1; |