Added the ability to remove parameters in req->uri_with() by passing in an undef...
Brian Cassidy [Thu, 29 May 2008 13:01:03 +0000 (13:01 +0000)]
Changes
lib/Catalyst/Request.pm
t/lib/TestApp/Controller/Engine/Request/URI.pm
t/live_engine_request_uri.t

diff --git a/Changes b/Changes
index b826322..593eed0 100644 (file)
--- a/Changes
+++ b/Changes
@@ -3,6 +3,8 @@
 5.7xxx  xxx
         - Get some of the optional_* tests working from dirs with spaces (RT #26455)
         - Fix Catalyst::Utils::home() when application .pm is in the current dir (RT #34437)
+        - Added the ability to remove parameters in req->uri_with() by passing in
+          an undef value (RT #34782)
 
 5.7014  2008-05-25 15:26:00
         - Addition of .conf in restart regex in Catalyst::Engine::HTTP::Restarter::Watcher
index 50886c0..9625636 100644 (file)
@@ -525,7 +525,8 @@ Returns a URI object for the current request. Stringifies to the URI text.
 =head2 $req->uri_with( { key => 'value' } );
 
 Returns a rewritten URI object for the current request. Key/value pairs
-passed in will override existing parameters. Unmodified pairs will be
+passed in will override existing parameters. You can remove an existing
+parameter by passing in an undef value. Unmodified pairs will be
 preserved.
 
 =cut
@@ -535,7 +536,7 @@ sub uri_with {
     
     carp( 'No arguments passed to uri_with()' ) unless $args;
 
-    for my $value ( values %$args ) {
+    foreach my $value ( values %$args ) {
         next unless defined $value;
         for ( ref $value eq 'ARRAY' ? @$value : $value ) {
             $_ = "$_";
@@ -543,11 +544,12 @@ sub uri_with {
         }
     };
     
-    my $uri = $self->uri->clone;
-    
+    my $uri   = $self->uri->clone;
+    my %query = ( %{ $uri->query_form_hash }, %$args );
+
     $uri->query_form( {
-        %{ $uri->query_form_hash },
-        %$args
+        # remove undef values
+        map { defined $query{ $_ } ? ( $_ => $query{ $_ } ) : () } keys %query
     } );
     return $uri;
 }
index b41c66a..44a2185 100644 (file)
@@ -77,4 +77,26 @@ sub uri_with_undef : Local {
     $c->forward('TestApp::View::Dump::Request');
 }
 
+sub uri_with_undef_only : Local {
+    my ( $self, $c ) = @_;
+
+    my $uri = $c->req->uri_with( { a => undef } );
+    
+    $c->res->header( 'X-Catalyst-uri-with' => "$uri" );
+    $c->forward('TestApp::View::Dump::Request');
+}
+
+sub uri_with_undef_ignore : Local {
+    my ( $self, $c ) = @_;
+
+    my $uri = $c->req->uri_with( { a => 1, b => undef } );
+    
+    my %query = $uri->query_form;
+    $c->res->header( 'X-Catalyst-uri-with' => "$uri" );
+    $c->res->header( 'X-Catalyst-Param-a' => $query{ a } );
+    $c->res->header( 'X-Catalyst-Param-b' => $query{ b } );
+    $c->res->header( 'X-Catalyst-Param-c' => $query{ c } );
+    $c->forward('TestApp::View::Dump::Request');
+}
+
 1;
index cecab6c..043a241 100644 (file)
@@ -1,12 +1,10 @@
-\feff#!perl
-
 use strict;
 use warnings;
 
 use FindBin;
 use lib "$FindBin::Bin/lib";
 
-use Test::More tests => 49;
+use Test::More tests => 66;
 use Catalyst::Test 'TestApp';
 use Catalyst::Request;
 
@@ -120,3 +118,36 @@ SKIP:
     is( $response->header( 'X-Catalyst-warnings' ), 0, 'no warnings emitted' );
 }
 
+# more tests with undef - should be ignored
+{
+    my $uri = "http://localhost/engine/request/uri/uri_with_undef_only";
+    ok( my $response = request($uri), 'Request' );
+    ok( $response->is_success, 'Response Successful 2xx' );
+    is( $response->header( 'X-Catalyst-uri-with' ), $uri, 'uri_with ok' );
+
+    # try with existing param
+    $uri = "$uri?x=1";
+    ok( $response = request($uri), 'Request' );
+    ok( $response->is_success, 'Response Successful 2xx' );
+    is( $response->header( 'X-Catalyst-uri-with' ), $uri, 'uri_with ok' );
+}
+
+{
+    my $uri = "http://localhost/engine/request/uri/uri_with_undef_ignore";
+    ok( my $response = request($uri), 'Request' );
+    ok( $response->is_success, 'Response Successful 2xx' );
+    is( $response->header( 'X-Catalyst-uri-with' ), "${uri}?a=1", 'uri_with ok' );
+
+    # remove an existing param
+    ok( $response = request("${uri}?b=1"), 'Request' );
+    ok( $response->is_success, 'Response Successful 2xx' );
+    is( $response->header( 'X-Catalyst-uri-with' ), "${uri}?a=1", 'uri_with ok' );
+
+    # remove an existing param, leave one, and add a new one
+    ok( $response = request("${uri}?b=1&c=1"), 'Request' );
+    ok( $response->is_success, 'Response Successful 2xx' );
+    is( $response->header( 'X-Catalyst-Param-a' ), '1', 'param "a" ok' );
+    ok( !defined $response->header( 'X-Catalyst-Param-b' ),'param "b" ok' );
+    is( $response->header( 'X-Catalyst-Param-c' ), '1', 'param "c" ok' );
+}
+