Commit | Line | Data |
9c7b6768 |
1 | use warnings; |
2 | use strict; |
3 | |
4 | # Make it easier to mount PSGI apps under catalyst |
5 | |
6 | { |
7 | package MyApp::Controller::User; |
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 local_example :Local { |
19 | my ($self, $c) = @_; |
20 | my $env = $self->get_env($c); |
21 | $c->res->from_psgi_response( |
22 | $psgi_app->($env)); |
23 | } |
24 | |
25 | sub local_example_args1 :Local Args(1) { |
26 | my ($self, $c) = @_; |
27 | my $env = $self->get_env($c); |
28 | $c->res->from_psgi_response( |
29 | $psgi_app->($env)); |
30 | } |
31 | |
32 | sub path_example :Path('path-example') { |
33 | my ($self, $c) = @_; |
34 | my $env = $self->get_env($c); |
35 | $c->res->from_psgi_response( |
36 | $psgi_app->($env)); |
37 | } |
38 | |
39 | sub path_example_args1 :Path('path-example-args1') { |
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 chained :Chained(/) PathPrefix CaptureArgs(0) { } |
47 | |
48 | sub from_chain :Chained('chained') PathPart('') CaptureArgs(0) {} |
49 | |
50 | sub end_chain :Chained('from_chain') PathPath(abc-123) Args(1) |
51 | { |
52 | my ($self, $c) = @_; |
53 | my $env = $self->get_env($c); |
54 | $c->res->from_psgi_response( |
55 | $psgi_app->($env)); |
56 | } |
d12acbe6 |
57 | |
58 | sub mounted :Local Args(1) { |
59 | my ($self, $c, $arg) = @_; |
60 | our $app ||= ref($c)->psgi_app; |
61 | my $env = $self->get_env($c); |
62 | $c->res->from_psgi_response( |
63 | $app->($env)); |
64 | } |
65 | |
66 | sub mount_arg :Path(/mounted) Arg(1) { |
67 | my ($self, $c, $arg) = @_; |
68 | my $uri = $c->uri_for( $self->action_for('local_example_args1'),$arg); |
69 | $c->res->body("$uri"); |
70 | } |
71 | |
72 | sub mount_noarg :Path(/mounted_no_arg) { |
73 | my ($self, $c) = @_; |
74 | my $uri = $c->uri_for( $self->action_for('local_example_args1'),444); |
75 | $c->res->body("$uri"); |
76 | } |
77 | |
9c7b6768 |
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 | |
100 | use Test::More; |
101 | use Catalyst::Test 'MyApp'; |
102 | |
d12acbe6 |
103 | { |
104 | my ($res, $c) = ctx_request('/user/mounted/111?path_prefix=1'); |
105 | is $c->action, 'user/mounted'; |
106 | is $res->content, 'http://localhost/user/user/local_example_args1/111'; |
107 | is_deeply $c->req->args, [111]; |
108 | } |
109 | |
110 | { |
111 | my ($res, $c) = ctx_request('/user/mounted/mounted_no_arg?env_path=1'); |
112 | is $c->action, 'user/mounted'; |
113 | is $res->content, 'http://localhost/user/mounted/user/local_example_args1/444'; |
114 | is_deeply $c->req->args, ['mounted_no_arg']; |
115 | } |
116 | |
9c7b6768 |
117 | # BEGIN [user/local_example] |
118 | { |
119 | my ($res, $c) = ctx_request('/user/local_example'); |
120 | is $c->action, 'user/local_example'; |
121 | is $res->content, '/user/local_example'; |
122 | is_deeply $c->req->args, []; |
123 | } |
124 | |
125 | { |
126 | my ($res, $c) = ctx_request('/user/local_example/111/222'); |
127 | is $c->action, 'user/local_example'; |
128 | is $res->content, '/user/local_example/111/222'; |
129 | is_deeply $c->req->args, [111,222]; |
130 | } |
131 | |
132 | { |
133 | my ($res, $c) = ctx_request('/user/local_example?path_prefix=1'); |
134 | is $c->action, 'user/local_example'; |
135 | is $res->content, '/local_example'; |
136 | is_deeply $c->req->args, []; |
137 | } |
138 | |
139 | { |
140 | my ($res, $c) = ctx_request('/user/local_example/111/222?path_prefix=1'); |
141 | is $c->action, 'user/local_example'; |
142 | is $res->content, '/local_example/111/222'; |
143 | is_deeply $c->req->args, [111,222]; |
144 | } |
145 | |
146 | { |
147 | my ($res, $c) = ctx_request('/user/local_example?env_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?env_path=1'); |
155 | is $c->action, 'user/local_example'; |
156 | is $res->content, '/111/222'; |
157 | is_deeply $c->req->args, [111,222]; |
158 | } |
159 | |
160 | { |
161 | my ($res, $c) = ctx_request('/user/local_example?path=1'); |
162 | is $c->action, 'user/local_example'; |
163 | is $res->content, '/'; |
164 | is_deeply $c->req->args, []; |
165 | } |
166 | |
167 | { |
168 | my ($res, $c) = ctx_request('/user/local_example/111/222?path=1'); |
169 | is $c->action, 'user/local_example'; |
170 | is $res->content, '/'; |
171 | is_deeply $c->req->args, [111,222]; |
172 | } |
173 | |
174 | # END [user/local_example] |
175 | |
176 | # BEGIN [/user/local_example_args1/***/] |
177 | |
178 | { |
179 | my ($res, $c) = ctx_request('/user/local_example_args1/333'); |
180 | is $c->action, 'user/local_example_args1'; |
181 | is $res->content, '/user/local_example_args1/333'; |
182 | is_deeply $c->req->args, [333]; |
183 | } |
184 | |
185 | { |
186 | my ($res, $c) = ctx_request('/user/local_example_args1/333?path_prefix=1'); |
187 | is $c->action, 'user/local_example_args1'; |
188 | is $res->content, '/local_example_args1/333'; |
189 | is_deeply $c->req->args, [333]; |
190 | } |
191 | |
192 | { |
193 | my ($res, $c) = ctx_request('/user/local_example_args1/333?env_path=1'); |
194 | is $c->action, 'user/local_example_args1'; |
195 | is $res->content, '/333'; |
196 | is_deeply $c->req->args, [333]; |
197 | } |
198 | |
199 | { |
200 | my ($res, $c) = ctx_request('/user/local_example_args1/333?path=1'); |
201 | is $c->action, 'user/local_example_args1'; |
202 | is $res->content, '/'; |
203 | is_deeply $c->req->args, [333]; |
204 | } |
205 | |
206 | # END [/user/local_example_args1/***/] |
207 | |
208 | # BEGIN [/user/path-example] |
209 | |
210 | { |
211 | my ($res, $c) = ctx_request('/user/path-example'); |
212 | is $c->action, 'user/path_example'; |
213 | is $res->content, '/user/path-example'; |
214 | is_deeply $c->req->args, []; |
215 | } |
216 | |
217 | { |
218 | my ($res, $c) = ctx_request('/user/path-example?path_prefix=1'); |
219 | is $c->action, 'user/path_example'; |
220 | is $res->content, '/path-example'; |
221 | is_deeply $c->req->args, []; |
222 | } |
223 | |
224 | { |
225 | my ($res, $c) = ctx_request('/user/path-example?env_path=1'); |
226 | is $c->action, 'user/path_example'; |
227 | is $res->content, '/'; |
228 | is_deeply $c->req->args, []; |
229 | } |
230 | |
231 | { |
232 | my ($res, $c) = ctx_request('/user/path-example?path=1'); |
233 | is $c->action, 'user/path_example'; |
234 | is $res->content, '/'; |
235 | is_deeply $c->req->args, []; |
236 | } |
237 | |
238 | |
239 | { |
240 | my ($res, $c) = ctx_request('/user/path-example/111/222'); |
241 | is $c->action, 'user/path_example'; |
242 | is $res->content, '/user/path-example/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_prefix=1'); |
248 | is $c->action, 'user/path_example'; |
249 | is $res->content, '/path-example/111/222'; |
250 | is_deeply $c->req->args, [111,222]; |
251 | } |
252 | |
253 | { |
254 | my ($res, $c) = ctx_request('/user/path-example/111/222?env_path=1'); |
255 | is $c->action, 'user/path_example'; |
256 | is $res->content, '/111/222'; |
257 | is_deeply $c->req->args, [111,222]; |
258 | } |
259 | |
260 | { |
261 | my ($res, $c) = ctx_request('/user/path-example/111/222?path=1'); |
262 | is $c->action, 'user/path_example'; |
263 | is $res->content, '/'; |
264 | is_deeply $c->req->args, [111,222]; |
265 | } |
266 | |
267 | { |
268 | my ($res, $c) = ctx_request('/user/path-example-args1/333'); |
269 | is $c->action, 'user/path_example_args1'; |
270 | is $res->content, '/user/path-example-args1/333'; |
271 | is_deeply $c->req->args, [333]; |
272 | } |
273 | |
274 | { |
275 | my ($res, $c) = ctx_request('/user/path-example-args1/333?path_prefix=1'); |
276 | is $c->action, 'user/path_example_args1'; |
277 | is $res->content, '/path-example-args1/333'; |
278 | is_deeply $c->req->args, [333]; |
279 | } |
280 | |
281 | { |
282 | my ($res, $c) = ctx_request('/user/path-example-args1/333?env_path=1'); |
283 | is $c->action, 'user/path_example_args1'; |
284 | is $res->content, '/333'; |
285 | is_deeply $c->req->args, [333]; |
286 | } |
287 | |
288 | { |
289 | my ($res, $c) = ctx_request('/user/path-example-args1/333?path=1'); |
290 | is $c->action, 'user/path_example_args1'; |
291 | is $res->content, '/'; |
292 | is_deeply $c->req->args, [333]; |
293 | } |
294 | |
295 | # Chaining test /user/end_chain/* |
296 | # |
297 | # |
298 | |
299 | { |
300 | my ($res, $c) = ctx_request('/user/end_chain/444'); |
301 | is $c->action, 'user/end_chain'; |
302 | is $res->content, '/user/end_chain/444'; |
303 | is_deeply $c->req->args, [444]; |
304 | } |
305 | |
306 | { |
307 | my ($res, $c) = ctx_request('/user/end_chain/444?path_prefix=1'); |
308 | is $c->action, 'user/end_chain'; |
309 | is $res->content, '/end_chain/444'; |
310 | is_deeply $c->req->args, [444]; |
311 | } |
312 | |
313 | { |
314 | my ($res, $c) = ctx_request('/user/end_chain/444?env_path=1'); |
315 | is $c->action, 'user/end_chain'; |
316 | is $res->content, '/444'; |
317 | is_deeply $c->req->args, [444]; |
318 | } |
319 | |
320 | { |
321 | my ($res, $c) = ctx_request('/user/end_chain/444?path=1'); |
322 | is $c->action, 'user/end_chain'; |
323 | is $res->content, '/'; |
324 | is_deeply $c->req->args, [444]; |
325 | } |
326 | |
327 | |
328 | done_testing(); |
329 | |
330 | __END__ |
331 | |
332 | |
333 | use Plack::App::URLMap; |
334 | use HTTP::Request::Common; |
335 | use HTTP::Message::PSGI; |
336 | |
337 | my $urlmap = Plack::App::URLMap->new; |
338 | |
339 | my $app1 = sub { |
340 | my $env = shift; |
341 | return [200, [], [ |
342 | "REQUEST_URI: $env->{REQUEST_URI}, FROM: $env->{MAP_TO}, PATH_INFO: $env->{PATH_INFO}, SCRIPT_NAME $env->{SCRIPT_NAME}"]]; |
343 | }; |
344 | |
345 | $urlmap->map("/" => sub { my $env = shift; $env->{MAP_TO} = '/'; $app1->($env)}); |
346 | $urlmap->map("/foo" => sub { my $env = shift; $env->{MAP_TO} = '/foo'; $app1->($env)}); |
347 | $urlmap->map("/bar/baz" => sub { my $env = shift; $env->{MAP_TO} = '/foo/bar'; $app1->($env)}); |
348 | |
349 | my $app = $urlmap->to_app; |
350 | |
351 | warn $app->(req_to_psgi(GET '/'))->[2]->[0]; |
352 | warn $app->(req_to_psgi(GET '/111'))->[2]->[0]; |
353 | warn $app->(req_to_psgi(GET '/foo'))->[2]->[0]; |
354 | warn $app->(req_to_psgi(GET '/foo/222'))->[2]->[0]; |
355 | warn $app->(req_to_psgi(GET '/bar/baz'))->[2]->[0]; |
356 | warn $app->(req_to_psgi(GET '/bar/baz/333'))->[2]->[0]; |
357 | |