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