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_headers
91 $c->req->method( $c->http->request->method );
92 $c->req->headers( $c->http->request->headers );
95 =item $c->prepare_parameters
99 sub prepare_parameters {
103 my $request = $c->http->request;
105 push( @params, $request->uri->query_form );
107 if ( $request->content_type eq 'application/x-www-form-urlencoded' ) {
108 my $uri = URI->new('http:');
109 $uri->query( $request->content );
110 push( @params, $uri->query_form );
113 if ( $request->content_type eq 'multipart/form-data' ) {
115 for my $part ( $request->parts ) {
117 my $disposition = $part->header('Content-Disposition');
118 my %parameters = @{ ( split_header_words($disposition) )[0] };
120 if ( $parameters{filename} ) {
122 my $fh = IO::File->new_tmpfile;
123 $fh->write( $part->content ) or die $!;
124 $fh->seek( SEEK_SET, 0 ) or die $!;
126 $c->req->uploads->{ $parameters{filename} } = {
128 size => ( stat $fh )[7],
129 type => $part->content_type
132 push( @params, $parameters{filename}, $fh );
135 push( @params, $parameters{name}, $part->content );
140 my $parameters = $c->req->parameters;
142 while ( my ( $name, $value ) = splice( @params, 0, 2 ) ) {
144 if ( exists $parameters->{$name} ) {
145 for ( $parameters->{$name} ) {
146 $_ = [$_] unless ref($_) eq "ARRAY";
151 $parameters->{$name} = $value;
156 =item $c->prepare_path
165 my $scheme = $c->http->request->uri->scheme;
166 my $host = $c->http->request->uri->host;
167 my $port = $c->http->request->uri->port;
170 $base->scheme($scheme);
174 $base = $base->canonical->as_string;
177 my $path = $c->http->request->uri->path || '/';
178 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
181 $c->req->base($base);
182 $c->req->path($path);
185 =item $c->prepare_request($r)
189 sub prepare_request {
190 my ( $c, $http ) = @_;
194 =item $c->prepare_uploads
198 sub prepare_uploads {
208 my $request = shift || '/';
210 unless ( ref $request ) {
212 my $uri = ( $request =~ m/http/i )
214 : URI->new( 'http://localhost' . $request );
216 $request = $uri->canonical;
219 unless ( ref $request eq 'HTTP::Request' ) {
220 $request = HTTP::Request->new( 'GET', $request );
223 my $host = sprintf( '%s:%d', $request->uri->host, $request->uri->port );
224 $request->header( 'Host' => $host );
226 my $http = Catalyst::Engine::Test::HTTP->new(
227 address => '127.0.0.1',
228 hostname => 'localhost',
230 response => HTTP::Response->new
233 $http->response->date(time);
235 $class->handler($http);
237 return $http->response;
248 Sebastian Riedel, C<sri@cpan.org>
249 Christian Hansen, C<ch@ngmedia.com>
253 This program is free software, you can redistribute it and/or modify it under
254 the same terms as Perl itself.