[Catalyst-commits] r7857 - in Catalyst-Runtime/5.70/trunk: . lib/Catalyst t t/lib/TestApp/Controller/Engine/Request

bricas at dev.catalyst.perl.org bricas at dev.catalyst.perl.org
Thu May 29 14:01:03 BST 2008


Author: bricas
Date: 2008-05-29 14:01:03 +0100 (Thu, 29 May 2008)
New Revision: 7857

Modified:
   Catalyst-Runtime/5.70/trunk/Changes
   Catalyst-Runtime/5.70/trunk/lib/Catalyst/Request.pm
   Catalyst-Runtime/5.70/trunk/t/lib/TestApp/Controller/Engine/Request/URI.pm
   Catalyst-Runtime/5.70/trunk/t/live_engine_request_uri.t
Log:
Added the ability to remove parameters in req->uri_with() by passing in an undef value (RT #34782)

Modified: Catalyst-Runtime/5.70/trunk/Changes
===================================================================
--- Catalyst-Runtime/5.70/trunk/Changes	2008-05-29 13:01:00 UTC (rev 7856)
+++ Catalyst-Runtime/5.70/trunk/Changes	2008-05-29 13:01:03 UTC (rev 7857)
@@ -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

Modified: Catalyst-Runtime/5.70/trunk/lib/Catalyst/Request.pm
===================================================================
--- Catalyst-Runtime/5.70/trunk/lib/Catalyst/Request.pm	2008-05-29 13:01:00 UTC (rev 7856)
+++ Catalyst-Runtime/5.70/trunk/lib/Catalyst/Request.pm	2008-05-29 13:01:03 UTC (rev 7857)
@@ -525,7 +525,8 @@
 =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 @@
     
     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 @@
         }
     };
     
-    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;
 }

Modified: Catalyst-Runtime/5.70/trunk/t/lib/TestApp/Controller/Engine/Request/URI.pm
===================================================================
--- Catalyst-Runtime/5.70/trunk/t/lib/TestApp/Controller/Engine/Request/URI.pm	2008-05-29 13:01:00 UTC (rev 7856)
+++ Catalyst-Runtime/5.70/trunk/t/lib/TestApp/Controller/Engine/Request/URI.pm	2008-05-29 13:01:03 UTC (rev 7857)
@@ -77,4 +77,26 @@
     $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;

Modified: Catalyst-Runtime/5.70/trunk/t/live_engine_request_uri.t
===================================================================
--- Catalyst-Runtime/5.70/trunk/t/live_engine_request_uri.t	2008-05-29 13:01:00 UTC (rev 7856)
+++ Catalyst-Runtime/5.70/trunk/t/live_engine_request_uri.t	2008-05-29 13:01:03 UTC (rev 7857)
@@ -1,12 +1,10 @@
-#!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 @@
     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' );
+}
+




More information about the Catalyst-commits mailing list