143414e1b541b6b6f13462b2e5c32a3262cb75c2
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / Test.pm
1 package Catalyst::Engine::Test;
2
3 use strict;
4 use base 'Catalyst::Engine';
5
6 use Class::Struct ();
7 use HTTP::Headers::Util 'split_header_words';
8 use HTTP::Request;
9 use HTTP::Response;
10 use File::Temp;
11 use URI;
12
13 __PACKAGE__->mk_accessors(qw/http/);
14
15 Class::Struct::struct 'Catalyst::Engine::Test::HTTP' => {
16     request  => 'HTTP::Request',
17     response => 'HTTP::Response',
18     hostname => '$',
19     address  => '$'
20 };
21
22 =head1 NAME
23
24 Catalyst::Engine::Test - Catalyst Test Engine
25
26 =head1 SYNOPSIS
27
28 A script using the Catalyst::Engine::Test module might look like:
29
30     #!/usr/bin/perl -w
31
32     BEGIN { 
33        $ENV{CATALYST_ENGINE} = 'Test';
34     }
35
36     use strict;
37     use lib '/path/to/MyApp/lib';
38     use MyApp;
39
40     MyApp->run('/a/path');
41
42 =head1 DESCRIPTION
43
44 This is the Catalyst engine specialized for testing.
45
46 =head1 OVERLOADED METHODS
47
48 This class overloads some methods from C<Catalyst::Engine>.
49
50 =over 4
51
52 =item $c->finalize_headers
53
54 =cut
55
56 sub finalize_headers {
57     my $c = shift;
58
59     $c->http->response->code( $c->response->status );
60
61     for my $name ( $c->response->headers->header_field_names ) {
62         $c->http->response->push_header( $name => [ $c->response->header($name) ] );
63     }
64 }
65
66 =item $c->finalize_output
67
68 =cut
69
70 sub finalize_output {
71     my $c = shift;
72     $c->http->response->content( $c->response->output );
73 }
74
75 =item $c->prepare_connection
76
77 =cut
78
79 sub prepare_connection {
80     my $c = shift;
81     $c->req->hostname( $c->http->hostname );
82     $c->req->address( $c->http->address );
83 }
84
85 =item $c->prepare_headers
86
87 =cut
88
89 sub prepare_headers {
90     my $c = shift;
91     $c->req->method( $c->http->request->method );
92     $c->req->headers( $c->http->request->headers );
93 }
94
95 =item $c->prepare_parameters
96
97 =cut
98
99 sub prepare_parameters {
100     my $c = shift;
101
102     my ( @params, @uploads );
103
104     my $request = $c->http->request;
105
106     push( @params, $request->uri->query_form );
107
108     if ( $request->content_type eq 'application/x-www-form-urlencoded' ) {
109         my $uri = URI->new('http:');
110         $uri->query( $request->content );
111         push( @params, $uri->query_form );
112     }
113
114     if ( $request->content_type eq 'multipart/form-data' ) {
115
116         for my $part ( $request->parts ) {
117
118             my $disposition = $part->header('Content-Disposition');
119             my %parameters  = @{ ( split_header_words($disposition) )[0] };
120
121             if ( $parameters{filename} ) {
122
123                 my $fh = File::Temp->new( UNLINK => 0 );
124                 $fh->write( $part->content ) or die $!;
125                 $fh->flush or die $!;
126
127                 my $upload = Catalyst::Request::Upload->new(
128                     filename => $parameters{filename},
129                     size     => ( stat $fh )[7],
130                     tempname => $fh->filename,
131                     type     => $part->content_type
132                 );
133                 
134                 $fh->close;
135
136                 push( @uploads, $parameters{name}, $upload );
137                 push( @params,  $parameters{name}, $parameters{filename} );
138             }
139             else {
140                 push( @params, $parameters{name}, $part->content );
141             }
142         }
143     }
144     
145     $c->req->_assign_values( $c->req->parameters, \@params );
146     $c->req->_assign_values( $c->req->uploads, \@uploads );
147 }
148
149 =item $c->prepare_path
150
151 =cut
152
153 sub prepare_path {
154     my $c = shift;
155
156     my $base;
157     {
158         my $scheme = $c->http->request->uri->scheme;
159         my $host   = $c->http->request->uri->host;
160         my $port   = $c->http->request->uri->port;
161
162         $base = URI->new;
163         $base->scheme($scheme);
164         $base->host($host);
165         $base->port($port);
166
167         $base = $base->canonical->as_string;
168     }
169
170     my $path = $c->http->request->uri->path || '/';
171     $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
172     $path =~ s/^\///;
173
174     $c->req->base($base);
175     $c->req->path($path);
176 }
177
178 =item $c->prepare_request($r)
179
180 =cut
181
182 sub prepare_request {
183     my ( $c, $http ) = @_;
184     $c->http($http);
185 }
186
187 =item $c->prepare_uploads
188
189 =cut
190
191 sub prepare_uploads {
192     my $c = shift;
193 }
194
195 =item $c->run
196
197 =cut
198
199 sub run {
200     my $class   = shift;
201     my $request = shift || '/';
202
203     unless ( ref $request ) {
204
205         my $uri =
206           ( $request =~ m/http/i )
207           ? URI->new($request)
208           : URI->new( 'http://localhost' . $request );
209
210         $request = $uri->canonical;
211     }
212
213     unless ( ref $request eq 'HTTP::Request' ) {
214         $request = HTTP::Request->new( 'GET', $request );
215     }
216
217     my $host = sprintf( '%s:%d', $request->uri->host, $request->uri->port );
218     $request->header( 'Host' => $host );
219
220     my $http = Catalyst::Engine::Test::HTTP->new(
221         address  => '127.0.0.1',
222         hostname => 'localhost',
223         request  => $request,
224         response => HTTP::Response->new
225     );
226
227     $http->response->date(time);
228
229     $class->handler($http);
230
231     return $http->response;
232 }
233
234 =back
235
236 =head1 SEE ALSO
237
238 L<Catalyst>.
239
240 =head1 AUTHOR
241
242 Sebastian Riedel, C<sri@cpan.org>
243 Christian Hansen, C<ch@ngmedia.com>
244
245 =head1 COPYRIGHT
246
247 This program is free software, you can redistribute it and/or modify it under
248 the same terms as Perl itself.
249
250 =cut
251
252 1;