Dont use Catalyst::Test for handling remote apps (CATALYST_SERVER)
[catagits/Test-WWW-Mechanize-Catalyst.git] / lib / Test / WWW / Mechanize / Catalyst.pm
index 8910c44..358f33f 100644 (file)
@@ -75,12 +75,9 @@ sub _make_request {
     my ( $self, $request ) = @_;
 
     my $response = $self->_do_catalyst_request($request);
-    $response->header( 'Content-Base', $request->uri );
-    $response->request($request);
-    if ( $request->uri->as_string =~ m{^/} ) {
-        $request->uri(
-            URI->new( 'http://localhost:80/' . $request->uri->as_string ) );
-    }
+    $response->header( 'Content-Base', $response->request->uri )
+      unless $response->header('Content-Base');
+
     $self->cookie_jar->extract_cookies($response) if $self->cookie_jar;
 
     # fail tests under the Catalyst debug screen
@@ -140,7 +137,7 @@ sub _do_catalyst_request {
     $self->cookie_jar->add_cookie_header($request) if $self->cookie_jar;
 
     # Woe betide anyone who unsets CATALYST_SERVER
-    return Catalyst::Test::remote_request($request)
+    return $self->_do_remote_request($request)
       if $ENV{CATALYST_SERVER};
 
     # If there's no Host header, set one.
@@ -151,19 +148,74 @@ sub _do_catalyst_request {
 
       $request->header('Host', $host);
     }
-  
-    if ( $self->{allow_external} ) {
-        unless ( $request->uri->as_string =~ m{^/}
-            || $request->uri->host eq 'localhost' )
-        {
-            return $self->SUPER::_make_request($request);
-        }
-    }
-  
+    my $res = $self->_check_external_request($request);
+    return $res if $res;
+
     my @creds = $self->get_basic_credentials( "Basic", $uri );
     $request->authorization_basic( @creds ) if @creds;
 
-    return Catalyst::Test::local_request($self->{catalyst_app}, $request);
+    my $response =Catalyst::Test::local_request($self->{catalyst_app}, $request);
+
+    # LWP would normally do this, but we dont get down that far.
+    $response->request($request);
+
+    return $response
+}
+
+sub _check_external_request {
+    my ($self, $request) = @_;
+
+    # If there's no host then definatley not an external request.
+    $request->uri->can('host_port') or return;
+
+    if ( $self->allow_external && $request->uri->host_port ne 'localhost:80' ) {
+        return $self->SUPER::_make_request($request);
+    }
+    return undef;
+}
+
+sub _do_remote_request {
+    my ($self, $request) = @_;
+
+    my $res = $self->_check_external_request($request);
+    return $res if $res;
+
+    my $server  = URI->new( $ENV{CATALYST_SERVER} );
+
+    if ( $server->path =~ m|^(.+)?/$| ) {
+        my $path = $1;
+        $server->path("$path") if $path;    # need to be quoted
+    }
+
+    # the request path needs to be sanitised if $server is using a
+    # non-root path due to potential overlap between request path and
+    # response path.
+    if ($server->path) {
+        # If request path is '/', we have to add a trailing slash to the
+        # final request URI
+        my $add_trailing = $request->uri->path eq '/';
+        
+        my @sp = split '/', $server->path;
+        my @rp = split '/', $request->uri->path;
+        shift @sp;shift @rp; # leading /
+        if (@rp) {
+            foreach my $sp (@sp) {
+                $sp eq $rp[0] ? shift @rp : last
+            }
+        }
+        $request->uri->path(join '/', @rp);
+        
+        if ( $add_trailing ) {
+            $request->uri->path( $request->uri->path . '/' );
+        }
+    }
+
+    $request->uri->scheme( $server->scheme );
+    $request->uri->host( $server->host );
+    $request->uri->port( $server->port );
+    $request->uri->path( $server->path . $request->uri->path );
+    return $self->SUPER::_make_request($request);
 }
 
 sub import {