From: John Napiorkowski Date: Thu, 1 May 2014 21:13:31 +0000 (-0500) Subject: more docs and test cases for ENV localization X-Git-Tag: 5.90064~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=4477b313f9ef93789650b63e5ce408fe65ca54b2 more docs and test cases for ENV localization --- diff --git a/lib/Catalyst/Utils.pm b/lib/Catalyst/Utils.pm index 3f594e6..ff4f53d 100644 --- a/lib/Catalyst/Utils.pm +++ b/lib/Catalyst/Utils.pm @@ -530,7 +530,7 @@ sub env_at_path_prefix { =head2 env_at_action -Localize C<$env> under the current controller path prefix: +Localize C<$env> under the current action namespace. package MyApp::Controller::User; @@ -543,11 +543,16 @@ Localize C<$env> under the current controller path prefix: my $env = $c->Catalyst::Utils::env_at_action; } -Assuming you have a requst like GET /user/name: +Assuming you have a request like GET /user/name: In the example case C<$env> will have PATH_INFO of '/' instead of '/user/name' and SCRIPT_NAME will now be '/user/name'. +Alternatively, assuming you have a requst like GET /user/name/foo: + +In this example case C<$env> will have PATH_INFO of '/foo' instead of +'/user/name/foo' and SCRIPT_NAME will now be '/user/name'. + This is probably a common case where you want to mount a PSGI application under an action but let the Args fall through to the PSGI app. @@ -575,7 +580,7 @@ sub env_at_action { =head2 env_at_request_uri -Localize C<$env> under the current controller path prefix: +Localize C<$env> under the current request URI: package MyApp::Controller::User; diff --git a/t/psgi_utils.t b/t/psgi_utils.t index 21f6004..9b60926 100644 --- a/t/psgi_utils.t +++ b/t/psgi_utils.t @@ -4,6 +4,32 @@ use strict; # Make it easier to mount PSGI apps under catalyst { + package MyApp::Controller::Docs; + + use base 'Catalyst::Controller'; + use Plack::Request; + use Catalyst::Utils; + + my $psgi_app = sub { + my $req = Plack::Request->new(shift); + return [200,[],[$req->path]]; + }; + + sub name :Local { + my ($self, $c) = @_; + my $env = $c->Catalyst::Utils::env_at_action; + $c->res->from_psgi_response( + $psgi_app->($env)); + + } + + sub name_args :Local Args(1) { + my ($self, $c, $arg) = @_; + my $env = $c->Catalyst::Utils::env_at_action; + $c->res->from_psgi_response( + $psgi_app->($env)); + } + package MyApp::Controller::User; use base 'Catalyst::Controller'; @@ -289,6 +315,26 @@ use Catalyst::Test 'MyApp'; is_deeply $c->req->args, [444]; } +{ + my ($res, $c) = ctx_request('/docs/name'); + is $c->action, 'docs/name'; + is $res->content, '/'; + is_deeply $c->req->args, []; +} + +{ + my ($res, $c) = ctx_request('/docs/name/111/222'); + is $c->action, 'docs/name'; + is $res->content, '/111/222'; + is_deeply $c->req->args, [111,222]; +} + +{ + my ($res, $c) = ctx_request('/docs/name_args/111'); + is $c->action, 'docs/name_args'; + is $res->content, '/111'; + is_deeply $c->req->args, [111]; +} done_testing();