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