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 if ( ( caller(0) )[1] eq '-e' ) {
65 die qq/Couldn't load "$class", "$@"/ if $@;
67 my $caller = caller(0);
69 *{"$caller\::request"} = \&request;
70 *{"$caller\::get"} = sub { request(@_)->content };
77 open STDOUT, '>', \$output;
78 $uri = URI->new($uri);
80 $ENV{REQUEST_METHOD} ||= 'GET';
81 $ENV{HTTP_HOST} ||= $uri->authority || 'localhost';
82 $ENV{SCRIPT_NAME} ||= $uri->path || '/';
83 $ENV{QUERY_STRING} ||= $uri->query || '';
84 $ENV{CONTENT_TYPE} ||= 'text/plain';
87 return HTTP::Response->parse($output);
94 Catalyst::Test::server(3000);
102 my $tcp = getprotobyname('tcp');
103 socket( HTTPDaemon, PF_INET, SOCK_STREAM, $tcp ) or die $!;
104 setsockopt( HTTPDaemon, SOL_SOCKET, SO_REUSEADDR, pack( "l", 1 ) )
106 bind( HTTPDaemon, sockaddr_in( $port, INADDR_ANY ) ) or die $!;
107 listen( HTTPDaemon, SOMAXCONN ) or die $!;
109 print "You can connect to your server at http://localhost:$port\n";
113 for ( ; accept( Remote, HTTPDaemon ) ; close Remote ) {
116 my $remote_sockaddr = getpeername(STDIN);
117 my ( undef, $iaddr ) = sockaddr_in($remote_sockaddr);
118 my $peername = gethostbyaddr( $iaddr, AF_INET ) || "localhost";
119 my $peeraddr = inet_ntoa($iaddr) || "127.0.0.1";
120 my $local_sockaddr = getsockname(STDIN);
121 my ( undef, $localiaddr ) = sockaddr_in($local_sockaddr);
122 my $localname = gethostbyaddr( $localiaddr, AF_INET ) || 'localhost';
123 my $localaddr = inet_ntoa($localiaddr) || '127.0.0.1';
126 while ( sysread( STDIN, my $buff, 1 ) ) {
127 last if $buff eq "\n";
130 my ( $method, $request_uri, $proto, undef ) = split /\s+/, $chunk;
131 my ( $file, undef, $query_string ) =
132 ( $request_uri =~ /([^?]*)(\?(.*))?/ );
133 last if ( $method !~ /^(GET|POST|HEAD)$/ );
137 while ( sysread( STDIN, my $buff, 1 ) ) {
138 if ( $buff eq "\n" ) {
139 $chunk =~ s/[\r\l\n\s]+$//;
140 if ( $chunk =~ /^([\w\-]+): (.+)/i ) {
142 $tag =~ s/^COOKIES$/COOKIE/;
145 $tag = "HTTP_" . $tag
146 unless ( grep /^$tag$/, qw(CONTENT_LENGTH CONTENT_TYPE) );
147 if ( $ENV{$tag} ) { $ENV{$tag} .= "; $val" }
148 else { $ENV{$tag} = $val }
150 last if $chunk =~ /^$/;
153 else { $chunk .= $buff }
155 $ENV{SERVER_PROTOCOL} = $proto;
156 $ENV{SERVER_PORT} = $port;
157 $ENV{SERVER_NAME} = $localname;
158 $ENV{SERVER_URL} = "http://$localname:$port/";
159 $ENV{PATH_INFO} = $file;
160 $ENV{REQUEST_URI} = $request_uri;
161 $ENV{REQUEST_METHOD} = $method;
162 $ENV{REMOTE_ADDR} = $peeraddr;
163 $ENV{REMOTE_HOST} = $peername;
164 $ENV{QUERY_STRING} = $query_string || '';
165 $ENV{CONTENT_TYPE} ||= 'text/plain';
166 $ENV{SERVER_SOFTWARE} ||= "Catalyst/$Catalyst::VERSION";
177 Sebastian Riedel, C<sri@cpan.org>
181 This program is free software, you can redistribute it and/or modify it under
182 the same terms as Perl itself.