X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fpsgi_utils.t;h=9c055596326b8da59ffdaf17728c732ebdfd6d08;hb=00038a21d88ab4f3620068c7b15e8f02c1b13e2d;hp=bbdb2ad89179b38d733c4995b4108167be77b8a5;hpb=d12acbe6bbfc0fbdfe8193dbb895e1ffb7995c9b;p=catagits%2FCatalyst-Runtime.git diff --git a/t/psgi_utils.t b/t/psgi_utils.t index bbdb2ad..9c05559 100644 --- a/t/psgi_utils.t +++ b/t/psgi_utils.t @@ -3,17 +3,52 @@ use strict; # Make it easier to mount PSGI apps under catalyst +my $psgi_app = sub { + my $req = Plack::Request->new(shift); + return [200,[],[$req->path]]; +}; + { - package MyApp::Controller::User; + package MyApp::PSGIObject; + + sub as_psgi { + return [200, ['Content-Type' => 'text/plain'], ['as_psgi']]; + }; + + package MyApp::Controller::Docs; + $INC{'MyApp/Controller/Docs.pm'} = __FILE__; 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 as_psgi :Local { + my ($self, $c) = @_; + my $as_psgi = bless +{}, 'MyApp::PSGIObject'; + $c->res->from_psgi_response($as_psgi); + } + + 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; + $INC{'MyApp/Controller/User.pm'} = __FILE__; + + use base 'Catalyst::Controller'; + use Plack::Request; + use Catalyst::Utils; sub local_example :Local { my ($self, $c) = @_; @@ -89,8 +124,6 @@ use strict; } } - $INC{'MyApp/Controller/User.pm'} = __FILE__; - package MyApp; use Catalyst; MyApp->setup; @@ -101,6 +134,11 @@ use Test::More; use Catalyst::Test 'MyApp'; { + my ($res, $c) = ctx_request('/docs/as_psgi'); + is $res->content, 'as_psgi'; +} + +{ my ($res, $c) = ctx_request('/user/mounted/111?path_prefix=1'); is $c->action, 'user/mounted'; is $res->content, 'http://localhost/user/user/local_example_args1/111'; @@ -324,34 +362,25 @@ 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, []; +} -done_testing(); - -__END__ - - -use Plack::App::URLMap; -use HTTP::Request::Common; -use HTTP::Message::PSGI; - -my $urlmap = Plack::App::URLMap->new; - -my $app1 = sub { - my $env = shift; - return [200, [], [ - "REQUEST_URI: $env->{REQUEST_URI}, FROM: $env->{MAP_TO}, PATH_INFO: $env->{PATH_INFO}, SCRIPT_NAME $env->{SCRIPT_NAME}"]]; -}; - -$urlmap->map("/" => sub { my $env = shift; $env->{MAP_TO} = '/'; $app1->($env)}); -$urlmap->map("/foo" => sub { my $env = shift; $env->{MAP_TO} = '/foo'; $app1->($env)}); -$urlmap->map("/bar/baz" => sub { my $env = shift; $env->{MAP_TO} = '/foo/bar'; $app1->($env)}); - -my $app = $urlmap->to_app; +{ + 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]; +} -warn $app->(req_to_psgi(GET '/'))->[2]->[0]; -warn $app->(req_to_psgi(GET '/111'))->[2]->[0]; -warn $app->(req_to_psgi(GET '/foo'))->[2]->[0]; -warn $app->(req_to_psgi(GET '/foo/222'))->[2]->[0]; -warn $app->(req_to_psgi(GET '/bar/baz'))->[2]->[0]; -warn $app->(req_to_psgi(GET '/bar/baz/333'))->[2]->[0]; +{ + 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();