1 package Catalyst::Test;
4 use UNIVERSAL::require;
14 $ENV{CATALYST_ENGINE} = 'CGI';
15 $ENV{CATALYST_TEST} = 1;
19 Catalyst::Test - Test Catalyst applications
29 use Catalyst::Test 'TestApp';
30 request('index.html');
34 perl -MCatalyst::Test=MyApp -e1 index.html
37 perl -MCatalyst::Test=MyApp -e1 3000
41 Test Catalyst applications.
49 my $content = get('foo/bar?test=1');
53 Returns a C<HTTP::Response> object.
55 my $res =request('foo/bar?test=1');
62 if ( ( caller(0) )[1] eq '-e' ) {
63 if ( $ARGV[0] =~ /^\d+$/ ) { server( $ARGV[0] ) }
64 else { print request( $ARGV[0] || 'http://localhost' )->content }
71 if ( $class = shift ) {
73 unless ( $INC{'Test/Builder.pm'} ) {
74 die qq/Couldn't load "$class", "$@"/ if $@;
76 my $caller = caller(0);
78 *{"$caller\::request"} = \&request;
79 *{"$caller\::get"} = sub { request(@_)->content };
85 unless ( ref $request ) {
86 $request = URI->new( $request, 'http' );
88 unless ( ref $request eq 'HTTP::Request' ) {
89 $request = HTTP::Request->new( 'GET', $request );
91 local ( *STDIN, *STDOUT );
94 $ENV{CONTENT_TYPE} ||= $request->header('Content-Type') || '';
95 $ENV{CONTENT_LENGTH} ||= $request->header('Content-Length') || '';
96 $ENV{GATEWAY_INTERFACE} ||= 'CGI/1.1';
97 $ENV{HTTP_USER_AGENT} ||= 'Catalyst';
98 $ENV{HTTP_HOST} ||= $request->uri->host || 'localhost';
99 $ENV{QUERY_STRING} ||= $request->uri->query || '';
100 $ENV{REQUEST_METHOD} ||= $request->method;
101 $ENV{SCRIPT_NAME} ||= $request->uri->path || '/';
102 $ENV{SERVER_NAME} ||= $request->uri->host || 'localhost';
103 $ENV{SERVER_PORT} ||= $request->uri->port;
104 $ENV{SERVER_PROTOCOL} ||= 'HTTP/1.1';
106 for my $field ( $request->header_field_names ) {
107 if ( $field =~ /^Content-(Length|Type)$/ ) {
111 $ENV{ 'HTTP_' . uc($field) } = $request->header($field);
113 if ( $request->content_length ) {
114 my $body = IO::File->new_tmpfile;
115 $body->print( $request->content ) or die $!;
116 $body->seek( 0, SEEK_SET ) or die $!;
117 open( STDIN, "<&=", $body->fileno )
118 or die("Failed to dup \$body: $!");
120 open( STDOUT, '>', \$output );
123 return HTTP::Response->parse($output);
130 Catalyst::Test::server(3000);
135 my ( $port, $script ) = @_;
138 my $tcp = getprotobyname('tcp');
139 socket( HTTPDaemon, PF_INET, SOCK_STREAM, $tcp ) or die $!;
140 setsockopt( HTTPDaemon, SOL_SOCKET, SO_REUSEADDR, pack( "l", 1 ) )
142 bind( HTTPDaemon, sockaddr_in( $port, INADDR_ANY ) ) or die $!;
143 listen( HTTPDaemon, SOMAXCONN ) or die $!;
145 print "You can connect to your server at http://localhost:$port\n";
149 for ( ; accept( Remote, HTTPDaemon ) ; close Remote ) {
152 my $remote_sockaddr = getpeername(STDIN);
153 my ( undef, $iaddr ) = sockaddr_in($remote_sockaddr);
154 my $peername = gethostbyaddr( $iaddr, AF_INET ) || "localhost";
155 my $peeraddr = inet_ntoa($iaddr) || "127.0.0.1";
156 my $local_sockaddr = getsockname(STDIN);
157 my ( undef, $localiaddr ) = sockaddr_in($local_sockaddr);
158 my $localname = gethostbyaddr( $localiaddr, AF_INET ) || 'localhost';
159 my $localaddr = inet_ntoa($localiaddr) || '127.0.0.1';
162 while ( sysread( STDIN, my $buff, 1 ) ) {
163 last if $buff eq "\n";
166 my ( $method, $request_uri, $proto, undef ) = split /\s+/, $chunk;
167 my ( $file, undef, $query_string ) =
168 ( $request_uri =~ /([^?]*)(\?(.*))?/ );
169 last if ( $method !~ /^(GET|POST|HEAD)$/ );
173 while ( sysread( STDIN, my $buff, 1 ) ) {
174 if ( $buff eq "\n" ) {
175 $chunk =~ s/[\r\l\n\s]+$//;
176 if ( $chunk =~ /^([\w\-]+): (.+)/i ) {
178 $tag =~ s/^COOKIES$/COOKIE/;
181 $tag = "HTTP_" . $tag
182 unless ( grep /^$tag$/, qw(CONTENT_LENGTH CONTENT_TYPE) );
183 if ( $ENV{$tag} ) { $ENV{$tag} .= "; $val" }
184 else { $ENV{$tag} = $val }
186 last if $chunk =~ /^$/;
189 else { $chunk .= $buff }
191 $ENV{SERVER_PROTOCOL} = $proto;
192 $ENV{SERVER_PORT} = $port;
193 $ENV{SERVER_NAME} = $localname;
194 $ENV{SERVER_URL} = "http://$localname:$port/";
195 $ENV{PATH_INFO} = $file;
196 $ENV{REQUEST_URI} = $request_uri;
197 $ENV{REQUEST_METHOD} = $method;
198 $ENV{REMOTE_ADDR} = $peeraddr;
199 $ENV{REMOTE_HOST} = $peername;
200 $ENV{QUERY_STRING} = $query_string || '';
201 $ENV{CONTENT_TYPE} ||= 'multipart/form-data';
202 $ENV{SERVER_SOFTWARE} ||= "Catalyst/$Catalyst::VERSION";
203 $script ? print STDOUT `$script` : $class->run;
213 Sebastian Riedel, C<sri@cpan.org>
217 This program is free software, you can redistribute it and/or modify it under
218 the same terms as Perl itself.