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 );
93 $ENV{CONTENT_TYPE} ||= $request->header('Content-Type') || '';
94 $ENV{CONTENT_LENGTH} ||= $request->header('Content-Length') || '';
95 $ENV{GATEWAY_INTERFACE} ||= 'CGI/1.1';
96 $ENV{HTTP_HOST} ||= $request->uri->host || 'localhost';
97 $ENV{QUERY_STRING} ||= $request->uri->query || '';
98 $ENV{REQUEST_METHOD} ||= $request->method;
99 $ENV{SCRIPT_NAME} ||= $request->uri->path || '/';
100 $ENV{SERVER_NAME} ||= $request->uri->host || 'localhost';
101 $ENV{SERVER_PORT} ||= $request->uri->port;
102 $ENV{SERVER_PROTOCOL} ||= 'HTTP/1.1';
104 for my $field ( $request->header_field_names ) {
105 if ( $field =~ /^Content-(Length|Type)$/ ) {
109 $ENV{ 'HTTP_' . uc($field) } = $request->header($field);
111 if ( $request->content_length ) {
112 my $body = IO::File->new_tmpfile;
113 $body->print( $request->content ) or die $!;
114 $body->seek( 0, SEEK_SET ) or die $!;
115 open( STDIN, "<&=", $body->fileno )
116 or die("Failed to dup \$body: $!");
118 open( STDOUT, '>', \$output );
121 return HTTP::Response->parse($output);
128 Catalyst::Test::server(3000);
133 my ( $port, $script ) = @_;
136 my $tcp = getprotobyname('tcp');
137 socket( HTTPDaemon, PF_INET, SOCK_STREAM, $tcp ) or die $!;
138 setsockopt( HTTPDaemon, SOL_SOCKET, SO_REUSEADDR, pack( "l", 1 ) )
140 bind( HTTPDaemon, sockaddr_in( $port, INADDR_ANY ) ) or die $!;
141 listen( HTTPDaemon, SOMAXCONN ) or die $!;
143 print "You can connect to your server at http://localhost:$port\n";
147 for ( ; accept( Remote, HTTPDaemon ) ; close Remote ) {
150 my $remote_sockaddr = getpeername(STDIN);
151 my ( undef, $iaddr ) = sockaddr_in($remote_sockaddr);
152 my $peername = gethostbyaddr( $iaddr, AF_INET ) || "localhost";
153 my $peeraddr = inet_ntoa($iaddr) || "127.0.0.1";
154 my $local_sockaddr = getsockname(STDIN);
155 my ( undef, $localiaddr ) = sockaddr_in($local_sockaddr);
156 my $localname = gethostbyaddr( $localiaddr, AF_INET ) || 'localhost';
157 my $localaddr = inet_ntoa($localiaddr) || '127.0.0.1';
160 while ( sysread( STDIN, my $buff, 1 ) ) {
161 last if $buff eq "\n";
164 my ( $method, $request_uri, $proto, undef ) = split /\s+/, $chunk;
165 my ( $file, undef, $query_string ) =
166 ( $request_uri =~ /([^?]*)(\?(.*))?/ );
167 last if ( $method !~ /^(GET|POST|HEAD)$/ );
171 while ( sysread( STDIN, my $buff, 1 ) ) {
172 if ( $buff eq "\n" ) {
173 $chunk =~ s/[\r\l\n\s]+$//;
174 if ( $chunk =~ /^([\w\-]+): (.+)/i ) {
176 $tag =~ s/^COOKIES$/COOKIE/;
179 $tag = "HTTP_" . $tag
180 unless ( grep /^$tag$/, qw(CONTENT_LENGTH CONTENT_TYPE) );
181 if ( $ENV{$tag} ) { $ENV{$tag} .= "; $val" }
182 else { $ENV{$tag} = $val }
184 last if $chunk =~ /^$/;
187 else { $chunk .= $buff }
189 $ENV{SERVER_PROTOCOL} = $proto;
190 $ENV{SERVER_PORT} = $port;
191 $ENV{SERVER_NAME} = $localname;
192 $ENV{SERVER_URL} = "http://$localname:$port/";
193 $ENV{PATH_INFO} = $file;
194 $ENV{REQUEST_URI} = $request_uri;
195 $ENV{REQUEST_METHOD} = $method;
196 $ENV{REMOTE_ADDR} = $peeraddr;
197 $ENV{REMOTE_HOST} = $peername;
198 $ENV{QUERY_STRING} = $query_string || '';
199 $ENV{CONTENT_TYPE} ||= 'multipart/form-data';
200 $ENV{SERVER_SOFTWARE} ||= "Catalyst/$Catalyst::VERSION";
201 $script ? print STDOUT `$script` : $class->run;
211 Sebastian Riedel, C<sri@cpan.org>
215 This program is free software, you can redistribute it and/or modify it under
216 the same terms as Perl itself.