Updated catalyst.pl
[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;
fc7ec1d9 8use UNIVERSAL::require;
0f895006 9
a1102dfa 10$ENV{CATALYST_ENGINE} ||= 'CGI';
b39840da 11
fc7ec1d9 12=head1 NAME
13
8d2fa70c 14Catalyst::Test - Test Catalyst Applications
fc7ec1d9 15
16=head1 SYNOPSIS
17
49faa307 18 # Helper
49faa307 19 script/test.pl
20
fc7ec1d9 21 # Tests
22 use Catalyst::Test 'TestApp';
23 request('index.html');
24 get('index.html');
25
45374ac6 26 # Run tests against a remote server
21465c88 27 CATALYST_SERVER='http://localhost:3000/' prove -r -l lib/ t/
45374ac6 28
b6898a9f 29 # Tests with inline apps need to use Catalyst::Engine::Test
30 package TestApp;
31
8d2fa70c 32 use Catalyst;
b6898a9f 33
c46c32fa 34 sub foo : Global {
b6898a9f 35 my ( $self, $c ) = @_;
36 $c->res->output('bar');
c46c32fa 37 }
38
39 __PACKAGE__->setup();
b6898a9f 40
41 package main;
42
43 use Test::More tests => 1;
44 use Catalyst::Test 'TestApp';
45
46 ok( get('/foo') =~ /bar/ );
47
fc7ec1d9 48=head1 DESCRIPTION
49
8d2fa70c 50Test Catalyst Applications.
fc7ec1d9 51
52=head2 METHODS
53
bea4160a 54=over 4
55
56=item get
fc7ec1d9 57
58Returns the content.
59
60 my $content = get('foo/bar?test=1');
61
bea4160a 62=item request
fc7ec1d9 63
64Returns a C<HTTP::Response> object.
65
795117cf 66 my $res = request('foo/bar?test=1');
fc7ec1d9 67
68=cut
69
fc7ec1d9 70sub import {
66d9e175 71 my $self = shift;
45374ac6 72 my $class = shift;
73
74 my ( $get, $request );
75
d96e14c2 76 if ( $ENV{CATALYST_SERVER} ) {
45374ac6 77 $request = sub { remote_request(@_) };
78 $get = sub { remote_request(@_)->content };
79 }
80
81 else {
bc024080 82 $class->require;
795117cf 83 die if $@ && $@ !~ /^Can't locate /;
d96e14c2 84 $class->import;
85
0f895006 86 $request = sub { local_request( $class, @_ ) };
87 $get = sub { local_request( $class, @_ )->content };
49faa307 88 }
45374ac6 89
90 no strict 'refs';
91 my $caller = caller(0);
92 *{"$caller\::request"} = $request;
93 *{"$caller\::get"} = $get;
94}
95
0f895006 96=item local_request
97
98=cut
99
100sub local_request {
101 my $class = shift;
102
103 require HTTP::Request::AsCGI;
104
105 my $request = Catalyst::Utils::request( shift(@_) );
106 my $cgi = HTTP::Request::AsCGI->new( $request, %ENV )->setup;
107
108 $class->handle_request;
109
110 return $cgi->restore->response;
111}
112
523d44ec 113my $agent;
114
bea4160a 115=item remote_request
116
b77e7869 117Do an actual remote request using LWP.
bea4160a 118
119=cut
120
45374ac6 121sub remote_request {
45374ac6 122
68eb5874 123 require LWP::UserAgent;
124
d837e1a7 125 my $request = Catalyst::Utils::request( shift(@_) );
0f895006 126 my $server = URI->new( $ENV{CATALYST_SERVER} );
523d44ec 127
128 if ( $server->path =~ m|^(.+)?/$| ) {
9ffadf88 129 $server->path("$1"); # need to be quoted
523d44ec 130 }
131
132 $request->uri->scheme( $server->scheme );
133 $request->uri->host( $server->host );
134 $request->uri->port( $server->port );
135 $request->uri->path( $server->path . $request->uri->path );
136
68eb5874 137 unless ($agent) {
9ffadf88 138
d837e1a7 139 $agent = LWP::UserAgent->new(
523d44ec 140 keep_alive => 1,
141 max_redirect => 0,
142 timeout => 60,
143 );
d837e1a7 144
523d44ec 145 $agent->env_proxy;
146 }
45374ac6 147
148 return $agent->request($request);
fc7ec1d9 149}
150
bea4160a 151=back
152
fc7ec1d9 153=head1 SEE ALSO
154
155L<Catalyst>.
156
157=head1 AUTHOR
158
159Sebastian Riedel, C<sri@cpan.org>
160
161=head1 COPYRIGHT
162
163This program is free software, you can redistribute it and/or modify it under
164the same terms as Perl itself.
165
166=cut
167
1681;