now really die
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Test.pm
1 package Catalyst::Test;
2
3 use strict;
4 use UNIVERSAL::require;
5
6 $ENV{CATALYST_ENGINE} = 'Test';
7
8 =head1 NAME
9
10 Catalyst::Test - Test Catalyst applications
11
12 =head1 SYNOPSIS
13
14     # Helper
15     script/test.pl
16
17     # Tests
18     use Catalyst::Test 'TestApp';
19     request('index.html');
20     get('index.html');
21
22     # Run tests against a remote server
23     CATALYST_SERVER='http://localhost:3000/' prove -l lib/ t/
24
25     # Tests with inline apps need to use Catalyst::Engine::Test
26     package TestApp;
27
28     use Catalyst qw[-Engine=Test];
29
30     __PACKAGE__->action(
31         foo => sub {
32             my ( $self, $c ) = @_;
33             $c->res->output('bar');
34         }
35     );
36
37     package main;
38
39     use Test::More tests => 1;
40     use Catalyst::Test 'TestApp';
41
42     ok( get('/foo') =~ /bar/ );
43
44 =head1 DESCRIPTION
45
46 Test Catalyst applications.
47
48 =head2 METHODS
49
50 =over 4
51
52 =item get
53
54 Returns the content.
55
56     my $content = get('foo/bar?test=1');
57
58 =item 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         my $error = $UNIVERSAL::require::ERROR;
80         die qq/Couldn't load "$class", "$error"/ if $error;
81
82         unless ( $INC{'Test/Builder.pm'} ) {
83             die qq/Couldn't load "$class", "$@"/ if $@;
84         }
85
86         $class->import;
87
88         $request = sub { $class->run(@_) };
89         $get     = sub { $class->run(@_)->content };
90     }
91
92     no strict 'refs';
93     my $caller = caller(0);
94     *{"$caller\::request"} = $request;
95     *{"$caller\::get"}     = $get;
96 }
97
98 my $agent;
99
100 =item remote_request
101
102 Do an actual remote rquest using LWP.
103
104 =cut
105
106 sub remote_request {
107     my $request = shift;
108
109     require LWP::UserAgent;
110
111     unless ( ref $request ) {
112
113         my $uri =
114           ( $request =~ m/http/i )
115           ? URI->new($request)
116           : URI->new( 'http://localhost' . $request );
117
118         $request = $uri->canonical;
119     }
120
121     unless ( ref $request eq 'HTTP::Request' ) {
122         $request = HTTP::Request->new( 'GET', $request );
123     }
124
125     my $server = URI->new( $ENV{CATALYST_SERVER} );
126
127     if ( $server->path =~ m|^(.+)?/$| ) {
128         $server->path("$1");    # need to be quoted
129     }
130
131     $request->uri->scheme( $server->scheme );
132     $request->uri->host( $server->host );
133     $request->uri->port( $server->port );
134     $request->uri->path( $server->path . $request->uri->path );
135
136     unless ($agent) {
137         $agent = LWP::UserAgent->new(
138
139             #  cookie_jar   => {},
140             keep_alive   => 1,
141             max_redirect => 0,
142             timeout      => 60,
143         );
144         $agent->env_proxy;
145     }
146
147     return $agent->request($request);
148 }
149
150 =back 
151
152 =head1 SEE ALSO
153
154 L<Catalyst>.
155
156 =head1 AUTHOR
157
158 Sebastian Riedel, C<sri@cpan.org>
159
160 =head1 COPYRIGHT
161
162 This program is free software, you can redistribute it and/or modify it under
163 the same terms as Perl itself.
164
165 =cut
166
167 1;