docs and correct middleware order
[catagits/Catalyst-Runtime.git] / t / psgi_utils.t
CommitLineData
9c7b6768 1use warnings;
2use strict;
3
4# Make it easier to mount PSGI apps under catalyst
5
b5436c19 6my $psgi_app = sub {
7 my $req = Plack::Request->new(shift);
8 return [200,[],[$req->path]];
9};
10
9c7b6768 11{
4477b313 12 package MyApp::Controller::Docs;
13
14 use base 'Catalyst::Controller';
15 use Plack::Request;
16 use Catalyst::Utils;
17
4477b313 18 sub name :Local {
19 my ($self, $c) = @_;
20 my $env = $c->Catalyst::Utils::env_at_action;
21 $c->res->from_psgi_response(
22 $psgi_app->($env));
23
24 }
25
26 sub name_args :Local Args(1) {
27 my ($self, $c, $arg) = @_;
28 my $env = $c->Catalyst::Utils::env_at_action;
29 $c->res->from_psgi_response(
30 $psgi_app->($env));
31 }
32
9c7b6768 33 package MyApp::Controller::User;
34
35 use base 'Catalyst::Controller';
36 use Plack::Request;
37 use Catalyst::Utils;
38
9c7b6768 39 sub local_example :Local {
40 my ($self, $c) = @_;
41 my $env = $self->get_env($c);
42 $c->res->from_psgi_response(
43 $psgi_app->($env));
44 }
45
46 sub local_example_args1 :Local Args(1) {
47 my ($self, $c) = @_;
48 my $env = $self->get_env($c);
49 $c->res->from_psgi_response(
50 $psgi_app->($env));
51 }
52
53 sub path_example :Path('path-example') {
54 my ($self, $c) = @_;
55 my $env = $self->get_env($c);
56 $c->res->from_psgi_response(
57 $psgi_app->($env));
58 }
59
60 sub path_example_args1 :Path('path-example-args1') {
61 my ($self, $c) = @_;
62 my $env = $self->get_env($c);
63 $c->res->from_psgi_response(
64 $psgi_app->($env));
65 }
66
67 sub chained :Chained(/) PathPrefix CaptureArgs(0) { }
68
69 sub from_chain :Chained('chained') PathPart('') CaptureArgs(0) {}
70
71 sub end_chain :Chained('from_chain') PathPath(abc-123) Args(1)
72 {
73 my ($self, $c) = @_;
74 my $env = $self->get_env($c);
75 $c->res->from_psgi_response(
76 $psgi_app->($env));
77 }
78
79 sub get_env {
80 my ($self, $c) = @_;
81 if($c->req->query_parameters->{path_prefix}) {
82 return $c->Catalyst::Utils::env_at_path_prefix;
83 } elsif($c->req->query_parameters->{env_path}) {
84 return $c->Catalyst::Utils::env_at_action;
85 } elsif($c->req->query_parameters->{path}) {
86 return $c->Catalyst::Utils::env_at_request_uri;
87 } else {
88 return $c->req->env;
89 }
90 }
91
92 $INC{'MyApp/Controller/User.pm'} = __FILE__;
93
94 package MyApp;
95 use Catalyst;
96 MyApp->setup;
97
98}
99
100use Test::More;
101use Catalyst::Test 'MyApp';
102
103# BEGIN [user/local_example]
104{
105 my ($res, $c) = ctx_request('/user/local_example');
106 is $c->action, 'user/local_example';
107 is $res->content, '/user/local_example';
108 is_deeply $c->req->args, [];
109}
110
111{
112 my ($res, $c) = ctx_request('/user/local_example/111/222');
113 is $c->action, 'user/local_example';
114 is $res->content, '/user/local_example/111/222';
115 is_deeply $c->req->args, [111,222];
116}
117
118{
119 my ($res, $c) = ctx_request('/user/local_example?path_prefix=1');
120 is $c->action, 'user/local_example';
121 is $res->content, '/local_example';
122 is_deeply $c->req->args, [];
123}
124
125{
126 my ($res, $c) = ctx_request('/user/local_example/111/222?path_prefix=1');
127 is $c->action, 'user/local_example';
128 is $res->content, '/local_example/111/222';
129 is_deeply $c->req->args, [111,222];
130}
131
132{
133 my ($res, $c) = ctx_request('/user/local_example?env_path=1');
134 is $c->action, 'user/local_example';
135 is $res->content, '/';
136 is_deeply $c->req->args, [];
137}
138
139{
140 my ($res, $c) = ctx_request('/user/local_example/111/222?env_path=1');
141 is $c->action, 'user/local_example';
142 is $res->content, '/111/222';
143 is_deeply $c->req->args, [111,222];
144}
145
146{
147 my ($res, $c) = ctx_request('/user/local_example?path=1');
148 is $c->action, 'user/local_example';
149 is $res->content, '/';
150 is_deeply $c->req->args, [];
151}
152
153{
154 my ($res, $c) = ctx_request('/user/local_example/111/222?path=1');
155 is $c->action, 'user/local_example';
156 is $res->content, '/';
157 is_deeply $c->req->args, [111,222];
158}
159
160# END [user/local_example]
161
162# BEGIN [/user/local_example_args1/***/]
163
164{
165 my ($res, $c) = ctx_request('/user/local_example_args1/333');
166 is $c->action, 'user/local_example_args1';
167 is $res->content, '/user/local_example_args1/333';
168 is_deeply $c->req->args, [333];
169}
170
171{
172 my ($res, $c) = ctx_request('/user/local_example_args1/333?path_prefix=1');
173 is $c->action, 'user/local_example_args1';
174 is $res->content, '/local_example_args1/333';
175 is_deeply $c->req->args, [333];
176}
177
178{
179 my ($res, $c) = ctx_request('/user/local_example_args1/333?env_path=1');
180 is $c->action, 'user/local_example_args1';
181 is $res->content, '/333';
182 is_deeply $c->req->args, [333];
183}
184
185{
186 my ($res, $c) = ctx_request('/user/local_example_args1/333?path=1');
187 is $c->action, 'user/local_example_args1';
188 is $res->content, '/';
189 is_deeply $c->req->args, [333];
190}
191
192# END [/user/local_example_args1/***/]
193
194# BEGIN [/user/path-example]
195
196{
197 my ($res, $c) = ctx_request('/user/path-example');
198 is $c->action, 'user/path_example';
199 is $res->content, '/user/path-example';
200 is_deeply $c->req->args, [];
201}
202
203{
204 my ($res, $c) = ctx_request('/user/path-example?path_prefix=1');
205 is $c->action, 'user/path_example';
206 is $res->content, '/path-example';
207 is_deeply $c->req->args, [];
208}
209
210{
211 my ($res, $c) = ctx_request('/user/path-example?env_path=1');
212 is $c->action, 'user/path_example';
213 is $res->content, '/';
214 is_deeply $c->req->args, [];
215}
216
217{
218 my ($res, $c) = ctx_request('/user/path-example?path=1');
219 is $c->action, 'user/path_example';
220 is $res->content, '/';
221 is_deeply $c->req->args, [];
222}
223
224
225{
226 my ($res, $c) = ctx_request('/user/path-example/111/222');
227 is $c->action, 'user/path_example';
228 is $res->content, '/user/path-example/111/222';
229 is_deeply $c->req->args, [111,222];
230}
231
232{
233 my ($res, $c) = ctx_request('/user/path-example/111/222?path_prefix=1');
234 is $c->action, 'user/path_example';
235 is $res->content, '/path-example/111/222';
236 is_deeply $c->req->args, [111,222];
237}
238
239{
240 my ($res, $c) = ctx_request('/user/path-example/111/222?env_path=1');
241 is $c->action, 'user/path_example';
242 is $res->content, '/111/222';
243 is_deeply $c->req->args, [111,222];
244}
245
246{
247 my ($res, $c) = ctx_request('/user/path-example/111/222?path=1');
248 is $c->action, 'user/path_example';
249 is $res->content, '/';
250 is_deeply $c->req->args, [111,222];
251}
252
253{
254 my ($res, $c) = ctx_request('/user/path-example-args1/333');
255 is $c->action, 'user/path_example_args1';
256 is $res->content, '/user/path-example-args1/333';
257 is_deeply $c->req->args, [333];
258}
259
260{
261 my ($res, $c) = ctx_request('/user/path-example-args1/333?path_prefix=1');
262 is $c->action, 'user/path_example_args1';
263 is $res->content, '/path-example-args1/333';
264 is_deeply $c->req->args, [333];
265}
266
267{
268 my ($res, $c) = ctx_request('/user/path-example-args1/333?env_path=1');
269 is $c->action, 'user/path_example_args1';
270 is $res->content, '/333';
271 is_deeply $c->req->args, [333];
272}
273
274{
275 my ($res, $c) = ctx_request('/user/path-example-args1/333?path=1');
276 is $c->action, 'user/path_example_args1';
277 is $res->content, '/';
278 is_deeply $c->req->args, [333];
279}
280
281# Chaining test /user/end_chain/*
282#
283#
284
285{
286 my ($res, $c) = ctx_request('/user/end_chain/444');
287 is $c->action, 'user/end_chain';
288 is $res->content, '/user/end_chain/444';
289 is_deeply $c->req->args, [444];
290}
291
292{
293 my ($res, $c) = ctx_request('/user/end_chain/444?path_prefix=1');
294 is $c->action, 'user/end_chain';
295 is $res->content, '/end_chain/444';
296 is_deeply $c->req->args, [444];
297}
298
299{
300 my ($res, $c) = ctx_request('/user/end_chain/444?env_path=1');
301 is $c->action, 'user/end_chain';
302 is $res->content, '/444';
303 is_deeply $c->req->args, [444];
304}
305
306{
307 my ($res, $c) = ctx_request('/user/end_chain/444?path=1');
308 is $c->action, 'user/end_chain';
309 is $res->content, '/';
310 is_deeply $c->req->args, [444];
311}
312
4477b313 313{
314 my ($res, $c) = ctx_request('/docs/name');
315 is $c->action, 'docs/name';
316 is $res->content, '/';
317 is_deeply $c->req->args, [];
318}
319
320{
321 my ($res, $c) = ctx_request('/docs/name/111/222');
322 is $c->action, 'docs/name';
323 is $res->content, '/111/222';
324 is_deeply $c->req->args, [111,222];
325}
326
327{
328 my ($res, $c) = ctx_request('/docs/name_args/111');
329 is $c->action, 'docs/name_args';
330 is $res->content, '/111';
331 is_deeply $c->req->args, [111];
332}
9c7b6768 333
334done_testing();
335
336__END__
337
338
339use Plack::App::URLMap;
340use HTTP::Request::Common;
341use HTTP::Message::PSGI;
342
343my $urlmap = Plack::App::URLMap->new;
344
345my $app1 = sub {
346 my $env = shift;
347 return [200, [], [
348 "REQUEST_URI: $env->{REQUEST_URI}, FROM: $env->{MAP_TO}, PATH_INFO: $env->{PATH_INFO}, SCRIPT_NAME $env->{SCRIPT_NAME}"]];
349};
350
351$urlmap->map("/" => sub { my $env = shift; $env->{MAP_TO} = '/'; $app1->($env)});
352$urlmap->map("/foo" => sub { my $env = shift; $env->{MAP_TO} = '/foo'; $app1->($env)});
353$urlmap->map("/bar/baz" => sub { my $env = shift; $env->{MAP_TO} = '/foo/bar'; $app1->($env)});
354
355my $app = $urlmap->to_app;
356
357warn $app->(req_to_psgi(GET '/'))->[2]->[0];
358warn $app->(req_to_psgi(GET '/111'))->[2]->[0];
359warn $app->(req_to_psgi(GET '/foo'))->[2]->[0];
360warn $app->(req_to_psgi(GET '/foo/222'))->[2]->[0];
361warn $app->(req_to_psgi(GET '/bar/baz'))->[2]->[0];
362warn $app->(req_to_psgi(GET '/bar/baz/333'))->[2]->[0];
363