1 package Catalyst::Test;
4 use UNIVERSAL::require;
10 $ENV{CATALYST_ENGINE} = 'CGI';
11 $ENV{CATALYST_TEST} = 1;
15 Catalyst::Test - Test Catalyst applications
20 use Catalyst::Test 'TestApp';
21 request('index.html');
25 perl -MCatalyst::Test=MyApp -e1 index.html
28 perl -MCatalyst::Test=MyApp -e1 3000
32 Test Catalyst applications.
40 my $content = get('foo/bar?test=1');
44 Returns a C<HTTP::Response> object.
46 my $res =request('foo/bar?test=1');
53 if ( ( caller(0) )[1] eq '-e' ) {
54 if ( $ARGV[0] =~ /^\d+$/ ) { server( $ARGV[0] ) }
55 else { print request( $ARGV[0] || 'http://localhost' )->content }
64 my $caller = ( caller(0) )[1];
65 unless ( $INC{'Test/Builder.pm'} ) {
66 die qq/Couldn't load "$class", "$@"/ if $@;
68 my $caller = caller(0);
70 *{"$caller\::request"} = \&request;
71 *{"$caller\::get"} = sub { request(@_)->content };
78 open STDOUT, '>', \$output;
79 $uri = URI->new($uri);
81 $ENV{REQUEST_METHOD} ||= 'GET';
82 $ENV{HTTP_HOST} ||= $uri->authority || 'localhost';
83 $ENV{SCRIPT_NAME} ||= $uri->path || '/';
84 $ENV{QUERY_STRING} ||= $uri->query || '';
85 $ENV{CONTENT_TYPE} ||= 'text/plain';
88 return HTTP::Response->parse($output);
95 Catalyst::Test::server(3000);
103 my $tcp = getprotobyname('tcp');
104 socket( HTTPDaemon, PF_INET, SOCK_STREAM, $tcp ) or die $!;
105 setsockopt( HTTPDaemon, SOL_SOCKET, SO_REUSEADDR, pack( "l", 1 ) )
107 bind( HTTPDaemon, sockaddr_in( $port, INADDR_ANY ) ) or die $!;
108 listen( HTTPDaemon, SOMAXCONN ) or die $!;
110 print "You can connect to your server at http://localhost:$port\n";
114 for ( ; accept( Remote, HTTPDaemon ) ; close Remote ) {
117 my $remote_sockaddr = getpeername(STDIN);
118 my ( undef, $iaddr ) = sockaddr_in($remote_sockaddr);
119 my $peername = gethostbyaddr( $iaddr, AF_INET ) || "localhost";
120 my $peeraddr = inet_ntoa($iaddr) || "127.0.0.1";
121 my $local_sockaddr = getsockname(STDIN);
122 my ( undef, $localiaddr ) = sockaddr_in($local_sockaddr);
123 my $localname = gethostbyaddr( $localiaddr, AF_INET ) || 'localhost';
124 my $localaddr = inet_ntoa($localiaddr) || '127.0.0.1';
127 while ( sysread( STDIN, my $buff, 1 ) ) {
128 last if $buff eq "\n";
131 my ( $method, $request_uri, $proto, undef ) = split /\s+/, $chunk;
132 my ( $file, undef, $query_string ) =
133 ( $request_uri =~ /([^?]*)(\?(.*))?/ );
134 last if ( $method !~ /^(GET|POST|HEAD)$/ );
138 while ( sysread( STDIN, my $buff, 1 ) ) {
139 if ( $buff eq "\n" ) {
140 $chunk =~ s/[\r\l\n\s]+$//;
141 if ( $chunk =~ /^([\w\-]+): (.+)/i ) {
143 $tag =~ s/^COOKIES$/COOKIE/;
146 $tag = "HTTP_" . $tag
147 unless ( grep /^$tag$/, qw(CONTENT_LENGTH CONTENT_TYPE) );
148 if ( $ENV{$tag} ) { $ENV{$tag} .= "; $val" }
149 else { $ENV{$tag} = $val }
151 last if $chunk =~ /^$/;
154 else { $chunk .= $buff }
156 $ENV{SERVER_PROTOCOL} = $proto;
157 $ENV{SERVER_PORT} = $port;
158 $ENV{SERVER_NAME} = $localname;
159 $ENV{SERVER_URL} = "http://$localname:$port/";
160 $ENV{PATH_INFO} = $file;
161 $ENV{REQUEST_URI} = $request_uri;
162 $ENV{REQUEST_METHOD} = $method;
163 $ENV{REMOTE_ADDR} = $peeraddr;
164 $ENV{REMOTE_HOST} = $peername;
165 $ENV{QUERY_STRING} = $query_string || '';
166 $ENV{CONTENT_TYPE} ||= 'multipart/form-data';
167 $ENV{SERVER_SOFTWARE} ||= "Catalyst/$Catalyst::VERSION";
178 Sebastian Riedel, C<sri@cpan.org>
182 This program is free software, you can redistribute it and/or modify it under
183 the same terms as Perl itself.