X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FTest%2FWWW%2FMechanize%2FCatalyst.pm;h=358f33f7bfbe48f09ac8e445883de3a7c769ce75;hb=97ae89ab316de422a55b3b3252ab4dbabfcdbdcd;hp=866a09f5aecfd42f3df4c666579220dc02777779;hpb=d6fc3a22e3ac5404cbf4bebfae7dab3e8073ad01;p=catagits%2FTest-WWW-Mechanize-Catalyst.git diff --git a/lib/Test/WWW/Mechanize/Catalyst.pm b/lib/Test/WWW/Mechanize/Catalyst.pm index 866a09f..358f33f 100644 --- a/lib/Test/WWW/Mechanize/Catalyst.pm +++ b/lib/Test/WWW/Mechanize/Catalyst.pm @@ -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 @@ -99,6 +96,7 @@ sub _make_request { # check if that was a redirect if ( $response->header('Location') + && $response->is_redirect && $self->redirect_ok( $request, $response ) ) { @@ -139,11 +137,9 @@ 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. unless ($request->header('Host')) { my $host = $self->has_host @@ -152,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 { @@ -452,6 +503,21 @@ Returns true value if the specified link was found and followed successfully. The HTTP::Response object returned by follow_link() is not available. +=head1 CAVEATS + +=head2 External Redirects and allow_external + +If you use non-fully qualified urls in your test scripts (i.e. anything without +a host, such as C<< ->get_ok( "/foo") >> ) and your app redirects to an +external URL, expect to be bitten once you come back to your application's urls +(it will try to request them on the remote server.) This is due to a limitation +in WWW::Mechanize. + +One workaround for this is that if you are expecting to redirect to an external +site, clone the TWMC obeject and use the cloned object for the external +redirect. + + =head1 SEE ALSO Related modules which may be of interest: L,