Fixed multiple header bug
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Test.pm
index fc29985..3af0f15 100644 (file)
@@ -1,10 +1,31 @@
 package Catalyst::Test;
 
 use strict;
+use warnings;
+
+use Catalyst::Exception;
+use Catalyst::Utils;
 use UNIVERSAL::require;
+use HTTP::Headers;
 
 $ENV{CATALYST_ENGINE} = 'Test';
 
+# Bypass a HTTP::Headers bug
+{
+    no warnings 'redefine';
+
+    sub HTTP::Headers::new {
+        my $class = shift;
+        my $self = bless {}, $class;
+        if (@_) {
+            while ( my ( $field, $val ) = splice( @_, 0, 2 ) ) {
+                $self->push_header( $field, $val );
+            }
+        }
+        return $self;
+    }
+}
+
 =head1 NAME
 
 Catalyst::Test - Test Catalyst applications
@@ -20,19 +41,19 @@ Catalyst::Test - Test Catalyst applications
     get('index.html');
 
     # Run tests against a remote server
-    CATALYST_SERVER='http://localhost:3000/' prove -l lib/ t/
+    CATALYST_SERVER='http://localhost:3000/' prove -r -l lib/ t/
 
     # Tests with inline apps need to use Catalyst::Engine::Test
     package TestApp;
 
     use Catalyst qw[-Engine=Test];
 
-    __PACKAGE__->action(
-        foo => sub {
+    sub foo : Global {
             my ( $self, $c ) = @_;
             $c->res->output('bar');
-        }
-    );
+    }
+
+    __PACKAGE__->setup();
 
     package main;
 
@@ -47,17 +68,19 @@ Test Catalyst applications.
 
 =head2 METHODS
 
-=head3 get
+=over 4
+
+=item get
 
 Returns the content.
 
     my $content = get('foo/bar?test=1');
 
-=head3 request
+=item request
 
 Returns a C<HTTP::Response> object.
 
-    my $res =request('foo/bar?test=1');
+    my $res = request('foo/bar?test=1');
 
 =cut
 
@@ -74,11 +97,7 @@ sub import {
 
     else {
         $class->require;
-
-        unless ( $INC{'Test/Builder.pm'} ) {
-            die qq/Couldn't load "$class", "$@"/ if $@;
-        }
-
+        die if $@ && $@ !~ /^Can't locate /;
         $class->import;
 
         $request = sub { $class->run(@_) };
@@ -91,36 +110,47 @@ sub import {
     *{"$caller\::get"}     = $get;
 }
 
+my $agent;
+
+=item remote_request
+
+Do an actual remote request using LWP.
+
+=cut
+
 sub remote_request {
-    my $request = shift;
 
     require LWP::UserAgent;
 
+    my $request = Catalyst::Utils::request( shift(@_) );
+
     my $server = URI->new( $ENV{CATALYST_SERVER} );
 
-    unless ( ref $request ) {
+    if ( $server->path =~ m|^(.+)?/$| ) {
+        $server->path("$1");    # need to be quoted
+    }
 
-        my $uri =
-          ( $request =~ m/http/i )
-          ? URI->new($request)
-          : URI->new( 'http://localhost' . $request );
+    $request->uri->scheme( $server->scheme );
+    $request->uri->host( $server->host );
+    $request->uri->port( $server->port );
+    $request->uri->path( $server->path . $request->uri->path );
 
-        $request = $uri->canonical;
-    }
+    unless ($agent) {
 
-    $request->scheme( $server->scheme );
-    $request->host( $server->host );
-    $request->port( $server->port );
+        $agent = LWP::UserAgent->new(
+            keep_alive   => 1,
+            max_redirect => 0,
+            timeout      => 60,
+        );
 
-    unless ( ref $request eq 'HTTP::Request' ) {
-        $request = HTTP::Request->new( 'GET', $request );
+        $agent->env_proxy;
     }
 
-    my $agent = LWP::UserAgent->new;
-
     return $agent->request($request);
 }
 
+=back 
+
 =head1 SEE ALSO
 
 L<Catalyst>.