1 package Catalyst::Engine::Test;
4 use base 'Catalyst::Engine';
7 use HTTP::Headers::Util 'split_header_words';
13 __PACKAGE__->mk_accessors(qw/http/);
15 Class::Struct::struct 'Catalyst::Engine::Test::HTTP' => {
16 request => 'HTTP::Request',
17 response => 'HTTP::Response',
24 Catalyst::Engine::Test - Catalyst Test Engine
28 A script using the Catalyst::Engine::Test module might look like:
33 $ENV{CATALYST_ENGINE} = 'Test';
37 use lib '/path/to/MyApp/lib';
40 MyApp->run('/a/path');
44 This is the Catalyst engine specialized for testing.
46 =head1 OVERLOADED METHODS
48 This class overloads some methods from C<Catalyst::Engine>.
52 =item $c->finalize_headers
56 sub finalize_headers {
59 $c->http->response->code( $c->response->status );
61 for my $name ( $c->response->headers->header_field_names ) {
62 $c->http->response->push_header( $name => [ $c->response->header($name) ] );
66 =item $c->finalize_output
72 $c->http->response->content( $c->response->output );
75 =item $c->prepare_connection
79 sub prepare_connection {
81 $c->req->hostname( $c->http->hostname );
82 $c->req->address( $c->http->address );
85 =item $c->prepare_input
93 $c->request->content_length
94 and $c->request->content_type
95 and $c->request->content_type ne 'application/x-www-form-urlencoded'
96 and $c->request->content_type ne 'multipart/form-data';
98 $c->request->input( $c->http->request->content );
101 =item $c->prepare_headers
105 sub prepare_headers {
107 $c->req->method( $c->http->request->method );
108 $c->req->headers( $c->http->request->headers );
111 =item $c->prepare_parameters
115 sub prepare_parameters {
118 my ( @params, @uploads );
120 my $request = $c->http->request;
122 push( @params, $request->uri->query_form );
124 if ( $request->content_type eq 'application/x-www-form-urlencoded' ) {
125 my $uri = URI->new('http:');
126 $uri->query( $request->content );
127 push( @params, $uri->query_form );
130 if ( $request->content_type eq 'multipart/form-data' ) {
132 for my $part ( $request->parts ) {
134 my $disposition = $part->header('Content-Disposition');
135 my %parameters = @{ ( split_header_words($disposition) )[0] };
137 if ( $parameters{filename} ) {
139 my $fh = File::Temp->new( UNLINK => 0 );
140 $fh->write( $part->content ) or die $!;
141 $fh->flush or die $!;
143 my $upload = Catalyst::Request::Upload->new(
144 filename => $parameters{filename},
145 size => ( $fh->stat )[7],
146 tempname => $fh->filename,
147 type => $part->content_type
152 push( @uploads, $parameters{name}, $upload );
153 push( @params, $parameters{name}, $parameters{filename} );
156 push( @params, $parameters{name}, $part->content );
161 $c->req->_assign_values( $c->req->parameters, \@params );
162 $c->req->_assign_values( $c->req->uploads, \@uploads );
165 =item $c->prepare_path
174 my $scheme = $c->http->request->uri->scheme;
175 my $host = $c->http->request->uri->host;
176 my $port = $c->http->request->uri->port;
179 $base->scheme($scheme);
183 $base = $base->canonical->as_string;
186 my $path = $c->http->request->uri->path || '/';
187 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
190 $c->req->base($base);
191 $c->req->path($path);
194 =item $c->prepare_request($r)
198 sub prepare_request {
199 my ( $c, $http ) = @_;
203 =item $c->prepare_uploads
207 sub prepare_uploads {
217 my $request = shift || '/';
219 unless ( ref $request ) {
222 ( $request =~ m/http/i )
224 : URI->new( 'http://localhost' . $request );
226 $request = $uri->canonical;
229 unless ( ref $request eq 'HTTP::Request' ) {
230 $request = HTTP::Request->new( 'GET', $request );
233 my $host = sprintf( '%s:%d', $request->uri->host, $request->uri->port );
234 $request->header( 'Host' => $host );
236 my $http = Catalyst::Engine::Test::HTTP->new(
237 address => '127.0.0.1',
238 hostname => 'localhost',
240 response => HTTP::Response->new
243 $http->response->date(time);
245 $class->handler($http);
247 return $http->response;
258 Sebastian Riedel, C<sri@cpan.org>
259 Christian Hansen, C<ch@ngmedia.com>
263 This program is free software, you can redistribute it and/or modify it under
264 the same terms as Perl itself.