fix undef values in params sent to uri_with() and uri_for()
Brian Cassidy [Wed, 8 Nov 2006 15:13:03 +0000 (15:13 +0000)]
Changes
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

diff --git a/Changes b/Changes
index d4ea759..977ace6 100644 (file)
--- a/Changes
+++ b/Changes
@@ -2,6 +2,7 @@ This file documents the revision history for Perl extension Catalyst.
 
 5.7006
         - Updated HTTP::Body dependency to 0.6, 0.5 can break on large POST requests.
+        - Skip utf8 fix for undef values in uri_with() and uri_for()
 
 5.7005   2006-11-07 19:37:35
         - Fixed lighttpd tests to be properly skipped.
index 06343ac..5edff66 100644 (file)
@@ -925,6 +925,7 @@ sub uri_for {
       ( scalar @args && ref $args[$#args] eq 'HASH' ? pop @args : {} );
 
     for my $value ( values %$params ) {
+        next unless defined $value;
         for ( ref $value eq 'ARRAY' ? @$value : $value ) {
             $_ = "$_";
             utf8::encode( $_ );
index dca909c..7624db7 100644 (file)
@@ -516,6 +516,7 @@ sub uri_with {
     carp( 'No arguments passed to uri_with()' ) unless $args;
 
     for my $value ( values %$args ) {
+        next unless defined $value;
         for ( ref $value eq 'ARRAY' ? @$value : $value ) {
             $_ = "$_";
             utf8::encode( $_ );
index 100557e..03cf758 100644 (file)
@@ -63,4 +63,18 @@ sub uri_with_utf8 : Local {
     $c->forward('TestApp::View::Dump::Request');\r
 }\r
 \r
+sub uri_with_undef : Local {\r
+    my ( $self, $c ) = @_;\r
+
+    my $warnings = 0;
+    local $SIG{__WARN__} = sub { $warnings++ };\r
+\r
+    # change the current uri
+    my $uri = $c->req->uri_with( { foo => undef } );\r
+    \r
+    $c->res->header( 'X-Catalyst-warnings' => $warnings );\r
+    \r
+    $c->forward('TestApp::View::Dump::Request');\r
+}\r
+\r
 1;\r
index 5027b9c..943b276 100644 (file)
@@ -6,7 +6,7 @@ use warnings;
 use FindBin;\r
 use lib "$FindBin::Bin/lib";\r
 \r
-use Test::More tests => 41;\r
+use Test::More tests => 44;\r
 use Catalyst::Test 'TestApp';\r
 use Catalyst::Request;\r
 \r
@@ -97,3 +97,10 @@ my $creq;
     ok( $response->is_success, 'Response Successful 2xx' );\r
     like( $response->header( 'X-Catalyst-uri-with' ), qr/%E2%98%A0$/, 'uri_with ok' );\r
 }\r
+
+# test with undef -- no warnings should be thrown\r
+{\r
+    ok( my $response = request("http://localhost/engine/request/uri/uri_with_undef"), 'Request' );\r
+    ok( $response->is_success, 'Response Successful 2xx' );\r
+    is( $response->header( 'X-Catalyst-warnings' ), 0, 'no warnings emitted' );\r
+}\r
index dfeb95f..72ccda1 100644 (file)
@@ -1,7 +1,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 10;
+use Test::More tests => 11;
 use URI;
 
 use_ok('Catalyst');
@@ -74,3 +74,12 @@ is(
         'http://127.0.0.1/bar/baz', 'URI with no base or match' );
 }
 
+# test with undef -- no warnings should be thrown
+{
+    my $warnings = 0;
+    local $SIG{__WARN__} = sub { $warnings++ };
+
+    Catalyst::uri_for( $context, '/bar/baz', { foo => undef } )->as_string,
+    is( $warnings, 0, "no warnings emitted" );
+}
+