Commit | Line | Data |
9c7b6768 |
1 | use warnings; |
2 | use strict; |
3 | |
4 | # Make it easier to mount PSGI apps under catalyst |
5 | |
b5436c19 |
6 | my $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; |
09b86ef3 |
13 | $INC{'MyApp/Controller/Docs.pm'} = __FILE__; |
4477b313 |
14 | |
15 | use base 'Catalyst::Controller'; |
16 | use Plack::Request; |
17 | use Catalyst::Utils; |
18 | |
4477b313 |
19 | sub name :Local { |
20 | my ($self, $c) = @_; |
21 | my $env = $c->Catalyst::Utils::env_at_action; |
22 | $c->res->from_psgi_response( |
23 | $psgi_app->($env)); |
24 | |
25 | } |
26 | |
27 | sub name_args :Local Args(1) { |
28 | my ($self, $c, $arg) = @_; |
29 | my $env = $c->Catalyst::Utils::env_at_action; |
30 | $c->res->from_psgi_response( |
31 | $psgi_app->($env)); |
32 | } |
33 | |
9c7b6768 |
34 | package MyApp::Controller::User; |
09b86ef3 |
35 | $INC{'MyApp/Controller/User.pm'} = __FILE__; |
9c7b6768 |
36 | |
37 | use base 'Catalyst::Controller'; |
38 | use Plack::Request; |
39 | use Catalyst::Utils; |
40 | |
9c7b6768 |
41 | sub local_example :Local { |
42 | my ($self, $c) = @_; |
43 | my $env = $self->get_env($c); |
44 | $c->res->from_psgi_response( |
45 | $psgi_app->($env)); |
46 | } |
47 | |
48 | sub local_example_args1 :Local Args(1) { |
49 | my ($self, $c) = @_; |
50 | my $env = $self->get_env($c); |
51 | $c->res->from_psgi_response( |
52 | $psgi_app->($env)); |
53 | } |
54 | |
55 | sub path_example :Path('path-example') { |
56 | my ($self, $c) = @_; |
57 | my $env = $self->get_env($c); |
58 | $c->res->from_psgi_response( |
59 | $psgi_app->($env)); |
60 | } |
61 | |
62 | sub path_example_args1 :Path('path-example-args1') { |
63 | my ($self, $c) = @_; |
64 | my $env = $self->get_env($c); |
65 | $c->res->from_psgi_response( |
66 | $psgi_app->($env)); |
67 | } |
68 | |
69 | sub chained :Chained(/) PathPrefix CaptureArgs(0) { } |
70 | |
71 | sub from_chain :Chained('chained') PathPart('') CaptureArgs(0) {} |
72 | |
73 | sub end_chain :Chained('from_chain') PathPath(abc-123) Args(1) |
74 | { |
75 | my ($self, $c) = @_; |
76 | my $env = $self->get_env($c); |
77 | $c->res->from_psgi_response( |
78 | $psgi_app->($env)); |
79 | } |
80 | |
81 | sub get_env { |
82 | my ($self, $c) = @_; |
83 | if($c->req->query_parameters->{path_prefix}) { |
84 | return $c->Catalyst::Utils::env_at_path_prefix; |
85 | } elsif($c->req->query_parameters->{env_path}) { |
86 | return $c->Catalyst::Utils::env_at_action; |
87 | } elsif($c->req->query_parameters->{path}) { |
88 | return $c->Catalyst::Utils::env_at_request_uri; |
89 | } else { |
90 | return $c->req->env; |
91 | } |
92 | } |
93 | |
9c7b6768 |
94 | package MyApp; |
95 | use Catalyst; |
96 | MyApp->setup; |
97 | |
98 | } |
99 | |
100 | use Test::More; |
101 | use 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 | |
334 | done_testing(); |
335 | |
336 | __END__ |
337 | |
338 | |
339 | use Plack::App::URLMap; |
340 | use HTTP::Request::Common; |
341 | use HTTP::Message::PSGI; |
342 | |
343 | my $urlmap = Plack::App::URLMap->new; |
344 | |
345 | my $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 | |
355 | my $app = $urlmap->to_app; |
356 | |
357 | warn $app->(req_to_psgi(GET '/'))->[2]->[0]; |
358 | warn $app->(req_to_psgi(GET '/111'))->[2]->[0]; |
359 | warn $app->(req_to_psgi(GET '/foo'))->[2]->[0]; |
360 | warn $app->(req_to_psgi(GET '/foo/222'))->[2]->[0]; |
361 | warn $app->(req_to_psgi(GET '/bar/baz'))->[2]->[0]; |
362 | warn $app->(req_to_psgi(GET '/bar/baz/333'))->[2]->[0]; |
363 | |