typo again :/
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Test.pm
1 package Catalyst::Test;
2
3 use strict;
4 use UNIVERSAL::require;
5 use IO::File;
6 use HTTP::Request;
7 use HTTP::Response;
8 use Socket;
9 use URI;
10
11 require Catalyst;
12
13 my $class;
14 $ENV{CATALYST_ENGINE} = 'CGI';
15 $ENV{CATALYST_TEST}   = 1;
16
17 =head1 NAME
18
19 Catalyst::Test - Test Catalyst applications
20
21 =head1 SYNOPSIS
22
23     # Helper
24     script/cgi-server.pl
25     script/server.pl
26     script/test.pl
27
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
41 Test Catalyst applications.
42
43 =head2 METHODS
44
45 =head3 get
46
47 Returns the content.
48
49     my $content = get('foo/bar?test=1');
50
51 =head3 request
52
53 Returns 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
69 sub import {
70     my $self = shift;
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 };
80     }
81 }
82
83 sub request {
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 );
92     my %clean  = %ENV;
93     my $output = '';
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';
104
105     for my $field ( $request->header_field_names ) {
106         if ( $field =~ /^Content-(Length|Type)$/ ) {
107             next;
108         }
109         $field =~ s/-/_/g;
110         $ENV{ 'HTTP_' . uc($field) } = $request->header($field);
111     }
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: $!");
118     }
119     open( STDOUT, '>', \$output );
120     $class->handler;
121     %ENV = %clean;
122     return HTTP::Response->parse($output);
123 }
124
125 =head3 server
126
127 Starts a testserver.
128
129     Catalyst::Test::server(3000);
130
131 =cut
132
133 sub server {
134     my ( $port, $script ) = @_;
135
136     # Listen
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 ) )
140       or warn $!;
141     bind( HTTPDaemon, sockaddr_in( $port, INADDR_ANY ) ) or die $!;
142     listen( HTTPDaemon, SOMAXCONN ) or die $!;
143
144     print "You can connect to your server at http://localhost:$port\n";
145
146     # Process
147     my %clean = %ENV;
148     for ( ; accept( Remote, HTTPDaemon ) ; close Remote ) {
149         *STDIN  = *Remote;
150         *STDOUT = *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';
159         my $chunk;
160
161         while ( sysread( STDIN, my $buff, 1 ) ) {
162             last if $buff eq "\n";
163             $chunk .= $buff;
164         }
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)$/ );
169         %ENV = %clean;
170
171         $chunk = '';
172         while ( sysread( STDIN, my $buff, 1 ) ) {
173             if ( $buff eq "\n" ) {
174                 $chunk =~ s/[\r\l\n\s]+$//;
175                 if ( $chunk =~ /^([\w\-]+): (.+)/i ) {
176                     my $tag = uc($1);
177                     $tag =~ s/^COOKIES$/COOKIE/;
178                     my $val = $2;
179                     $tag =~ s/-/_/g;
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 }
184                 }
185                 last if $chunk =~ /^$/;
186                 $chunk = '';
187             }
188             else { $chunk .= $buff }
189         }
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;
203     }
204 }
205
206 =head1 SEE ALSO
207
208 L<Catalyst>.
209
210 =head1 AUTHOR
211
212 Sebastian Riedel, C<sri@cpan.org>
213
214 =head1 COPYRIGHT
215
216 This program is free software, you can redistribute it and/or modify it under
217 the same terms as Perl itself.
218
219 =cut
220
221 1;