make pod tests pass, if rclamp applies my patch.
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Test.pm
CommitLineData
fc7ec1d9 1package Catalyst::Test;
2
3use strict;
4use UNIVERSAL::require;
fc7ec1d9 5
e05c5e3c 6$ENV{CATALYST_ENGINE} = 'Test';
fc7ec1d9 7
8=head1 NAME
9
10Catalyst::Test - Test Catalyst applications
11
12=head1 SYNOPSIS
13
49faa307 14 # Helper
49faa307 15 script/test.pl
16
fc7ec1d9 17 # Tests
18 use Catalyst::Test 'TestApp';
19 request('index.html');
20 get('index.html');
21
45374ac6 22 # Run tests against a remote server
d96e14c2 23 CATALYST_SERVER='http://localhost:3000/' prove -l lib/ t/
45374ac6 24
b6898a9f 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
fc7ec1d9 44=head1 DESCRIPTION
45
46Test Catalyst applications.
47
48=head2 METHODS
49
bea4160a 50=over 4
51
52=item get
fc7ec1d9 53
54Returns the content.
55
56 my $content = get('foo/bar?test=1');
57
bea4160a 58=item request
fc7ec1d9 59
60Returns a C<HTTP::Response> object.
61
62 my $res =request('foo/bar?test=1');
63
64=cut
65
fc7ec1d9 66sub import {
66d9e175 67 my $self = shift;
45374ac6 68 my $class = shift;
69
70 my ( $get, $request );
71
d96e14c2 72 if ( $ENV{CATALYST_SERVER} ) {
45374ac6 73 $request = sub { remote_request(@_) };
74 $get = sub { remote_request(@_)->content };
75 }
76
77 else {
bc024080 78 $class->require;
d96e14c2 79
bc024080 80 unless ( $INC{'Test/Builder.pm'} ) {
81 die qq/Couldn't load "$class", "$@"/ if $@;
82 }
e646f111 83
d96e14c2 84 $class->import;
85
45374ac6 86 $request = sub { $class->run(@_) };
87 $get = sub { $class->run(@_)->content };
49faa307 88 }
45374ac6 89
90 no strict 'refs';
91 my $caller = caller(0);
92 *{"$caller\::request"} = $request;
93 *{"$caller\::get"} = $get;
94}
95
523d44ec 96my $agent;
97
bea4160a 98=item remote_request
99
100Do an actual remote rquest using LWP.
101
102=cut
103
45374ac6 104sub remote_request {
105 my $request = shift;
106
107 require LWP::UserAgent;
108
45374ac6 109 unless ( ref $request ) {
110
523d44ec 111 my $uri = ( $request =~ m/http/i )
45374ac6 112 ? URI->new($request)
113 : URI->new( 'http://localhost' . $request );
114
115 $request = $uri->canonical;
116 }
117
45374ac6 118 unless ( ref $request eq 'HTTP::Request' ) {
119 $request = HTTP::Request->new( 'GET', $request );
120 }
121
523d44ec 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 $agent = LWP::UserAgent->new(
135 # cookie_jar => {},
136 keep_alive => 1,
137 max_redirect => 0,
138 timeout => 60,
139 );
140 $agent->env_proxy;
141 }
45374ac6 142
143 return $agent->request($request);
fc7ec1d9 144}
145
bea4160a 146=back
147
fc7ec1d9 148=head1 SEE ALSO
149
150L<Catalyst>.
151
152=head1 AUTHOR
153
154Sebastian Riedel, C<sri@cpan.org>
155
156=head1 COPYRIGHT
157
158This program is free software, you can redistribute it and/or modify it under
159the same terms as Perl itself.
160
161=cut
162
1631;