4 # Make it easier to mount PSGI apps under catalyst
7 my $req = Plack::Request->new(shift);
8 return [200,[],[$req->path]];
12 package MyApp::PSGIObject;
15 return [200, ['Content-Type' => 'text/plain'], ['as_psgi']];
18 package MyApp::Controller::Docs;
19 $INC{'MyApp/Controller/Docs.pm'} = __FILE__;
21 use base 'Catalyst::Controller';
27 my $as_psgi = bless +{}, 'MyApp::PSGIObject';
28 $c->res->from_psgi_response($as_psgi);
33 my $env = $c->Catalyst::Utils::env_at_action;
34 $c->res->from_psgi_response(
39 sub name_args :Local Args(1) {
40 my ($self, $c, $arg) = @_;
41 my $env = $c->Catalyst::Utils::env_at_action;
42 $c->res->from_psgi_response(
46 sub filehandle :Local {
47 my ($self, $c, $arg) = @_;
48 my $path = File::Spec->catfile('t', 'utf8.txt');
49 open(my $fh, '<', $path) || die "trouble: $!";
50 $c->res->from_psgi_response([200, ['Content-Type'=>'text/html'], $fh]);
54 my ($self, $c, $arg) = @_;
55 $c->res->from_psgi_response([200, ['Content-Type'=>'text/html'], ["hello","world"]]);
58 sub streaming_body :Local {
62 my $writer = $respond->([200,["Content-Type" => "text/plain"]]);
63 $writer->write("body");
66 $c->res->from_psgi_response($psgi_app);
68 sub streaming_body_with_charset :Local {
72 my $writer = $respond->([200,["Content-Type" => "text/plain; charset=utf-8"]]);
73 $writer->write("body");
77 $c->res->from_psgi_response($psgi_app);
80 package MyApp::Controller::User;
81 $INC{'MyApp/Controller/User.pm'} = __FILE__;
83 use base 'Catalyst::Controller';
87 sub local_example :Local {
89 my $env = $self->get_env($c);
90 $c->res->from_psgi_response(
94 sub local_example_args1 :Local Args(1) {
96 my $env = $self->get_env($c);
97 $c->res->from_psgi_response(
101 sub path_example :Path('path-example') {
103 my $env = $self->get_env($c);
104 $c->res->from_psgi_response(
108 sub path_example_args1 :Path('path-example-args1') {
110 my $env = $self->get_env($c);
111 $c->res->from_psgi_response(
115 sub chained :Chained(/) PathPrefix CaptureArgs(0) { }
117 sub from_chain :Chained('chained') PathPart('') CaptureArgs(0) {}
119 sub end_chain :Chained('from_chain') PathPath(abc-123) Args(1)
122 my $env = $self->get_env($c);
123 $c->res->from_psgi_response(
127 sub mounted :Local Args(1) {
128 my ($self, $c, $arg) = @_;
129 our $app ||= ref($c)->psgi_app;
130 my $env = $self->get_env($c);
131 $c->res->from_psgi_response(
135 sub mount_arg :Path(/mounted) Arg(1) {
136 my ($self, $c, $arg) = @_;
137 my $uri = $c->uri_for( $self->action_for('local_example_args1'),$arg);
138 $c->res->body("$uri");
141 sub mount_noarg :Path(/mounted_no_arg) {
143 my $uri = $c->uri_for( $self->action_for('local_example_args1'),444);
144 $c->res->body("$uri");
150 if($c->req->query_parameters->{path_prefix}) {
151 return $c->Catalyst::Utils::env_at_path_prefix;
152 } elsif($c->req->query_parameters->{env_path}) {
153 return $c->Catalyst::Utils::env_at_action;
154 } elsif($c->req->query_parameters->{path}) {
155 return $c->Catalyst::Utils::env_at_request_uri;
168 use Catalyst::Test 'MyApp';
171 my ($res, $c) = ctx_request('/docs/as_psgi');
172 is $res->content, 'as_psgi';
176 my ($res, $c) = ctx_request('/user/mounted/111?path_prefix=1');
177 is $c->action, 'user/mounted';
178 is $res->content, 'http://localhost/user/user/local_example_args1/111';
179 is_deeply $c->req->args, [111];
183 my ($res, $c) = ctx_request('/user/mounted/mounted_no_arg?env_path=1');
184 is $c->action, 'user/mounted';
185 is $res->content, 'http://localhost/user/mounted/user/local_example_args1/444';
186 is_deeply $c->req->args, ['mounted_no_arg'];
189 # BEGIN [user/local_example]
191 my ($res, $c) = ctx_request('/user/local_example');
192 is $c->action, 'user/local_example';
193 is $res->content, '/user/local_example';
194 is_deeply $c->req->args, [];
198 my ($res, $c) = ctx_request('/user/local_example/111/222');
199 is $c->action, 'user/local_example';
200 is $res->content, '/user/local_example/111/222';
201 is_deeply $c->req->args, [111,222];
205 my ($res, $c) = ctx_request('/user/local_example?path_prefix=1');
206 is $c->action, 'user/local_example';
207 is $res->content, '/local_example';
208 is_deeply $c->req->args, [];
212 my ($res, $c) = ctx_request('/user/local_example/111/222?path_prefix=1');
213 is $c->action, 'user/local_example';
214 is $res->content, '/local_example/111/222';
215 is_deeply $c->req->args, [111,222];
219 my ($res, $c) = ctx_request('/user/local_example?env_path=1');
220 is $c->action, 'user/local_example';
221 is $res->content, '/';
222 is_deeply $c->req->args, [];
226 my ($res, $c) = ctx_request('/user/local_example/111/222?env_path=1');
227 is $c->action, 'user/local_example';
228 is $res->content, '/111/222';
229 is_deeply $c->req->args, [111,222];
233 my ($res, $c) = ctx_request('/user/local_example?path=1');
234 is $c->action, 'user/local_example';
235 is $res->content, '/';
236 is_deeply $c->req->args, [];
240 my ($res, $c) = ctx_request('/user/local_example/111/222?path=1');
241 is $c->action, 'user/local_example';
242 is $res->content, '/';
243 is_deeply $c->req->args, [111,222];
246 # END [user/local_example]
248 # BEGIN [/user/local_example_args1/***/]
251 my ($res, $c) = ctx_request('/user/local_example_args1/333');
252 is $c->action, 'user/local_example_args1';
253 is $res->content, '/user/local_example_args1/333';
254 is_deeply $c->req->args, [333];
258 my ($res, $c) = ctx_request('/user/local_example_args1/333?path_prefix=1');
259 is $c->action, 'user/local_example_args1';
260 is $res->content, '/local_example_args1/333';
261 is_deeply $c->req->args, [333];
265 my ($res, $c) = ctx_request('/user/local_example_args1/333?env_path=1');
266 is $c->action, 'user/local_example_args1';
267 is $res->content, '/333';
268 is_deeply $c->req->args, [333];
272 my ($res, $c) = ctx_request('/user/local_example_args1/333?path=1');
273 is $c->action, 'user/local_example_args1';
274 is $res->content, '/';
275 is_deeply $c->req->args, [333];
278 # END [/user/local_example_args1/***/]
280 # BEGIN [/user/path-example]
283 my ($res, $c) = ctx_request('/user/path-example');
284 is $c->action, 'user/path_example';
285 is $res->content, '/user/path-example';
286 is_deeply $c->req->args, [];
290 my ($res, $c) = ctx_request('/user/path-example?path_prefix=1');
291 is $c->action, 'user/path_example';
292 is $res->content, '/path-example';
293 is_deeply $c->req->args, [];
297 my ($res, $c) = ctx_request('/user/path-example?env_path=1');
298 is $c->action, 'user/path_example';
299 is $res->content, '/';
300 is_deeply $c->req->args, [];
304 my ($res, $c) = ctx_request('/user/path-example?path=1');
305 is $c->action, 'user/path_example';
306 is $res->content, '/';
307 is_deeply $c->req->args, [];
312 my ($res, $c) = ctx_request('/user/path-example/111/222');
313 is $c->action, 'user/path_example';
314 is $res->content, '/user/path-example/111/222';
315 is_deeply $c->req->args, [111,222];
319 my ($res, $c) = ctx_request('/user/path-example/111/222?path_prefix=1');
320 is $c->action, 'user/path_example';
321 is $res->content, '/path-example/111/222';
322 is_deeply $c->req->args, [111,222];
326 my ($res, $c) = ctx_request('/user/path-example/111/222?env_path=1');
327 is $c->action, 'user/path_example';
328 is $res->content, '/111/222';
329 is_deeply $c->req->args, [111,222];
333 my ($res, $c) = ctx_request('/user/path-example/111/222?path=1');
334 is $c->action, 'user/path_example';
335 is $res->content, '/';
336 is_deeply $c->req->args, [111,222];
340 my ($res, $c) = ctx_request('/user/path-example-args1/333');
341 is $c->action, 'user/path_example_args1';
342 is $res->content, '/user/path-example-args1/333';
343 is_deeply $c->req->args, [333];
347 my ($res, $c) = ctx_request('/user/path-example-args1/333?path_prefix=1');
348 is $c->action, 'user/path_example_args1';
349 is $res->content, '/path-example-args1/333';
350 is_deeply $c->req->args, [333];
354 my ($res, $c) = ctx_request('/user/path-example-args1/333?env_path=1');
355 is $c->action, 'user/path_example_args1';
356 is $res->content, '/333';
357 is_deeply $c->req->args, [333];
361 my ($res, $c) = ctx_request('/user/path-example-args1/333?path=1');
362 is $c->action, 'user/path_example_args1';
363 is $res->content, '/';
364 is_deeply $c->req->args, [333];
367 # Chaining test /user/end_chain/*
372 my ($res, $c) = ctx_request('/user/end_chain/444');
373 is $c->action, 'user/end_chain';
374 is $res->content, '/user/end_chain/444';
375 is_deeply $c->req->args, [444];
379 my ($res, $c) = ctx_request('/user/end_chain/444?path_prefix=1');
380 is $c->action, 'user/end_chain';
381 is $res->content, '/end_chain/444';
382 is_deeply $c->req->args, [444];
386 my ($res, $c) = ctx_request('/user/end_chain/444?env_path=1');
387 is $c->action, 'user/end_chain';
388 is $res->content, '/444';
389 is_deeply $c->req->args, [444];
393 my ($res, $c) = ctx_request('/user/end_chain/444?path=1');
394 is $c->action, 'user/end_chain';
395 is $res->content, '/';
396 is_deeply $c->req->args, [444];
400 my ($res, $c) = ctx_request('/docs/name');
401 is $c->action, 'docs/name';
402 is $res->content, '/';
403 is_deeply $c->req->args, [];
407 my ($res, $c) = ctx_request('/docs/name/111/222');
408 is $c->action, 'docs/name';
409 is $res->content, '/111/222';
410 is_deeply $c->req->args, [111,222];
414 my ($res, $c) = ctx_request('/docs/name_args/111');
415 is $c->action, 'docs/name_args';
416 is $res->content, '/111';
417 is_deeply $c->req->args, [111];
423 my ($res, $c) = ctx_request('/docs/filehandle');
424 is Encode::decode_utf8($res->content), "<p>This is stream_body_fh action ♥</p>\n";
428 my ($res, $c) = ctx_request('/docs/direct');
429 is $res->content, "helloworld";
433 my ($res, $c) = ctx_request('/docs/streaming_body');
434 is $res->content, "body";
437 my ($res, $c) = ctx_request('/docs/streaming_body_with_charset');
438 is $res->content, "body";