updated C::Test and documented changes
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / Test.pm
CommitLineData
e646f111 1package Catalyst::Engine::Test;
2
3use strict;
4use base 'Catalyst::Engine::CGI::NPH';
5
6use HTTP::Request;
7use HTTP::Response;
8use IO::File;
9use URI;
10
11=head1 NAME
12
13Catalyst::Engine::Test - Catalyst Test Engine
14
15=head1 SYNOPSIS
16
17See L<Catalyst>.
18
19=head1 DESCRIPTION
20
21This is the Catalyst engine specialized for testing.
22
23=head1 OVERLOADED METHODS
24
25This class overloads some methods from C<Catalyst::Engine::CGI::NPH>.
26
27=over 4
28
29=item $c->run
30
31=cut
32
33sub run {
34 my $class = shift;
35 my $request = shift || '/';
36
37 unless ( ref $request ) {
38 $request = URI->new( $request, 'http' );
39 }
40 unless ( ref $request eq 'HTTP::Request' ) {
41 $request = HTTP::Request->new( 'GET', $request );
42 }
43
44 local ( *STDIN, *STDOUT );
45
46 my %clean = %ENV;
47 my $output = '';
48 $ENV{CONTENT_TYPE} ||= $request->header('Content-Type') || '';
49 $ENV{CONTENT_LENGTH} ||= $request->header('Content-Length') || '';
50 $ENV{GATEWAY_INTERFACE} ||= 'CGI/1.1';
51 $ENV{HTTP_USER_AGENT} ||= 'Catalyst';
52 $ENV{HTTP_HOST} ||= $request->uri->host || 'localhost';
53 $ENV{QUERY_STRING} ||= $request->uri->query || '';
54 $ENV{REQUEST_METHOD} ||= $request->method;
55 $ENV{PATH_INFO} ||= $request->uri->path || '/';
56 $ENV{SCRIPT_NAME} ||= '/';
57 $ENV{SERVER_NAME} ||= $request->uri->host || 'localhost';
58 $ENV{SERVER_PORT} ||= $request->uri->port;
59 $ENV{SERVER_PROTOCOL} ||= 'HTTP/1.1';
60
61 for my $field ( $request->header_field_names ) {
62 if ( $field =~ /^Content-(Length|Type)$/ ) {
63 next;
64 }
65 $field =~ s/-/_/g;
66 $ENV{ 'HTTP_' . uc($field) } = $request->header($field);
67 }
68
69 if ( $request->content_length ) {
70 my $body = IO::File->new_tmpfile;
71 $body->print( $request->content ) or die $!;
72 $body->seek( 0, SEEK_SET ) or die $!;
73 open( STDIN, "<&=", $body->fileno )
74 or die("Failed to dup \$body: $!");
75 }
76
77 open( STDOUT, '>', \$output );
78 $class->handler;
79 %ENV = %clean;
80 return HTTP::Response->parse($output);
81}
82
83=back
84
85=head1 SEE ALSO
86
87L<Catalyst>.
88
89=head1 AUTHOR
90
91Sebastian Riedel, C<sri@cpan.org>
92Christian Hansen, C<ch@ngmedia.com>
93
94=head1 COPYRIGHT
95
96This program is free software, you can redistribute it and/or modify it under
97the same terms as Perl itself.
98
99=cut
100
1011;