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