fixed warnings in tests
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / Test.pm
CommitLineData
e646f111 1package Catalyst::Engine::Test;
2
3use strict;
75fd617a 4use base 'Catalyst::Engine';
5
75fd617a 6use Class::Struct ();
7use HTTP::Headers::Util 'split_header_words';
8use HTTP::Request;
9use HTTP::Response;
e7c0c583 10use File::Temp;
75fd617a 11use URI;
12
6dc87a0f 13__PACKAGE__->mk_accessors(qw/http/);
75fd617a 14
6dc87a0f 15Class::Struct::struct 'Catalyst::Engine::Test::HTTP' => {
75fd617a 16 request => 'HTTP::Request',
17 response => 'HTTP::Response',
18 hostname => '$',
19 address => '$'
20};
e646f111 21
22=head1 NAME
23
24Catalyst::Engine::Test - Catalyst Test Engine
25
26=head1 SYNOPSIS
27
c9afa5fc 28A 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');
e646f111 41
42=head1 DESCRIPTION
43
44This is the Catalyst engine specialized for testing.
45
46=head1 OVERLOADED METHODS
47
75fd617a 48This class overloads some methods from C<Catalyst::Engine>.
e646f111 49
50=over 4
51
75fd617a 52=item $c->finalize_headers
53
54=cut
55
56sub finalize_headers {
57 my $c = shift;
58
6dc87a0f 59 $c->http->response->code( $c->response->status );
1a80619d 60
61 for my $name ( $c->response->headers->header_field_names ) {
6dc87a0f 62 $c->http->response->push_header( $name => [ $c->response->header($name) ] );
75fd617a 63 }
75fd617a 64}
65
66=item $c->finalize_output
67
68=cut
69
70sub finalize_output {
71 my $c = shift;
969647fd 72 $c->http->response->content( $c->response->output );
75fd617a 73}
74
75=item $c->prepare_connection
76
77=cut
78
79sub prepare_connection {
80 my $c = shift;
6dc87a0f 81 $c->req->hostname( $c->http->hostname );
82 $c->req->address( $c->http->address );
75fd617a 83}
84
85=item $c->prepare_headers
86
87=cut
88
89sub prepare_headers {
90 my $c = shift;
6dc87a0f 91 $c->req->method( $c->http->request->method );
92 $c->req->headers( $c->http->request->headers );
75fd617a 93}
94
95=item $c->prepare_parameters
96
97=cut
98
99sub prepare_parameters {
100 my $c = shift;
101
e7c0c583 102 my ( @params, @uploads );
103
6dc87a0f 104 my $request = $c->http->request;
75fd617a 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
146554c5 123 my $fh = File::Temp->new( UNLINK => 0 );
75fd617a 124 $fh->write( $part->content ) or die $!;
d3fced79 125 $fh->flush or die $!;
e7c0c583 126
146554c5 127 my $upload = Catalyst::Request::Upload->new(
e7c0c583 128 filename => $parameters{filename},
c462faf0 129 size => ( $fh->stat )[7],
e7c0c583 130 tempname => $fh->filename,
131 type => $part->content_type
146554c5 132 );
133
134 $fh->close;
75fd617a 135
e7c0c583 136 push( @uploads, $parameters{name}, $upload );
d3fced79 137 push( @params, $parameters{name}, $parameters{filename} );
75fd617a 138 }
139 else {
140 push( @params, $parameters{name}, $part->content );
141 }
142 }
143 }
e7c0c583 144
145 $c->req->_assign_values( $c->req->parameters, \@params );
146 $c->req->_assign_values( $c->req->uploads, \@uploads );
75fd617a 147}
148
149=item $c->prepare_path
150
151=cut
152
153sub prepare_path {
154 my $c = shift;
155
156 my $base;
157 {
6dc87a0f 158 my $scheme = $c->http->request->uri->scheme;
159 my $host = $c->http->request->uri->host;
160 my $port = $c->http->request->uri->port;
75fd617a 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
6dc87a0f 170 my $path = $c->http->request->uri->path || '/';
4b19b4f3 171 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
75fd617a 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
182sub prepare_request {
6dc87a0f 183 my ( $c, $http ) = @_;
184 $c->http($http);
75fd617a 185}
186
187=item $c->prepare_uploads
188
189=cut
190
191sub prepare_uploads {
192 my $c = shift;
193}
194
e646f111 195=item $c->run
196
197=cut
198
199sub run {
200 my $class = shift;
201 my $request = shift || '/';
202
203 unless ( ref $request ) {
45374ac6 204
e7c0c583 205 my $uri =
206 ( $request =~ m/http/i )
45374ac6 207 ? URI->new($request)
208 : URI->new( 'http://localhost' . $request );
209
210 $request = $uri->canonical;
e646f111 211 }
45374ac6 212
e646f111 213 unless ( ref $request eq 'HTTP::Request' ) {
214 $request = HTTP::Request->new( 'GET', $request );
215 }
216
4716267f 217 my $host = sprintf( '%s:%d', $request->uri->host, $request->uri->port );
218 $request->header( 'Host' => $host );
219
6dc87a0f 220 my $http = Catalyst::Engine::Test::HTTP->new(
45374ac6 221 address => '127.0.0.1',
1a80619d 222 hostname => 'localhost',
223 request => $request,
224 response => HTTP::Response->new
45374ac6 225 );
e646f111 226
6dc87a0f 227 $http->response->date(time);
b26df351 228
6dc87a0f 229 $class->handler($http);
e646f111 230
6dc87a0f 231 return $http->response;
e646f111 232}
233
234=back
235
236=head1 SEE ALSO
237
238L<Catalyst>.
239
240=head1 AUTHOR
241
242Sebastian Riedel, C<sri@cpan.org>
243Christian Hansen, C<ch@ngmedia.com>
244
245=head1 COPYRIGHT
246
247This program is free software, you can redistribute it and/or modify it under
248the same terms as Perl itself.
249
250=cut
251
2521;