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