Commit | Line | Data |
fc7ec1d9 |
1 | package Catalyst::Test; |
2 | |
3 | use strict; |
b39840da |
4 | use warnings; |
d837e1a7 |
5 | |
a2f2cde9 |
6 | use Catalyst::Exception; |
d837e1a7 |
7 | use Catalyst::Utils; |
16d306fa |
8 | use Class::Inspector; |
0f895006 |
9 | |
fc7ec1d9 |
10 | =head1 NAME |
11 | |
8d2fa70c |
12 | Catalyst::Test - Test Catalyst Applications |
fc7ec1d9 |
13 | |
14 | =head1 SYNOPSIS |
15 | |
49faa307 |
16 | # Helper |
49faa307 |
17 | script/test.pl |
18 | |
fc7ec1d9 |
19 | # Tests |
20 | use Catalyst::Test 'TestApp'; |
21 | request('index.html'); |
22 | get('index.html'); |
23 | |
7ef58180 |
24 | use HTTP::Request::Common; |
25 | my $response = request POST '/foo', [ |
26 | bar => 'baz', |
27 | something => 'else' |
28 | ]; |
29 | |
45374ac6 |
30 | # Run tests against a remote server |
21465c88 |
31 | CATALYST_SERVER='http://localhost:3000/' prove -r -l lib/ t/ |
45374ac6 |
32 | |
b6898a9f |
33 | # Tests with inline apps need to use Catalyst::Engine::Test |
34 | package TestApp; |
35 | |
8d2fa70c |
36 | use Catalyst; |
b6898a9f |
37 | |
c46c32fa |
38 | sub foo : Global { |
b6898a9f |
39 | my ( $self, $c ) = @_; |
40 | $c->res->output('bar'); |
c46c32fa |
41 | } |
42 | |
43 | __PACKAGE__->setup(); |
b6898a9f |
44 | |
45 | package main; |
46 | |
47 | use Test::More tests => 1; |
48 | use Catalyst::Test 'TestApp'; |
49 | |
50 | ok( get('/foo') =~ /bar/ ); |
51 | |
fc7ec1d9 |
52 | =head1 DESCRIPTION |
53 | |
7ef58180 |
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. |
fc7ec1d9 |
61 | |
62 | =head2 METHODS |
63 | |
b5ecfcf0 |
64 | =head2 get |
fc7ec1d9 |
65 | |
66 | Returns the content. |
67 | |
68 | my $content = get('foo/bar?test=1'); |
69 | |
f13fc03f |
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 | |
b5ecfcf0 |
81 | =head2 request |
fc7ec1d9 |
82 | |
83 | Returns a C<HTTP::Response> object. |
84 | |
795117cf |
85 | my $res = request('foo/bar?test=1'); |
fc7ec1d9 |
86 | |
87 | =cut |
88 | |
fc7ec1d9 |
89 | sub import { |
66d9e175 |
90 | my $self = shift; |
45374ac6 |
91 | my $class = shift; |
92 | |
93 | my ( $get, $request ); |
94 | |
d96e14c2 |
95 | if ( $ENV{CATALYST_SERVER} ) { |
45374ac6 |
96 | $request = sub { remote_request(@_) }; |
97 | $get = sub { remote_request(@_)->content }; |
fb02aed1 |
98 | } elsif (! $class) { |
99 | $request = sub { Catalyst::Exception->throw("Must specify a test app: use Catalyst::Test 'TestApp'") }; |
100 | $get = $request; |
101 | } else { |
16d306fa |
102 | unless( Class::Inspector->loaded( $class ) ) { |
1e514a51 |
103 | require Class::Inspector->filename( $class ); |
af81c980 |
104 | } |
d96e14c2 |
105 | $class->import; |
106 | |
0f895006 |
107 | $request = sub { local_request( $class, @_ ) }; |
108 | $get = sub { local_request( $class, @_ )->content }; |
49faa307 |
109 | } |
45374ac6 |
110 | |
111 | no strict 'refs'; |
112 | my $caller = caller(0); |
113 | *{"$caller\::request"} = $request; |
114 | *{"$caller\::get"} = $get; |
115 | } |
116 | |
b5ecfcf0 |
117 | =head2 local_request |
0f895006 |
118 | |
7ef58180 |
119 | Simulate a request using L<HTTP::Request::AsCGI>. |
120 | |
0f895006 |
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 | |
523d44ec |
136 | my $agent; |
137 | |
b5ecfcf0 |
138 | =head2 remote_request |
bea4160a |
139 | |
b77e7869 |
140 | Do an actual remote request using LWP. |
bea4160a |
141 | |
142 | =cut |
143 | |
45374ac6 |
144 | sub remote_request { |
45374ac6 |
145 | |
68eb5874 |
146 | require LWP::UserAgent; |
147 | |
d837e1a7 |
148 | my $request = Catalyst::Utils::request( shift(@_) ); |
0f895006 |
149 | my $server = URI->new( $ENV{CATALYST_SERVER} ); |
523d44ec |
150 | |
151 | if ( $server->path =~ m|^(.+)?/$| ) { |
890e8d18 |
152 | my $path = $1; |
153 | $server->path("$path") if $path; # need to be quoted |
f4c0f6f7 |
154 | } |
cdae055a |
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) { |
f4c0f6f7 |
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 | |
cdae055a |
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) { |
a7daf37e |
169 | $sp eq $rp[0] ? shift @rp : last |
cdae055a |
170 | } |
171 | } |
172 | $request->uri->path(join '/', @rp); |
f4c0f6f7 |
173 | |
174 | if ( $add_trailing ) { |
175 | $request->uri->path( $request->uri->path . '/' ); |
176 | } |
523d44ec |
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 | |
68eb5874 |
184 | unless ($agent) { |
9ffadf88 |
185 | |
d837e1a7 |
186 | $agent = LWP::UserAgent->new( |
523d44ec |
187 | keep_alive => 1, |
188 | max_redirect => 0, |
189 | timeout => 60, |
451553f5 |
190 | |
191 | # work around newer LWP max_redirect 0 bug |
192 | # http://rt.cpan.org/Ticket/Display.html?id=40260 |
193 | requests_redirectable => [], |
523d44ec |
194 | ); |
d837e1a7 |
195 | |
523d44ec |
196 | $agent->env_proxy; |
197 | } |
45374ac6 |
198 | |
199 | return $agent->request($request); |
fc7ec1d9 |
200 | } |
201 | |
fc7ec1d9 |
202 | =head1 SEE ALSO |
203 | |
7ef58180 |
204 | L<Catalyst>, L<Test::WWW::Mechanize::Catalyst>, |
205 | L<Test::WWW::Selenium::Catalyst>, L<Test::More>, L<HTTP::Request::Common> |
fc7ec1d9 |
206 | |
0bf7ab71 |
207 | =head1 AUTHORS |
fc7ec1d9 |
208 | |
0bf7ab71 |
209 | Catalyst Contributors, see Catalyst.pm |
fc7ec1d9 |
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; |