authors cleanup
[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
45374ac6 24 # Run tests against a remote server
21465c88 25 CATALYST_SERVER='http://localhost:3000/' prove -r -l lib/ t/
45374ac6 26
b6898a9f 27 # Tests with inline apps need to use Catalyst::Engine::Test
28 package TestApp;
29
8d2fa70c 30 use Catalyst;
b6898a9f 31
c46c32fa 32 sub foo : Global {
b6898a9f 33 my ( $self, $c ) = @_;
34 $c->res->output('bar');
c46c32fa 35 }
36
37 __PACKAGE__->setup();
b6898a9f 38
39 package main;
40
41 use Test::More tests => 1;
42 use Catalyst::Test 'TestApp';
43
44 ok( get('/foo') =~ /bar/ );
45
fc7ec1d9 46=head1 DESCRIPTION
47
8d2fa70c 48Test Catalyst Applications.
fc7ec1d9 49
50=head2 METHODS
51
b5ecfcf0 52=head2 get
fc7ec1d9 53
54Returns the content.
55
56 my $content = get('foo/bar?test=1');
57
f13fc03f 58Note that this method doesn't follow redirects, so to test for a
59correctly redirecting page you'll need to use a combination of this
60method and the L<request> method below:
61
62 my $res = request('/'); # redirects to /y
63 warn $res->header('location');
64 use URI;
65 my $uri = URI->new($res->header('location'));
66 is ( $uri->path , '/y');
67 my $content = get($uri->path);
68
b5ecfcf0 69=head2 request
fc7ec1d9 70
71Returns a C<HTTP::Response> object.
72
795117cf 73 my $res = request('foo/bar?test=1');
fc7ec1d9 74
75=cut
76
fc7ec1d9 77sub import {
66d9e175 78 my $self = shift;
45374ac6 79 my $class = shift;
80
81 my ( $get, $request );
82
d96e14c2 83 if ( $ENV{CATALYST_SERVER} ) {
45374ac6 84 $request = sub { remote_request(@_) };
85 $get = sub { remote_request(@_)->content };
fb02aed1 86 } elsif (! $class) {
87 $request = sub { Catalyst::Exception->throw("Must specify a test app: use Catalyst::Test 'TestApp'") };
88 $get = $request;
89 } else {
16d306fa 90 unless( Class::Inspector->loaded( $class ) ) {
1e514a51 91 require Class::Inspector->filename( $class );
af81c980 92 }
d96e14c2 93 $class->import;
94
0f895006 95 $request = sub { local_request( $class, @_ ) };
96 $get = sub { local_request( $class, @_ )->content };
49faa307 97 }
45374ac6 98
99 no strict 'refs';
100 my $caller = caller(0);
101 *{"$caller\::request"} = $request;
102 *{"$caller\::get"} = $get;
103}
104
b5ecfcf0 105=head2 local_request
0f895006 106
107=cut
108
109sub local_request {
110 my $class = shift;
111
112 require HTTP::Request::AsCGI;
113
114 my $request = Catalyst::Utils::request( shift(@_) );
115 my $cgi = HTTP::Request::AsCGI->new( $request, %ENV )->setup;
116
117 $class->handle_request;
118
119 return $cgi->restore->response;
120}
121
523d44ec 122my $agent;
123
b5ecfcf0 124=head2 remote_request
bea4160a 125
b77e7869 126Do an actual remote request using LWP.
bea4160a 127
128=cut
129
45374ac6 130sub remote_request {
45374ac6 131
68eb5874 132 require LWP::UserAgent;
133
d837e1a7 134 my $request = Catalyst::Utils::request( shift(@_) );
0f895006 135 my $server = URI->new( $ENV{CATALYST_SERVER} );
523d44ec 136
137 if ( $server->path =~ m|^(.+)?/$| ) {
890e8d18 138 my $path = $1;
139 $server->path("$path") if $path; # need to be quoted
f4c0f6f7 140 }
cdae055a 141
142 # the request path needs to be sanitised if $server is using a
143 # non-root path due to potential overlap between request path and
144 # response path.
145 if ($server->path) {
f4c0f6f7 146 # If request path is '/', we have to add a trailing slash to the
147 # final request URI
148 my $add_trailing = $request->uri->path eq '/';
149
cdae055a 150 my @sp = split '/', $server->path;
151 my @rp = split '/', $request->uri->path;
152 shift @sp;shift @rp; # leading /
153 if (@rp) {
154 foreach my $sp (@sp) {
a7daf37e 155 $sp eq $rp[0] ? shift @rp : last
cdae055a 156 }
157 }
158 $request->uri->path(join '/', @rp);
f4c0f6f7 159
160 if ( $add_trailing ) {
161 $request->uri->path( $request->uri->path . '/' );
162 }
523d44ec 163 }
164
165 $request->uri->scheme( $server->scheme );
166 $request->uri->host( $server->host );
167 $request->uri->port( $server->port );
168 $request->uri->path( $server->path . $request->uri->path );
169
68eb5874 170 unless ($agent) {
9ffadf88 171
d837e1a7 172 $agent = LWP::UserAgent->new(
523d44ec 173 keep_alive => 1,
174 max_redirect => 0,
175 timeout => 60,
176 );
d837e1a7 177
523d44ec 178 $agent->env_proxy;
179 }
45374ac6 180
181 return $agent->request($request);
fc7ec1d9 182}
183
fc7ec1d9 184=head1 SEE ALSO
185
0bf7ab71 186L<Catalyst>
fc7ec1d9 187
0bf7ab71 188=head1 AUTHORS
fc7ec1d9 189
0bf7ab71 190Catalyst Contributors, see Catalyst.pm
fc7ec1d9 191
192=head1 COPYRIGHT
193
194This program is free software, you can redistribute it and/or modify it under
195the same terms as Perl itself.
196
197=cut
198
1991;