Releng, test fixes, bug fixes for Session plugins
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Test.pm
1 package Catalyst::Test;
2
3 use strict;
4
5 use Catalyst::Exception;
6 use Catalyst::Utils;
7 use UNIVERSAL::require;
8
9 $ENV{CATALYST_ENGINE} = 'Test';
10
11 =head1 NAME
12
13 Catalyst::Test - Test Catalyst applications
14
15 =head1 SYNOPSIS
16
17     # Helper
18     script/test.pl
19
20     # Tests
21     use Catalyst::Test 'TestApp';
22     request('index.html');
23     get('index.html');
24
25     # Run tests against a remote server
26     CATALYST_SERVER='http://localhost:3000/' prove -r -l lib/ t/
27
28     # Tests with inline apps need to use Catalyst::Engine::Test
29     package TestApp;
30
31     use Catalyst qw[-Engine=Test];
32
33     sub foo : Global {
34             my ( $self, $c ) = @_;
35             $c->res->output('bar');
36     }
37
38     __PACKAGE__->setup();
39
40     package main;
41
42     use Test::More tests => 1;
43     use Catalyst::Test 'TestApp';
44
45     ok( get('/foo') =~ /bar/ );
46
47 =head1 DESCRIPTION
48
49 Test Catalyst applications.
50
51 =head2 METHODS
52
53 =over 4
54
55 =item get
56
57 Returns the content.
58
59     my $content = get('foo/bar?test=1');
60
61 =item request
62
63 Returns a C<HTTP::Response> object.
64
65     my $res =request('foo/bar?test=1');
66
67 =cut
68
69 sub import {
70     my $self  = shift;
71     my $class = shift;
72
73     my ( $get, $request );
74
75     if ( $ENV{CATALYST_SERVER} ) {
76         $request = sub { remote_request(@_) };
77         $get     = sub { remote_request(@_)->content };
78     }
79
80     else {
81         $class->require;
82                 die if $@ && $@ !~ /^Can't locate /;
83         $class->import;
84
85         $request = sub { $class->run(@_) };
86         $get     = sub { $class->run(@_)->content };
87     }
88
89     no strict 'refs';
90     my $caller = caller(0);
91     *{"$caller\::request"} = $request;
92     *{"$caller\::get"}     = $get;
93 }
94
95 my $agent;
96
97 =item remote_request
98
99 Do an actual remote request using LWP.
100
101 =cut
102
103 sub remote_request {
104
105     require LWP::UserAgent;
106
107     my $request = Catalyst::Utils::request( shift(@_) );
108
109     my $server = URI->new( $ENV{CATALYST_SERVER} );
110
111     if ( $server->path =~ m|^(.+)?/$| ) {
112         $server->path("$1");    # need to be quoted
113     }
114
115     $request->uri->scheme( $server->scheme );
116     $request->uri->host( $server->host );
117     $request->uri->port( $server->port );
118     $request->uri->path( $server->path . $request->uri->path );
119
120     unless ($agent) {
121
122         $agent = LWP::UserAgent->new(
123             keep_alive   => 1,
124             max_redirect => 0,
125             timeout      => 60,
126         );
127
128         $agent->env_proxy;
129     }
130
131     return $agent->request($request);
132 }
133
134 =back 
135
136 =head1 SEE ALSO
137
138 L<Catalyst>.
139
140 =head1 AUTHOR
141
142 Sebastian Riedel, C<sri@cpan.org>
143
144 =head1 COPYRIGHT
145
146 This program is free software, you can redistribute it and/or modify it under
147 the same terms as Perl itself.
148
149 =cut
150
151 1;