package Catalyst::Engine::Test;
use strict;
-use base 'Catalyst::Engine::CGI::NPH';
-
-use HTTP::Request;
-use HTTP::Response;
-use IO::File;
-use URI;
+use base 'Catalyst::Engine::HTTP::Base';
=head1 NAME
=head1 SYNOPSIS
-See L<Catalyst>.
+A script using the Catalyst::Engine::Test module might look like:
+
+ #!/usr/bin/perl -w
+
+ BEGIN {
+ $ENV{CATALYST_ENGINE} = 'Test';
+ }
+
+ use strict;
+ use lib '/path/to/MyApp/lib';
+ use MyApp;
+
+ MyApp->run('/a/path');
=head1 DESCRIPTION
=head1 OVERLOADED METHODS
-This class overloads some methods from C<Catalyst::Engine::CGI::NPH>.
+This class overloads some methods from C<Catalyst::Engine::HTTP::Base>.
=over 4
my $request = shift || '/';
unless ( ref $request ) {
- $request = URI->new( $request, 'http' );
+
+ my $uri =
+ ( $request =~ m/http/i )
+ ? URI->new($request)
+ : URI->new( 'http://localhost' . $request );
+
+ $request = $uri->canonical;
}
+
unless ( ref $request eq 'HTTP::Request' ) {
$request = HTTP::Request->new( 'GET', $request );
}
- local ( *STDIN, *STDOUT );
-
- 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;
- }
- $field =~ s/-/_/g;
- $ENV{ 'HTTP_' . uc($field) } = $request->header($field);
- }
+ my $host = sprintf( '%s:%d', $request->uri->host, $request->uri->port );
+ $request->header( 'Host' => $host );
- 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: $!");
- }
+ my $http = Catalyst::Engine::Test::HTTP->new(
+ address => '127.0.0.1',
+ hostname => 'localhost',
+ request => $request,
+ response => HTTP::Response->new
+ );
+
+ $http->response->date(time);
+
+ $class->handler($http);
- open( STDOUT, '>', \$output );
- $class->handler;
- %ENV = %clean;
- return HTTP::Response->parse($output);
+ return $http->response;
}
=back