Minor engine cleanup
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Test.pm
1 package Catalyst::Test;
2
3 use strict;
4
5 use Catalyst::Utils;
6 use UNIVERSAL::require;
7
8 $ENV{CATALYST_ENGINE} = 'Test';
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 -l lib/ t/
26
27     # Tests with inline apps need to use Catalyst::Engine::Test
28     package TestApp;
29
30     use Catalyst qw[-Engine=Test];
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 =over 4
53
54 =item get
55
56 Returns the content.
57
58     my $content = get('foo/bar?test=1');
59
60 =item 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         my $error = $UNIVERSAL::require::ERROR;
82         die qq/Couldn't load "$class", "$error"/ if $@;
83
84         $class->import;
85
86         $request = sub { $class->run(@_) };
87         $get     = sub { $class->run(@_)->content };
88     }
89
90     no strict 'refs';
91     my $caller = caller(0);
92     *{"$caller\::request"} = $request;
93     *{"$caller\::get"}     = $get;
94 }
95
96 my $agent;
97
98 =item remote_request
99
100 Do an actual remote rquest using LWP.
101
102 =cut
103
104 sub remote_request {
105
106     require LWP::UserAgent; 
107     
108     my $request = Catalyst::Utils::request( shift(@_) );
109
110     my $server  = URI->new( $ENV{CATALYST_SERVER} );
111
112     if ( $server->path =~ m|^(.+)?/$| ) {
113         $server->path("$1");    # need to be quoted
114     }
115
116     $request->uri->scheme( $server->scheme );
117     $request->uri->host( $server->host );
118     $request->uri->port( $server->port );
119     $request->uri->path( $server->path . $request->uri->path );
120
121     unless ( $agent ) {
122
123         $agent = LWP::UserAgent->new(
124             keep_alive   => 1,
125             max_redirect => 0,
126             timeout      => 60,
127         );
128
129         $agent->env_proxy;
130     }
131
132     return $agent->request($request);
133 }
134
135 =back 
136
137 =head1 SEE ALSO
138
139 L<Catalyst>.
140
141 =head1 AUTHOR
142
143 Sebastian Riedel, C<sri@cpan.org>
144
145 =head1 COPYRIGHT
146
147 This program is free software, you can redistribute it and/or modify it under
148 the same terms as Perl itself.
149
150 =cut
151
152 1;