Reformatted documentation
[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 =head2 get
55
56 Returns the content.
57
58     my $content = get('foo/bar?test=1');
59
60 =head2 request
61
62 Returns a C<HTTP::Response> object.
63
64     my $res = request('foo/bar?test=1');
65
66 =cut
67
68 sub import {
69     my $self  = shift;
70     my $class = shift;
71
72     my ( $get, $request );
73
74     if ( $ENV{CATALYST_SERVER} ) {
75         $request = sub { remote_request(@_) };
76         $get     = sub { remote_request(@_)->content };
77     }
78
79     else {
80         $class->require;
81         die if $@ && $@ !~ /^Can't locate /;
82         $class->import;
83
84         $request = sub { local_request( $class, @_ ) };
85         $get     = sub { local_request( $class, @_ )->content };
86     }
87
88     no strict 'refs';
89     my $caller = caller(0);
90     *{"$caller\::request"} = $request;
91     *{"$caller\::get"}     = $get;
92 }
93
94 =head2 local_request
95
96 =cut
97
98 sub local_request {
99     my $class = shift;
100
101     require HTTP::Request::AsCGI;
102
103     my $request = Catalyst::Utils::request( shift(@_) );
104     my $cgi     = HTTP::Request::AsCGI->new( $request, %ENV )->setup;
105
106     $class->handle_request;
107
108     return $cgi->restore->response;
109 }
110
111 my $agent;
112
113 =head2 remote_request
114
115 Do an actual remote request using LWP.
116
117 =cut
118
119 sub remote_request {
120
121     require LWP::UserAgent;
122
123     my $request = Catalyst::Utils::request( shift(@_) );
124     my $server  = URI->new( $ENV{CATALYST_SERVER} );
125
126     if ( $server->path =~ m|^(.+)?/$| ) {
127         $server->path("$1");    # need to be quoted
128     }
129
130     $request->uri->scheme( $server->scheme );
131     $request->uri->host( $server->host );
132     $request->uri->port( $server->port );
133     $request->uri->path( $server->path . $request->uri->path );
134
135     unless ($agent) {
136
137         $agent = LWP::UserAgent->new(
138             keep_alive   => 1,
139             max_redirect => 0,
140             timeout      => 60,
141         );
142
143         $agent->env_proxy;
144     }
145
146     return $agent->request($request);
147 }
148
149 =head1 SEE ALSO
150
151 L<Catalyst>.
152
153 =head1 AUTHOR
154
155 Sebastian Riedel, C<sri@cpan.org>
156
157 =head1 COPYRIGHT
158
159 This program is free software, you can redistribute it and/or modify it under
160 the same terms as Perl itself.
161
162 =cut
163
164 1;