From: Brian Cassidy Date: Sat, 15 Jul 2006 04:17:36 +0000 (+0000) Subject: Fix to allow uri_for and uri_with to stringify non-array references X-Git-Tag: 5.7099_04~387 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=fbb513f774337d5b48768491f0479b2b24e7fc4c Fix to allow uri_for and uri_with to stringify non-array references --- diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 8c0fc7b..c3b59b9 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -902,11 +902,10 @@ sub uri_for { ( scalar @args && ref $args[$#args] eq 'HASH' ? pop @args : {} ); for my $value ( values %$params ) { - my $isa_ref = ref $value; - if( $isa_ref and $isa_ref ne 'ARRAY' ) { - croak( "Non-array reference ($isa_ref) passed to uri_for()" ); + for ( ref $value eq 'ARRAY' ? @$value : $value ) { + $_ = "$_"; + utf8::encode( $_ ); } - utf8::encode( $_ ) for grep { defined } $isa_ref ? @$value : $value; }; # join args with '/', or a blank string diff --git a/lib/Catalyst/Request.pm b/lib/Catalyst/Request.pm index 4f85d24..219a7c1 100644 --- a/lib/Catalyst/Request.pm +++ b/lib/Catalyst/Request.pm @@ -510,14 +510,14 @@ sub uri_with { my( $self, $args ) = @_; carp( 'No arguments passed to uri_with()' ) unless $args; - + for my $value ( values %$args ) { - my $isa_ref = ref $value; - if( $isa_ref and $isa_ref ne 'ARRAY' ) { - croak( "Non-array reference ($isa_ref) passed to uri_with()" ); + for ( ref $value eq 'ARRAY' ? @$value : $value ) { + $_ = "$_"; + utf8::encode( $_ ); } - utf8::encode( $_ ) for grep{ defined } $isa_ref ? @$value : $value; }; + my $uri = $self->uri->clone; $uri->query_form( { diff --git a/t/lib/TestApp/Controller/Engine/Request/URI.pm b/t/lib/TestApp/Controller/Engine/Request/URI.pm index 97b2579..100557e 100644 --- a/t/lib/TestApp/Controller/Engine/Request/URI.pm +++ b/t/lib/TestApp/Controller/Engine/Request/URI.pm @@ -41,6 +41,17 @@ sub uri_with : Local { $c->forward('TestApp::View::Dump::Request'); } +sub uri_with_object : Local { + my ( $self, $c ) = @_; + + my $uri = $c->req->uri_with( { a => $c->req->base } ); + my %query = $uri->query_form; + + $c->res->header( 'X-Catalyst-Param-a' => $query{ a } ); + + $c->forward('TestApp::View::Dump::Request'); +} + sub uri_with_utf8 : Local { my ( $self, $c ) = @_; diff --git a/t/live_engine_request_uri.t b/t/live_engine_request_uri.t index dba622f..9b5833e 100644 --- a/t/live_engine_request_uri.t +++ b/t/live_engine_request_uri.t @@ -6,7 +6,7 @@ use warnings; use FindBin; use lib "$FindBin::Bin/lib"; -use Test::More tests => 38; +use Test::More tests => 41; use Catalyst::Test 'TestApp'; use Catalyst::Request; @@ -84,6 +84,13 @@ my $creq; is( $response->header( 'X-Catalyst-Param-b' ), '1', 'param "b" ok' ); } +# test that uri_with replaces params (and preserves) +{ + ok( my $response = request('http://localhost/engine/request/uri/uri_with_object'), 'Request' ); + ok( $response->is_success, 'Response Successful 2xx' ); + is( $response->header( 'X-Catalyst-Param-a' ), 'http://localhost/', 'param "a" ok' ); +} + # test that uri_with is utf8 safe { ok( my $response = request("http://localhost/engine/request/uri/uri_with_utf8"), 'Request' ); diff --git a/t/unit_core_uri_for.t b/t/unit_core_uri_for.t index 2fb1421..dfeb95f 100644 --- a/t/unit_core_uri_for.t +++ b/t/unit_core_uri_for.t @@ -1,7 +1,7 @@ use strict; use warnings; -use Test::More tests => 9; +use Test::More tests => 10; use URI; use_ok('Catalyst'); @@ -50,6 +50,13 @@ is( 'URI for undef action with query params in unicode' ); +# test with object +is( + Catalyst::uri_for( $context, 'quux', { param1 => $request->base } )->as_string, + 'http://127.0.0.1/foo/yada/quux?param1=http%3A%2F%2F127.0.0.1%2Ffoo', + 'URI for undef action with query param as object' +); + $request->base( URI->new('http://localhost:3000/') ); $request->match( 'orderentry/contract' ); is(