package Catalyst::Test;
use strict;
+
+use Catalyst::Exception;
+use Catalyst::Utils;
use UNIVERSAL::require;
$ENV{CATALYST_ENGINE} = 'Test';
request('index.html');
get('index.html');
+ # Run tests against a remote server
+ CATALYST_SERVER='http://localhost:3000/' prove -l lib/ t/
+
# Tests with inline apps need to use Catalyst::Engine::Test
package TestApp;
use Catalyst qw[-Engine=Test];
- __PACKAGE__->action(
- foo => sub {
+ sub foo : Global {
my ( $self, $c ) = @_;
$c->res->output('bar');
- }
- );
+ }
+
+ __PACKAGE__->setup();
package main;
ok( get('/foo') =~ /bar/ );
-
=head1 DESCRIPTION
Test Catalyst applications.
=head2 METHODS
-=head3 get
+=over 4
+
+=item get
Returns the content.
my $content = get('foo/bar?test=1');
-=head3 request
+=item request
Returns a C<HTTP::Response> object.
=cut
sub import {
- my $self = shift;
- if ( my $class = shift ) {
+ my $self = shift;
+ my $class = shift;
+
+ my ( $get, $request );
+
+ if ( $ENV{CATALYST_SERVER} ) {
+ $request = sub { remote_request(@_) };
+ $get = sub { remote_request(@_)->content };
+ }
+
+ else {
$class->require;
- unless ( $INC{'Test/Builder.pm'} ) {
- die qq/Couldn't load "$class", "$@"/ if $@;
+
+ if ( $@ ) {
+
+ my $error = $UNIVERSAL::require::ERROR;
+
+ Catalyst::Exception->throw(
+ message => qq/Couldn't load "$class", "$error"/
+ );
}
- no strict 'refs';
- my $caller = caller(0);
- *{"$caller\::request"} = sub { $class->run(@_) };
- *{"$caller\::get"} = sub { $class->run(@_)->content };
+ $class->import;
+
+ $request = sub { $class->run(@_) };
+ $get = sub { $class->run(@_)->content };
}
+
+ no strict 'refs';
+ my $caller = caller(0);
+ *{"$caller\::request"} = $request;
+ *{"$caller\::get"} = $get;
}
+my $agent;
+
+=item remote_request
+
+Do an actual remote rquest using LWP.
+
+=cut
+
+sub remote_request {
+
+ require LWP::UserAgent;
+
+ my $request = Catalyst::Utils::request( shift(@_) );
+
+ my $server = URI->new( $ENV{CATALYST_SERVER} );
+
+ if ( $server->path =~ m|^(.+)?/$| ) {
+ $server->path("$1"); # need to be quoted
+ }
+
+ $request->uri->scheme( $server->scheme );
+ $request->uri->host( $server->host );
+ $request->uri->port( $server->port );
+ $request->uri->path( $server->path . $request->uri->path );
+
+ unless ( $agent ) {
+
+ $agent = LWP::UserAgent->new(
+ keep_alive => 1,
+ max_redirect => 0,
+ timeout => 60,
+ );
+
+ $agent->env_proxy;
+ }
+
+ return $agent->request($request);
+}
+
+=back
+
=head1 SEE ALSO
L<Catalyst>.