X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FEngine%2FTest.pm;h=452cc2df8894e2ba3dd41975bf162231bfb89c92;hb=1cf1c56a0c68611a361dbb8c797891baf6d0974f;hp=df1b8c5aa20dd8759e73202236966646b700e49d;hpb=c9afa5fc4ed6c36afe6653d7d8fbb9909994c1a8;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Engine/Test.pm b/lib/Catalyst/Engine/Test.pm index df1b8c5..452cc2d 100644 --- a/lib/Catalyst/Engine/Test.pm +++ b/lib/Catalyst/Engine/Test.pm @@ -1,12 +1,11 @@ package Catalyst::Engine::Test; use strict; -use base 'Catalyst::Engine::CGI::NPH'; - -use HTTP::Request; +use base 'Catalyst::Engine::CGI'; +use Catalyst::Utils; use HTTP::Response; -use IO::File; -use URI; +use HTTP::Status; +use NEXT; =head1 NAME @@ -34,74 +33,102 @@ This is the Catalyst engine specialized for testing. =head1 OVERLOADED METHODS -This class overloads some methods from C. +This class overloads some methods from C. =over 4 -=item $c->run +=item finalize_headers =cut -sub run { - my $class = shift; - my $request = shift || '/'; +sub finalize_headers { + my ( $self, $c ) = @_; + my $protocol = $c->request->protocol; + my $status = $c->response->status; + my $message = status_message($status); + print "$protocol $status $message\n"; + $c->response->headers->date(time); + $self->NEXT::finalize_headers($c); +} - unless ( ref $request ) { - $request = URI->new( $request, 'http' ); - } - unless ( ref $request eq 'HTTP::Request' ) { - $request = HTTP::Request->new( 'GET', $request ); - } +=item $self->run($c) - local ( *STDIN, *STDOUT ); +=cut - my %clean = %ENV; - my $output = ''; - $ENV{CONTENT_TYPE} ||= $request->header('Content-Type') || ''; - $ENV{CONTENT_LENGTH} ||= $request->header('Content-Length') || ''; - $ENV{GATEWAY_INTERFACE} ||= 'CGI/1.1'; - $ENV{HTTP_USER_AGENT} ||= 'Catalyst'; - $ENV{HTTP_HOST} ||= $request->uri->host || 'localhost'; - $ENV{QUERY_STRING} ||= $request->uri->query || ''; - $ENV{REQUEST_METHOD} ||= $request->method; - $ENV{PATH_INFO} ||= $request->uri->path || '/'; - $ENV{SCRIPT_NAME} ||= '/'; - $ENV{SERVER_NAME} ||= $request->uri->host || 'localhost'; - $ENV{SERVER_PORT} ||= $request->uri->port; - $ENV{SERVER_PROTOCOL} ||= 'HTTP/1.1'; - - for my $field ( $request->header_field_names ) { - if ( $field =~ /^Content-(Length|Type)$/ ) { - next; +sub run { + my ( $self, $class, $request ) = @_; + + $request = Catalyst::Utils::request($request); + + $request->header( + 'Host' => sprintf( '%s:%d', $request->uri->host, $request->uri->port ) + ); + + # We emulate CGI + local %ENV = ( + PATH_INFO => $request->uri->path || '', + QUERY_STRING => $request->uri->query || '', + REMOTE_ADDR => '127.0.0.1', + REMOTE_HOST => 'localhost', + REQUEST_METHOD => $request->method, + SERVER_NAME => 'localhost', + SERVER_PORT => $request->uri->port, + SERVER_PROTOCOL => 'HTTP/1.1', + %ENV, + ); + + # Headers + for my $header ( $request->header_field_names ) { + my $name = uc $header; + $name = 'COOKIE' if $name eq 'COOKIES'; + $name =~ tr/-/_/; + $name = 'HTTP_' . $name + unless $name =~ m/\A(?:CONTENT_(?:LENGTH|TYPE)|COOKIE)\z/; + my $value = $request->header($header); + if ( exists $ENV{$name} ) { + $ENV{$name} .= "; $value"; + } + else { + $ENV{$name} = $value; } - $field =~ s/-/_/g; - $ENV{ 'HTTP_' . uc($field) } = $request->header($field); } - if ( $request->content_length ) { - my $body = IO::File->new_tmpfile; - $body->print( $request->content ) or die $!; - $body->seek( 0, SEEK_SET ) or die $!; - open( STDIN, "<&=", $body->fileno ) - or die("Failed to dup \$body: $!"); - } + # STDIN + local *STDIN; + my $input = $request->content; + open STDIN, '<', \$input; - open( STDOUT, '>', \$output ); - $class->handler; - %ENV = %clean; + # STDOUT + local *STDOUT; + my $output = ''; + open STDOUT, '>', \$output; + + # Process + $class->handle_request; + + # Response return HTTP::Response->parse($output); } +=item $self->read_chunk($c, $buffer, $length) + +=cut + +sub read_chunk { shift; shift; *STDIN->read(@_); } + =back =head1 SEE ALSO L. -=head1 AUTHOR +=head1 AUTHORS + +Sebastian Riedel, + +Christian Hansen, -Sebastian Riedel, C -Christian Hansen, C +Andy Grundman, =head1 COPYRIGHT