Workaround change in LWP that broke a cookie test (RT #40037)
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Test.pm
CommitLineData
fc7ec1d9 1package Catalyst::Test;
2
3use strict;
b39840da 4use warnings;
d837e1a7 5
a2f2cde9 6use Catalyst::Exception;
d837e1a7 7use Catalyst::Utils;
16d306fa 8use Class::Inspector;
0f895006 9
fc7ec1d9 10=head1 NAME
11
8d2fa70c 12Catalyst::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 54This module allows you to make requests to a Catalyst application either without
55a server, by simulating the environment of an HTTP request using
56L<HTTP::Request::AsCGI> or remotely if you define the CATALYST_SERVER
57environment variable.
58
59The </get> and </request> functions take either a URI or an L<HTTP::Request>
60object.
fc7ec1d9 61
62=head2 METHODS
63
b5ecfcf0 64=head2 get
fc7ec1d9 65
66Returns the content.
67
68 my $content = get('foo/bar?test=1');
69
f13fc03f 70Note that this method doesn't follow redirects, so to test for a
71correctly redirecting page you'll need to use a combination of this
72method 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
83Returns a C<HTTP::Response> object.
84
795117cf 85 my $res = request('foo/bar?test=1');
fc7ec1d9 86
87=cut
88
fc7ec1d9 89sub 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 119Simulate a request using L<HTTP::Request::AsCGI>.
120
0f895006 121=cut
122
123sub 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 136my $agent;
137
b5ecfcf0 138=head2 remote_request
bea4160a 139
b77e7869 140Do an actual remote request using LWP.
bea4160a 141
142=cut
143
45374ac6 144sub 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,
190 );
d837e1a7 191
523d44ec 192 $agent->env_proxy;
193 }
45374ac6 194
195 return $agent->request($request);
fc7ec1d9 196}
197
fc7ec1d9 198=head1 SEE ALSO
199
7ef58180 200L<Catalyst>, L<Test::WWW::Mechanize::Catalyst>,
201L<Test::WWW::Selenium::Catalyst>, L<Test::More>, L<HTTP::Request::Common>
fc7ec1d9 202
0bf7ab71 203=head1 AUTHORS
fc7ec1d9 204
0bf7ab71 205Catalyst Contributors, see Catalyst.pm
fc7ec1d9 206
207=head1 COPYRIGHT
208
209This program is free software, you can redistribute it and/or modify it under
210the same terms as Perl itself.
211
212=cut
213
2141;