Commit | Line | Data |
e646f111 |
1 | package Catalyst::Engine::Test; |
2 | |
3 | use strict; |
4 | use base 'Catalyst::Engine::CGI::NPH'; |
5 | |
6 | use HTTP::Request; |
7 | use HTTP::Response; |
8 | use IO::File; |
9 | use URI; |
10 | |
11 | =head1 NAME |
12 | |
13 | Catalyst::Engine::Test - Catalyst Test Engine |
14 | |
15 | =head1 SYNOPSIS |
16 | |
17 | See L<Catalyst>. |
18 | |
19 | =head1 DESCRIPTION |
20 | |
21 | This is the Catalyst engine specialized for testing. |
22 | |
23 | =head1 OVERLOADED METHODS |
24 | |
25 | This class overloads some methods from C<Catalyst::Engine::CGI::NPH>. |
26 | |
27 | =over 4 |
28 | |
29 | =item $c->run |
30 | |
31 | =cut |
32 | |
33 | sub 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 | |
87 | L<Catalyst>. |
88 | |
89 | =head1 AUTHOR |
90 | |
91 | Sebastian Riedel, C<sri@cpan.org> |
92 | Christian Hansen, C<ch@ngmedia.com> |
93 | |
94 | =head1 COPYRIGHT |
95 | |
96 | This program is free software, you can redistribute it and/or modify it under |
97 | the same terms as Perl itself. |
98 | |
99 | =cut |
100 | |
101 | 1; |