1 package Catalyst::Test;
4 use UNIVERSAL::require;
14 $ENV{CATALYST_ENGINE} = 'CGI';
18 Catalyst::Test - Test Catalyst applications
26 use Catalyst::Test 'TestApp';
27 request('index.html');
31 perl -MCatalyst::Test=MyApp -e1 index.html
35 Test Catalyst applications.
43 my $content = get('foo/bar?test=1');
47 Returns a C<HTTP::Response> object.
49 my $res =request('foo/bar?test=1');
56 if ( ( caller(0) )[1] eq '-e' ) {
57 print request( $ARGV[0] || 'http://localhost' )->content;
64 if ( $class = shift ) {
66 unless ( $INC{'Test/Builder.pm'} ) {
67 die qq/Couldn't load "$class", "$@"/ if $@;
69 my $caller = caller(0);
71 *{"$caller\::request"} = \&request;
72 *{"$caller\::get"} = sub { request(@_)->content };
78 unless ( ref $request ) {
79 $request = URI->new( $request, 'http' );
81 unless ( ref $request eq 'HTTP::Request' ) {
82 $request = HTTP::Request->new( 'GET', $request );
84 local ( *STDIN, *STDOUT );
87 $ENV{CONTENT_TYPE} ||= $request->header('Content-Type') || '';
88 $ENV{CONTENT_LENGTH} ||= $request->header('Content-Length') || '';
89 $ENV{GATEWAY_INTERFACE} ||= 'CGI/1.1';
90 $ENV{HTTP_USER_AGENT} ||= 'Catalyst';
91 $ENV{HTTP_HOST} ||= $request->uri->host || 'localhost';
92 $ENV{QUERY_STRING} ||= $request->uri->query || '';
93 $ENV{REQUEST_METHOD} ||= $request->method;
94 $ENV{PATH_INFO} ||= $request->uri->path || '/';
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.