Made restart tests a bit more descriptive
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / Test.pm
CommitLineData
e646f111 1package Catalyst::Engine::Test;
2
3use strict;
fbcc39ad 4use base 'Catalyst::Engine::CGI';
4d60aa90 5use Catalyst::Utils;
fbcc39ad 6use HTTP::Response;
7use HTTP::Status;
8use NEXT;
d837e1a7 9
e646f111 10=head1 NAME
11
12Catalyst::Engine::Test - Catalyst Test Engine
13
14=head1 SYNOPSIS
15
c9afa5fc 16A script using the Catalyst::Engine::Test module might look like:
17
18 #!/usr/bin/perl -w
19
20 BEGIN {
21 $ENV{CATALYST_ENGINE} = 'Test';
22 }
23
24 use strict;
25 use lib '/path/to/MyApp/lib';
26 use MyApp;
27
28 MyApp->run('/a/path');
e646f111 29
30=head1 DESCRIPTION
31
32This is the Catalyst engine specialized for testing.
33
34=head1 OVERLOADED METHODS
35
fbcc39ad 36This class overloads some methods from C<Catalyst::Engine::CGI>.
e646f111 37
38=over 4
39
fbcc39ad 40=item finalize_headers
41
42=cut
43
44sub finalize_headers {
45 my ( $self, $c ) = @_;
46 my $protocol = $c->request->protocol;
47 my $status = $c->response->status;
48 my $message = status_message($status);
49 print "$protocol $status $message\n";
50 $c->response->headers->date(time);
51 $self->NEXT::finalize_headers($c);
52}
53
54=item $self->run($c)
e646f111 55
56=cut
57
58sub run {
fbcc39ad 59 my ( $self, $class, $request ) = @_;
60
4d60aa90 61 $request = Catalyst::Utils::request($request);
4716267f 62
fbcc39ad 63 # We emulate CGI
64 local %ENV = (
4f5ebacd 65 PATH_INFO => $request->uri->path || '',
66 QUERY_STRING => $request->uri->query || '',
67 REMOTE_ADDR => '127.0.0.1',
68 REMOTE_HOST => 'localhost',
fbcc39ad 69 REQUEST_METHOD => $request->method,
70 SERVER_NAME => 'localhost',
71 SERVER_PORT => $request->uri->port,
72 SERVER_PROTOCOL => 'HTTP/1.1',
73 %ENV,
45374ac6 74 );
e646f111 75
fbcc39ad 76 # Headers
77 for my $header ( $request->header_field_names ) {
78 my $name = uc $header;
79 $name = 'COOKIE' if $name eq 'COOKIES';
80 $name =~ tr/-/_/;
81 $name = 'HTTP_' . $name
4f5ebacd 82 unless $name =~ m/\A(?:CONTENT_(?:LENGTH|TYPE)|COOKIE)\z/;
fbcc39ad 83 my $value = $request->header($header);
84 if ( exists $ENV{$name} ) {
85 $ENV{$name} .= "; $value";
86 }
87 else {
88 $ENV{$name} = $value;
89 }
90 }
91
92 # STDIN
93 local *STDIN;
94 my $input = $request->content;
95 open STDIN, '<', \$input;
96
97 # STDOUT
98 local *STDOUT;
99 my $output = '';
100 open STDOUT, '>', \$output;
b26df351 101
fbcc39ad 102 # Process
103 $class->handle_request;
e646f111 104
fbcc39ad 105 # Response
106 return HTTP::Response->parse($output);
e646f111 107}
108
fbcc39ad 109=item $self->read_chunk($c, $buffer, $length)
110
111=cut
112
4f5ebacd 113sub read_chunk { shift; shift; *STDIN->read(@_); }
fbcc39ad 114
e646f111 115=back
116
117=head1 SEE ALSO
118
119L<Catalyst>.
120
fbcc39ad 121=head1 AUTHORS
122
123Sebastian Riedel, <sri@cpan.org>
124
125Christian Hansen, <ch@ngmedia.com>
e646f111 126
fbcc39ad 127Andy Grundman, <andy@hybridized.org>
e646f111 128
129=head1 COPYRIGHT
130
131This program is free software, you can redistribute it and/or modify it under
132the same terms as Perl itself.
133
134=cut
135
1361;