Applied patch from Ton Voon, using Catalyst::Test without App name
[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 Class::Inspector;
9
10 =head1 NAME
11
12 Catalyst::Test - Test Catalyst Applications
13
14 =head1 SYNOPSIS
15
16     # Helper
17     script/test.pl
18
19     # Tests
20     use Catalyst::Test 'TestApp';
21     request('index.html');
22     get('index.html');
23
24     # Run tests against a remote server
25     CATALYST_SERVER='http://localhost:3000/' prove -r -l lib/ t/
26
27     # Tests with inline apps need to use Catalyst::Engine::Test
28     package TestApp;
29
30     use Catalyst;
31
32     sub foo : Global {
33             my ( $self, $c ) = @_;
34             $c->res->output('bar');
35     }
36
37     __PACKAGE__->setup();
38
39     package main;
40
41     use Test::More tests => 1;
42     use Catalyst::Test 'TestApp';
43
44     ok( get('/foo') =~ /bar/ );
45
46 =head1 DESCRIPTION
47
48 Test Catalyst Applications.
49
50 =head2 METHODS
51
52 =head2 get
53
54 Returns the content.
55
56     my $content = get('foo/bar?test=1');
57
58 =head2 request
59
60 Returns a C<HTTP::Response> object.
61
62     my $res = request('foo/bar?test=1');
63
64 =cut
65
66 sub import {
67     my $self  = shift;
68     my $class = shift;
69
70     my ( $get, $request );
71
72     if ( $ENV{CATALYST_SERVER} ) {
73         $request = sub { remote_request(@_) };
74         $get     = sub { remote_request(@_)->content };
75     } elsif (! $class) {
76         $request = sub { Catalyst::Exception->throw("Must specify a test app: use Catalyst::Test 'TestApp'") };
77         $get     = $request;
78     } else {
79         unless( Class::Inspector->loaded( $class ) ) {
80             require Class::Inspector->filename( $class );
81         }
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;