From: Brian Cassidy Date: Fri, 21 Apr 2006 18:29:33 +0000 (+0000) Subject: make uri_for unicode safe X-Git-Tag: 5.7099_04~623 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=5789a3d8e83afb0a0232d4f2a2617353497cd976 make uri_for unicode safe added utf8 tests for uri_for and uri_with --- diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index d756514..1026625 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -22,6 +22,7 @@ use Scalar::Util qw/weaken blessed/; use Tree::Simple qw/use_weak_refs/; use Tree::Simple::Visitor::FindByUID; use attributes; +use utf8; use Carp qw/croak/; __PACKAGE__->mk_accessors( @@ -854,6 +855,14 @@ sub uri_for { my $params = ( 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()" ); + } + utf8::encode( $_ ) for $isa_ref ? @$value : $value; + }; + # join args with '/', or a blank string my $args = ( scalar @args ? '/' . join( '/', @args ) : '' ); $args =~ s/^\/// unless $path; diff --git a/t/lib/TestApp/Controller/Engine/Request/URI.pm b/t/lib/TestApp/Controller/Engine/Request/URI.pm index cf0c408..97b2579 100644 --- a/t/lib/TestApp/Controller/Engine/Request/URI.pm +++ b/t/lib/TestApp/Controller/Engine/Request/URI.pm @@ -41,4 +41,15 @@ sub uri_with : Local { $c->forward('TestApp::View::Dump::Request'); } +sub uri_with_utf8 : Local { + my ( $self, $c ) = @_; + + # change the current uri + my $uri = $c->req->uri_with( { unicode => "\x{2620}" } ); + + $c->res->header( 'X-Catalyst-uri-with' => "$uri" ); + + $c->forward('TestApp::View::Dump::Request'); +} + 1; diff --git a/t/live_engine_request_uri.t b/t/live_engine_request_uri.t index 3bfaec4..dba622f 100644 --- a/t/live_engine_request_uri.t +++ b/t/live_engine_request_uri.t @@ -1,4 +1,4 @@ -#!perl +#!perl use strict; use warnings; @@ -6,7 +6,7 @@ use warnings; use FindBin; use lib "$FindBin::Bin/lib"; -use Test::More tests => 35; +use Test::More tests => 38; use Catalyst::Test 'TestApp'; use Catalyst::Request; @@ -83,3 +83,10 @@ my $creq; is( $response->header( 'X-Catalyst-Param-a' ), '1', 'param "a" ok' ); is( $response->header( 'X-Catalyst-Param-b' ), '1', 'param "b" ok' ); } + +# test that uri_with is utf8 safe +{ + ok( my $response = request("http://localhost/engine/request/uri/uri_with_utf8"), 'Request' ); + ok( $response->is_success, 'Response Successful 2xx' ); + like( $response->header( 'X-Catalyst-uri-with' ), qr/%E2%98%A0$/, 'uri_with ok' ); +} diff --git a/t/unit_core_uri_for.t b/t/unit_core_uri_for.t index f46e2fd..bd0690b 100644 --- a/t/unit_core_uri_for.t +++ b/t/unit_core_uri_for.t @@ -1,7 +1,7 @@ use strict; use warnings; -use Test::More tests => 8; +use Test::More tests => 9; use Test::MockObject; use URI; @@ -42,6 +42,13 @@ is( 'URI for undef action with query params' ); +# test with utf-8 +is( + Catalyst::uri_for( $context, 'quux', { param1 => "\x{2620}" } )->as_string, + 'http://127.0.0.1/foo/yada/quux?param1=%E2%98%A0', + 'URI for undef action with query params in unicode' +); + $request->mock( 'base', sub { URI->new('http://localhost:3000/') } ); $request->mock( 'match', sub { 'orderentry/contract' } ); is( @@ -60,3 +67,4 @@ is( is( Catalyst::uri_for( $context, '/bar/baz' )->as_string, 'http://127.0.0.1/bar/baz', 'URI with no base or match' ); } +