added $c->req->uri_with helper + tests
Brian Cassidy [Wed, 19 Apr 2006 22:09:18 +0000 (22:09 +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 b5a7ccb..3ad10bb 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,6 +1,7 @@
 This file documents the revision history for Perl extension Catalyst.
 
 5.67   
+       - Added $c->req->uri_with() helper
         - ConfigLoader: Updated to version 0.05
         - Fix up Engine to avoid a new 5.8.8 warning
         - Added app name with :: support for PAR
index 1340299..f9ff7b3 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 use base 'Class::Accessor::Fast';
 
 use IO::Socket qw[AF_INET inet_aton];
+use Carp;
 
 __PACKAGE__->mk_accessors(
     qw/action address arguments cookies headers match method
@@ -489,6 +490,27 @@ sub uploads {
 
 Returns a URI object for the current request. Stringifies to the URI text.
 
+=head2 $req->uri_with( { key => 'value' } );
+
+Returns a rewriten URI object for the current uri. Key/value pairs passed in
+will override existing parameters. Unmodified pairs will be preserved.
+
+=cut
+
+sub uri_with {
+    my( $self, $args ) = @_;
+    
+    carp( 'No arguments passed to uri_with()' ) unless $args;
+    
+    my $uri = $self->uri->clone;
+    
+    $uri->query_form( {
+        $uri->query_form,
+        %$args
+    } );
+    return $uri;
+}
+
 =head2 $req->user
 
 Returns the currently logged in user. Deprecated. The method recommended for
index cb86f30..cf0c408 100644 (file)
@@ -28,4 +28,17 @@ sub change_base : Local {
     $c->forward('TestApp::View::Dump::Request');\r
 }\r
 \r
+sub uri_with : Local {\r
+    my ( $self, $c ) = @_;\r
+\r
+    # change the current uri\r
+    my $uri   = $c->req->uri_with( { b => 1 } );\r
+    my %query = $uri->query_form;\r
+    \r
+    $c->res->header( 'X-Catalyst-Param-a' => $query{ a } );\r
+    $c->res->header( 'X-Catalyst-Param-b' => $query{ b } );\r
+    \r
+    $c->forward('TestApp::View::Dump::Request');\r
+}\r
+\r
 1;\r
index 5af97f9..3bfaec4 100644 (file)
@@ -6,7 +6,7 @@ use warnings;
 use FindBin;\r
 use lib "$FindBin::Bin/lib";\r
 \r
-use Test::More tests => 23;\r
+use Test::More tests => 35;\r
 use Catalyst::Test 'TestApp';\r
 use Catalyst::Request;\r
 \r
@@ -59,3 +59,27 @@ my $creq;
     is( $creq->{uri}->query, 'text=Catalyst%20Rocks', 'Query string ok' );\r
     is( $creq->{parameters}->{text}, 'Catalyst Rocks', 'Unescaped param ok' );\r
 }\r
+\r
+# test that uri_with adds params\r
+{\r
+    ok( my $response = request('http://localhost/engine/request/uri/uri_with'), 'Request' );\r
+    ok( $response->is_success, 'Response Successful 2xx' );\r
+    ok( !defined $response->header( 'X-Catalyst-Param-a' ), 'param "a" ok' );\r
+    is( $response->header( 'X-Catalyst-Param-b' ), '1', 'param "b" ok' );\r
+}\r
+\r
+# test that uri_with adds params (and preserves)\r
+{\r
+    ok( my $response = request('http://localhost/engine/request/uri/uri_with?a=1'), 'Request' );\r
+    ok( $response->is_success, 'Response Successful 2xx' );\r
+    is( $response->header( 'X-Catalyst-Param-a' ), '1', 'param "a" ok' );\r
+    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?a=1&b=2'), 'Request' );\r
+    ok( $response->is_success, 'Response Successful 2xx' );\r
+    is( $response->header( 'X-Catalyst-Param-a' ), '1', 'param "a" ok' );\r
+    is( $response->header( 'X-Catalyst-Param-b' ), '1', 'param "b" ok' );\r
+}\r