1 package Catalyst::Test;
6 use Catalyst::Exception;
8 use UNIVERSAL::require;
11 $ENV{CATALYST_ENGINE} = 'Test';
13 # Bypass a HTTP::Headers bug
15 no warnings 'redefine';
17 sub HTTP::Headers::new {
19 my $self = bless {}, $class;
21 while ( my ( $field, $val ) = splice( @_, 0, 2 ) ) {
22 $self->push_header( $field, $val );
31 Catalyst::Test - Test Catalyst applications
39 use Catalyst::Test 'TestApp';
40 request('index.html');
43 # Run tests against a remote server
44 CATALYST_SERVER='http://localhost:3000/' prove -r -l lib/ t/
46 # Tests with inline apps need to use Catalyst::Engine::Test
49 use Catalyst qw[-Engine=Test];
52 my ( $self, $c ) = @_;
53 $c->res->output('bar');
60 use Test::More tests => 1;
61 use Catalyst::Test 'TestApp';
63 ok( get('/foo') =~ /bar/ );
67 Test Catalyst applications.
77 my $content = get('foo/bar?test=1');
81 Returns a C<HTTP::Response> object.
83 my $res = request('foo/bar?test=1');
91 my ( $get, $request );
93 if ( $ENV{CATALYST_SERVER} ) {
94 $request = sub { remote_request(@_) };
95 $get = sub { remote_request(@_)->content };
100 die if $@ && $@ !~ /^Can't locate /;
103 $request = sub { $class->run(@_) };
104 $get = sub { $class->run(@_)->content };
108 my $caller = caller(0);
109 *{"$caller\::request"} = $request;
110 *{"$caller\::get"} = $get;
117 Do an actual remote request using LWP.
123 require LWP::UserAgent;
125 my $request = Catalyst::Utils::request( shift(@_) );
127 my $server = URI->new( $ENV{CATALYST_SERVER} );
129 if ( $server->path =~ m|^(.+)?/$| ) {
130 $server->path("$1"); # need to be quoted
133 $request->uri->scheme( $server->scheme );
134 $request->uri->host( $server->host );
135 $request->uri->port( $server->port );
136 $request->uri->path( $server->path . $request->uri->path );
140 $agent = LWP::UserAgent->new(
149 return $agent->request($request);
160 Sebastian Riedel, C<sri@cpan.org>
164 This program is free software, you can redistribute it and/or modify it under
165 the same terms as Perl itself.