Fix to allow uri_for and uri_with to stringify non-array references
Brian Cassidy [Sat, 15 Jul 2006 04:17:36 +0000 (04:17 +0000)]
lib/Catalyst.pm
lib/Catalyst/Request.pm
t/lib/TestApp/Controller/Engine/Request/URI.pm
t/live_engine_request_uri.t
t/unit_core_uri_for.t

index 8c0fc7b..c3b59b9 100644 (file)
@@ -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
index 4f85d24..219a7c1 100644 (file)
@@ -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( {
index 97b2579..100557e 100644 (file)
@@ -41,6 +41,17 @@ sub uri_with : Local {
     $c->forward('TestApp::View::Dump::Request');\r
 }\r
 \r
+sub uri_with_object : Local {\r
+    my ( $self, $c ) = @_;\r
+\r
+    my $uri   = $c->req->uri_with( { a => $c->req->base } );\r
+    my %query = $uri->query_form;\r
+    \r
+    $c->res->header( 'X-Catalyst-Param-a' => $query{ a } );\r
+    \r
+    $c->forward('TestApp::View::Dump::Request');\r
+}\r
+\r
 sub uri_with_utf8 : Local {\r
     my ( $self, $c ) = @_;\r
 \r
index dba622f..9b5833e 100644 (file)
@@ -6,7 +6,7 @@ use warnings;
 use FindBin;\r
 use lib "$FindBin::Bin/lib";\r
 \r
-use Test::More tests => 38;\r
+use Test::More tests => 41;\r
 use Catalyst::Test 'TestApp';\r
 use Catalyst::Request;\r
 \r
@@ -84,6 +84,13 @@ my $creq;
     is( $response->header( 'X-Catalyst-Param-b' ), '1', 'param "b" ok' );\r
 }\r
 \r
+# test that uri_with replaces params (and preserves)\r
+{\r
+    ok( my $response = request('http://localhost/engine/request/uri/uri_with_object'), 'Request' );\r
+    ok( $response->is_success, 'Response Successful 2xx' );\r
+    is( $response->header( 'X-Catalyst-Param-a' ), 'http://localhost/', 'param "a" ok' );\r
+}\r
+\r
 # test that uri_with is utf8 safe\r
 {\r
     ok( my $response = request("http://localhost/engine/request/uri/uri_with_utf8"), 'Request' );\r
index 2fb1421..dfeb95f 100644 (file)
@@ -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(