improved Catalyst::Test::request
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Test.pm
CommitLineData
fc7ec1d9 1package Catalyst::Test;
2
3use strict;
4use UNIVERSAL::require;
49faa307 5use IO::File;
6use HTTP::Request;
fc7ec1d9 7use HTTP::Response;
8use Socket;
9use URI;
10
bc024080 11require Catalyst;
12
fc7ec1d9 13my $class;
14$ENV{CATALYST_ENGINE} = 'CGI';
15$ENV{CATALYST_TEST} = 1;
16
17=head1 NAME
18
19Catalyst::Test - Test Catalyst applications
20
21=head1 SYNOPSIS
22
49faa307 23 # Helper
24 script/cgi-server.pl
25 script/server.pl
26 script/test.pl
27
fc7ec1d9 28 # Tests
29 use Catalyst::Test 'TestApp';
30 request('index.html');
31 get('index.html');
32
33 # Request
34 perl -MCatalyst::Test=MyApp -e1 index.html
35
36 # Server
37 perl -MCatalyst::Test=MyApp -e1 3000
38
39=head1 DESCRIPTION
40
41Test Catalyst applications.
42
43=head2 METHODS
44
45=head3 get
46
47Returns the content.
48
49 my $content = get('foo/bar?test=1');
50
51=head3 request
52
53Returns a C<HTTP::Response> object.
54
55 my $res =request('foo/bar?test=1');
56
57=cut
58
59{
60 no warnings;
61 CHECK {
62 if ( ( caller(0) )[1] eq '-e' ) {
63 if ( $ARGV[0] =~ /^\d+$/ ) { server( $ARGV[0] ) }
64 else { print request( $ARGV[0] || 'http://localhost' )->content }
65 }
66 }
67}
68
69sub import {
70 my $self = shift;
bc024080 71 if ( $class = shift ) {
72 $class->require;
73 unless ( $INC{'Test/Builder.pm'} ) {
74 die qq/Couldn't load "$class", "$@"/ if $@;
75 }
76 my $caller = caller(0);
77 no strict 'refs';
78 *{"$caller\::request"} = \&request;
79 *{"$caller\::get"} = sub { request(@_)->content };
fc7ec1d9 80 }
fc7ec1d9 81}
82
83sub request {
49faa307 84 my $request = shift;
85 unless ( ref $request ) {
86 $request = URI->new( $request, 'http' );
87 }
88 unless ( ref $request eq 'HTTP::Request' ) {
89 $request = HTTP::Request->new( 'GET', $request );
90 }
91 local ( *STDIN, *STDOUT );
fc7ec1d9 92 my %clean = %ENV;
49faa307 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';
103
104 for my $field ( $request->header_field_names ) {
105 if ( $field =~ /^Content-(Length|Type)$/ ) {
106 next;
107 }
108 $field =~ s/-/_/g;
109 $ENV{ 'HTTP_' . uc($field) } = $request->header($field);
110 }
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: $!");
117 }
118 open( STDOUT, '>', \$output );
fc7ec1d9 119 $class->handler;
120 %ENV = %clean;
121 return HTTP::Response->parse($output);
122}
123
124=head3 server
125
126Starts a testserver.
127
128 Catalyst::Test::server(3000);
129
130=cut
131
132sub server {
bc024080 133 my ( $port, $script ) = @_;
fc7ec1d9 134
135 # Listen
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 ) )
139 or warn $!;
140 bind( HTTPDaemon, sockaddr_in( $port, INADDR_ANY ) ) or die $!;
141 listen( HTTPDaemon, SOMAXCONN ) or die $!;
142
143 print "You can connect to your server at http://localhost:$port\n";
144
145 # Process
146 my %clean = %ENV;
147 for ( ; accept( Remote, HTTPDaemon ) ; close Remote ) {
148 *STDIN = *Remote;
149 *STDOUT = *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';
158 my $chunk;
159
160 while ( sysread( STDIN, my $buff, 1 ) ) {
161 last if $buff eq "\n";
162 $chunk .= $buff;
163 }
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)$/ );
168 %ENV = %clean;
169
170 $chunk = '';
171 while ( sysread( STDIN, my $buff, 1 ) ) {
172 if ( $buff eq "\n" ) {
173 $chunk =~ s/[\r\l\n\s]+$//;
174 if ( $chunk =~ /^([\w\-]+): (.+)/i ) {
175 my $tag = uc($1);
176 $tag =~ s/^COOKIES$/COOKIE/;
177 my $val = $2;
178 $tag =~ s/-/_/g;
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 }
183 }
184 last if $chunk =~ /^$/;
185 $chunk = '';
186 }
187 else { $chunk .= $buff }
188 }
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 || '';
7833fdfc 199 $ENV{CONTENT_TYPE} ||= 'multipart/form-data';
fc7ec1d9 200 $ENV{SERVER_SOFTWARE} ||= "Catalyst/$Catalyst::VERSION";
bc024080 201 $script ? print STDOUT `$script` : $class->run;
fc7ec1d9 202 }
203}
204
205=head1 SEE ALSO
206
207L<Catalyst>.
208
209=head1 AUTHOR
210
211Sebastian Riedel, C<sri@cpan.org>
212
213=head1 COPYRIGHT
214
215This program is free software, you can redistribute it and/or modify it under
216the same terms as Perl itself.
217
218=cut
219
2201;