to_app alias
[catagits/Catalyst-Runtime.git] / t / psgi_utils.t
1 use warnings;
2 use strict;
3
4 # Make it easier to mount PSGI apps under catalyst
5
6 my $psgi_app = sub {
7   my $req = Plack::Request->new(shift);
8   return [200,[],[$req->path]];
9 };
10
11 {
12   package MyApp::Controller::Docs;
13   $INC{'MyApp/Controller/Docs.pm'} = __FILE__;
14
15   use base 'Catalyst::Controller';
16   use Plack::Request;
17   use Catalyst::Utils;
18
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
34   package MyApp::Controller::User;
35   $INC{'MyApp/Controller/User.pm'} = __FILE__;
36
37   use base 'Catalyst::Controller';
38   use Plack::Request;
39   use Catalyst::Utils;
40
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 mounted :Local Args(1) {
82     my ($self, $c, $arg) = @_;
83     our $app ||= ref($c)->psgi_app;
84     my $env = $self->get_env($c);
85     $c->res->from_psgi_response(
86       $app->($env));
87   }
88
89   sub mount_arg :Path(/mounted) Arg(1) {
90     my ($self, $c, $arg) = @_;
91     my $uri =  $c->uri_for( $self->action_for('local_example_args1'),$arg);
92     $c->res->body("$uri");
93   }
94
95   sub mount_noarg :Path(/mounted_no_arg)  {
96     my ($self, $c) = @_;
97     my $uri =  $c->uri_for( $self->action_for('local_example_args1'),444);
98     $c->res->body("$uri");
99   }
100
101   
102   sub get_env {
103     my ($self, $c) = @_;
104     if($c->req->query_parameters->{path_prefix}) {
105       return $c->Catalyst::Utils::env_at_path_prefix;
106     } elsif($c->req->query_parameters->{env_path}) {
107       return $c->Catalyst::Utils::env_at_action;
108     } elsif($c->req->query_parameters->{path}) {
109       return $c->Catalyst::Utils::env_at_request_uri;
110     } else {
111       return $c->req->env;
112     }
113   }
114
115   package MyApp;
116   use Catalyst;
117   MyApp->setup;
118
119 }
120
121 use Test::More;
122 use Catalyst::Test 'MyApp';
123
124 {
125   my ($res, $c) = ctx_request('/user/mounted/111?path_prefix=1');
126   is $c->action, 'user/mounted';
127   is $res->content, 'http://localhost/user/user/local_example_args1/111';
128   is_deeply $c->req->args, [111];
129 }
130
131 {
132   my ($res, $c) = ctx_request('/user/mounted/mounted_no_arg?env_path=1');
133   is $c->action, 'user/mounted';
134   is $res->content, 'http://localhost/user/mounted/user/local_example_args1/444';
135   is_deeply $c->req->args, ['mounted_no_arg'];
136 }
137
138 # BEGIN [user/local_example]
139 {
140   my ($res, $c) = ctx_request('/user/local_example');
141   is $c->action, 'user/local_example';
142   is $res->content, '/user/local_example';
143   is_deeply $c->req->args, [];
144 }
145
146 {
147   my ($res, $c) = ctx_request('/user/local_example/111/222');
148   is $c->action, 'user/local_example';
149   is $res->content, '/user/local_example/111/222';
150   is_deeply $c->req->args, [111,222];
151 }
152
153 {
154   my ($res, $c) = ctx_request('/user/local_example?path_prefix=1');
155   is $c->action, 'user/local_example';
156   is $res->content, '/local_example';
157   is_deeply $c->req->args, [];
158 }
159
160 {
161   my ($res, $c) = ctx_request('/user/local_example/111/222?path_prefix=1');
162   is $c->action, 'user/local_example';
163   is $res->content, '/local_example/111/222';
164   is_deeply $c->req->args, [111,222];
165 }
166
167 {
168   my ($res, $c) = ctx_request('/user/local_example?env_path=1');
169   is $c->action, 'user/local_example';
170   is $res->content, '/';
171   is_deeply $c->req->args, [];
172 }
173
174 {
175   my ($res, $c) = ctx_request('/user/local_example/111/222?env_path=1');
176   is $c->action, 'user/local_example';
177   is $res->content, '/111/222';
178   is_deeply $c->req->args, [111,222];
179 }
180
181 {
182   my ($res, $c) = ctx_request('/user/local_example?path=1');
183   is $c->action, 'user/local_example';
184   is $res->content, '/';
185   is_deeply $c->req->args, [];
186 }
187
188 {
189   my ($res, $c) = ctx_request('/user/local_example/111/222?path=1');
190   is $c->action, 'user/local_example';
191   is $res->content, '/';
192   is_deeply $c->req->args, [111,222];
193 }
194
195 # END [user/local_example]
196
197 # BEGIN [/user/local_example_args1/***/]
198
199 {
200   my ($res, $c) = ctx_request('/user/local_example_args1/333');
201   is $c->action, 'user/local_example_args1';
202   is $res->content, '/user/local_example_args1/333';
203   is_deeply $c->req->args, [333];
204 }
205
206 {
207   my ($res, $c) = ctx_request('/user/local_example_args1/333?path_prefix=1');
208   is $c->action, 'user/local_example_args1';
209   is $res->content, '/local_example_args1/333';
210   is_deeply $c->req->args, [333];
211 }
212
213 {
214   my ($res, $c) = ctx_request('/user/local_example_args1/333?env_path=1');
215   is $c->action, 'user/local_example_args1';
216   is $res->content, '/333';
217   is_deeply $c->req->args, [333];
218 }
219
220 {
221   my ($res, $c) = ctx_request('/user/local_example_args1/333?path=1');
222   is $c->action, 'user/local_example_args1';
223   is $res->content, '/';
224   is_deeply $c->req->args, [333];
225 }
226
227 # END [/user/local_example_args1/***/]
228
229 # BEGIN [/user/path-example] 
230
231 {
232   my ($res, $c) = ctx_request('/user/path-example');
233   is $c->action, 'user/path_example';
234   is $res->content, '/user/path-example';
235   is_deeply $c->req->args, [];
236 }
237
238 {
239   my ($res, $c) = ctx_request('/user/path-example?path_prefix=1');
240   is $c->action, 'user/path_example';
241   is $res->content, '/path-example';
242   is_deeply $c->req->args, [];
243 }
244
245 {
246   my ($res, $c) = ctx_request('/user/path-example?env_path=1');
247   is $c->action, 'user/path_example';
248   is $res->content, '/';
249   is_deeply $c->req->args, [];
250 }
251
252 {
253   my ($res, $c) = ctx_request('/user/path-example?path=1');
254   is $c->action, 'user/path_example';
255   is $res->content, '/';
256   is_deeply $c->req->args, [];
257 }
258
259
260 {
261   my ($res, $c) = ctx_request('/user/path-example/111/222');
262   is $c->action, 'user/path_example';
263   is $res->content, '/user/path-example/111/222';
264   is_deeply $c->req->args, [111,222];
265 }
266
267 {
268   my ($res, $c) = ctx_request('/user/path-example/111/222?path_prefix=1');
269   is $c->action, 'user/path_example';
270   is $res->content, '/path-example/111/222';
271   is_deeply $c->req->args, [111,222];
272 }
273
274 {
275   my ($res, $c) = ctx_request('/user/path-example/111/222?env_path=1');
276   is $c->action, 'user/path_example';
277   is $res->content, '/111/222';
278   is_deeply $c->req->args, [111,222];
279 }
280
281 {
282   my ($res, $c) = ctx_request('/user/path-example/111/222?path=1');
283   is $c->action, 'user/path_example';
284   is $res->content, '/';
285   is_deeply $c->req->args, [111,222];
286 }
287
288 {
289   my ($res, $c) = ctx_request('/user/path-example-args1/333');
290   is $c->action, 'user/path_example_args1';
291   is $res->content, '/user/path-example-args1/333';
292   is_deeply $c->req->args, [333];
293 }
294
295 {
296   my ($res, $c) = ctx_request('/user/path-example-args1/333?path_prefix=1');
297   is $c->action, 'user/path_example_args1';
298   is $res->content, '/path-example-args1/333';
299   is_deeply $c->req->args, [333];
300 }
301
302 {
303   my ($res, $c) = ctx_request('/user/path-example-args1/333?env_path=1');
304   is $c->action, 'user/path_example_args1';
305   is $res->content, '/333';
306   is_deeply $c->req->args, [333];
307 }
308
309 {
310   my ($res, $c) = ctx_request('/user/path-example-args1/333?path=1');
311   is $c->action, 'user/path_example_args1';
312   is $res->content, '/';
313   is_deeply $c->req->args, [333];
314 }
315
316 # Chaining test /user/end_chain/*
317 #
318 #
319
320 {
321   my ($res, $c) = ctx_request('/user/end_chain/444');
322   is $c->action, 'user/end_chain';
323   is $res->content, '/user/end_chain/444';
324   is_deeply $c->req->args, [444];
325 }
326
327 {
328   my ($res, $c) = ctx_request('/user/end_chain/444?path_prefix=1');
329   is $c->action, 'user/end_chain';
330   is $res->content, '/end_chain/444';
331   is_deeply $c->req->args, [444];
332 }
333
334 {
335   my ($res, $c) = ctx_request('/user/end_chain/444?env_path=1');
336   is $c->action, 'user/end_chain';
337   is $res->content, '/444';
338   is_deeply $c->req->args, [444];
339 }
340
341 {
342   my ($res, $c) = ctx_request('/user/end_chain/444?path=1');
343   is $c->action, 'user/end_chain';
344   is $res->content, '/';
345   is_deeply $c->req->args, [444];
346 }
347
348 {
349   my ($res, $c) = ctx_request('/docs/name');
350   is $c->action, 'docs/name';
351   is $res->content, '/';
352   is_deeply $c->req->args, [];
353 }
354
355 {
356   my ($res, $c) = ctx_request('/docs/name/111/222');
357   is $c->action, 'docs/name';
358   is $res->content, '/111/222';
359   is_deeply $c->req->args, [111,222];
360 }
361
362 {
363   my ($res, $c) = ctx_request('/docs/name_args/111');
364   is $c->action, 'docs/name_args';
365   is $res->content, '/111';
366   is_deeply $c->req->args, [111];
367 }
368
369 done_testing();
370
371 __END__
372
373
374 use Plack::App::URLMap;
375 use HTTP::Request::Common;
376 use HTTP::Message::PSGI;
377
378 my $urlmap = Plack::App::URLMap->new;
379
380 my $app1 = sub {
381   my $env = shift;
382   return [200, [], [
383     "REQUEST_URI: $env->{REQUEST_URI}, FROM: $env->{MAP_TO}, PATH_INFO: $env->{PATH_INFO}, SCRIPT_NAME $env->{SCRIPT_NAME}"]];
384 };
385
386 $urlmap->map("/" => sub { my $env = shift; $env->{MAP_TO} = '/'; $app1->($env)});
387 $urlmap->map("/foo" => sub { my $env = shift; $env->{MAP_TO} = '/foo'; $app1->($env)});
388 $urlmap->map("/bar/baz" => sub { my $env = shift; $env->{MAP_TO} = '/foo/bar'; $app1->($env)});
389
390 my $app = $urlmap->to_app;
391
392 warn $app->(req_to_psgi(GET '/'))->[2]->[0];
393 warn $app->(req_to_psgi(GET '/111'))->[2]->[0];
394 warn $app->(req_to_psgi(GET '/foo'))->[2]->[0];
395 warn $app->(req_to_psgi(GET '/foo/222'))->[2]->[0];
396 warn $app->(req_to_psgi(GET '/bar/baz'))->[2]->[0];
397 warn $app->(req_to_psgi(GET '/bar/baz/333'))->[2]->[0];
398