Added Catalyst::Request::Upload
[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
126                 my $upload = Catalyst::Request::Upload->new(
127                     filename => $parameters{filename},
128                     size     => ( stat $fh )[7],
129                     tempname => $fh->filename,
130                     type     => $part->content_type
131                 );
132                 
133                 $fh->close;
134
135                 push( @uploads, $parameters{name}, $upload );
136                 push( @params,  $parameters{name}, $fh );
137             }
138             else {
139                 push( @params, $parameters{name}, $part->content );
140             }
141         }
142     }
143     
144     $c->req->_assign_values( $c->req->parameters, \@params );
145     $c->req->_assign_values( $c->req->uploads, \@uploads );
146 }
147
148 =item $c->prepare_path
149
150 =cut
151
152 sub prepare_path {
153     my $c = shift;
154
155     my $base;
156     {
157         my $scheme = $c->http->request->uri->scheme;
158         my $host   = $c->http->request->uri->host;
159         my $port   = $c->http->request->uri->port;
160
161         $base = URI->new;
162         $base->scheme($scheme);
163         $base->host($host);
164         $base->port($port);
165
166         $base = $base->canonical->as_string;
167     }
168
169     my $path = $c->http->request->uri->path || '/';
170     $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
171     $path =~ s/^\///;
172
173     $c->req->base($base);
174     $c->req->path($path);
175 }
176
177 =item $c->prepare_request($r)
178
179 =cut
180
181 sub prepare_request {
182     my ( $c, $http ) = @_;
183     $c->http($http);
184 }
185
186 =item $c->prepare_uploads
187
188 =cut
189
190 sub prepare_uploads {
191     my $c = shift;
192 }
193
194 =item $c->run
195
196 =cut
197
198 sub run {
199     my $class   = shift;
200     my $request = shift || '/';
201
202     unless ( ref $request ) {
203
204         my $uri =
205           ( $request =~ m/http/i )
206           ? URI->new($request)
207           : URI->new( 'http://localhost' . $request );
208
209         $request = $uri->canonical;
210     }
211
212     unless ( ref $request eq 'HTTP::Request' ) {
213         $request = HTTP::Request->new( 'GET', $request );
214     }
215
216     my $host = sprintf( '%s:%d', $request->uri->host, $request->uri->port );
217     $request->header( 'Host' => $host );
218
219     my $http = Catalyst::Engine::Test::HTTP->new(
220         address  => '127.0.0.1',
221         hostname => 'localhost',
222         request  => $request,
223         response => HTTP::Response->new
224     );
225
226     $http->response->date(time);
227
228     $class->handler($http);
229
230     return $http->response;
231 }
232
233 =back
234
235 =head1 SEE ALSO
236
237 L<Catalyst>.
238
239 =head1 AUTHOR
240
241 Sebastian Riedel, C<sri@cpan.org>
242 Christian Hansen, C<ch@ngmedia.com>
243
244 =head1 COPYRIGHT
245
246 This program is free software, you can redistribute it and/or modify it under
247 the same terms as Perl itself.
248
249 =cut
250
251 1;