1 package Catalyst::Test;
4 use UNIVERSAL::require;
14 $ENV{CATALYST_ENGINE} = 'CGI';
15 $ENV{CATALYST_TEST} = 1;
19 Catalyst::Test - Test Catalyst applications
27 use Catalyst::Test 'TestApp';
28 request('index.html');
32 perl -MCatalyst::Test=MyApp -e1 index.html
36 Test Catalyst applications.
44 my $content = get('foo/bar?test=1');
48 Returns a C<HTTP::Response> object.
50 my $res =request('foo/bar?test=1');
57 if ( ( caller(0) )[1] eq '-e' ) {
58 print request( $ARGV[0] || 'http://localhost' )->content;
65 if ( $class = shift ) {
67 unless ( $INC{'Test/Builder.pm'} ) {
68 die qq/Couldn't load "$class", "$@"/ if $@;
70 my $caller = caller(0);
72 *{"$caller\::request"} = \&request;
73 *{"$caller\::get"} = sub { request(@_)->content };
79 unless ( ref $request ) {
80 $request = URI->new( $request, 'http' );
82 unless ( ref $request eq 'HTTP::Request' ) {
83 $request = HTTP::Request->new( 'GET', $request );
85 local ( *STDIN, *STDOUT );
88 $ENV{CONTENT_TYPE} ||= $request->header('Content-Type') || '';
89 $ENV{CONTENT_LENGTH} ||= $request->header('Content-Length') || '';
90 $ENV{GATEWAY_INTERFACE} ||= 'CGI/1.1';
91 $ENV{HTTP_USER_AGENT} ||= 'Catalyst';
92 $ENV{HTTP_HOST} ||= $request->uri->host || 'localhost';
93 $ENV{QUERY_STRING} ||= $request->uri->query || '';
94 $ENV{REQUEST_METHOD} ||= $request->method;
95 $ENV{SCRIPT_NAME} ||= $request->uri->path || '/';
96 $ENV{SERVER_NAME} ||= $request->uri->host || 'localhost';
97 $ENV{SERVER_PORT} ||= $request->uri->port;
98 $ENV{SERVER_PROTOCOL} ||= 'HTTP/1.1';
100 for my $field ( $request->header_field_names ) {
101 if ( $field =~ /^Content-(Length|Type)$/ ) {
105 $ENV{ 'HTTP_' . uc($field) } = $request->header($field);
107 if ( $request->content_length ) {
108 my $body = IO::File->new_tmpfile;
109 $body->print( $request->content ) or die $!;
110 $body->seek( 0, SEEK_SET ) or die $!;
111 open( STDIN, "<&=", $body->fileno )
112 or die("Failed to dup \$body: $!");
114 open( STDOUT, '>', \$output );
117 return HTTP::Response->parse($output);
126 Sebastian Riedel, C<sri@cpan.org>
130 This program is free software, you can redistribute it and/or modify it under
131 the same terms as Perl itself.