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