as_psgi and to_app
[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::PSGIObject;
13
14   sub as_psgi {
15     return [200, ['Content-Type' => 'text/plain'], ['as_psgi']];
16   };
17
18   package MyApp::Controller::Docs;
19   $INC{'MyApp/Controller/Docs.pm'} = __FILE__;
20
21   use base 'Catalyst::Controller';
22   use Plack::Request;
23   use Catalyst::Utils;
24
25   sub as_psgi :Local {
26     my ($self, $c) = @_;
27     my $as_psgi = bless +{}, 'MyApp::PSGIObject';
28     $c->res->from_psgi_response($as_psgi);
29   }
30
31   sub name :Local {
32     my ($self, $c) = @_;
33     my $env = $c->Catalyst::Utils::env_at_action;
34     $c->res->from_psgi_response(
35       $psgi_app->($env));
36
37   }
38
39   sub name_args :Local Args(1) {
40     my ($self, $c, $arg) = @_;
41     my $env = $c->Catalyst::Utils::env_at_action;
42     $c->res->from_psgi_response(
43       $psgi_app->($env));
44   }
45
46   package MyApp::Controller::User;
47   $INC{'MyApp/Controller/User.pm'} = __FILE__;
48
49   use base 'Catalyst::Controller';
50   use Plack::Request;
51   use Catalyst::Utils;
52
53   sub local_example :Local {
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 local_example_args1 :Local Args(1) {
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 path_example :Path('path-example') {
68     my ($self, $c) = @_;
69     my $env = $self->get_env($c);
70     $c->res->from_psgi_response(
71       $psgi_app->($env));
72   }
73
74   sub path_example_args1 :Path('path-example-args1') {
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 chained :Chained(/) PathPrefix CaptureArgs(0) { }
82
83     sub from_chain :Chained('chained') PathPart('') CaptureArgs(0) {}
84
85       sub end_chain :Chained('from_chain') PathPath(abc-123) Args(1)
86       {
87         my ($self, $c) = @_;
88         my $env = $self->get_env($c);
89         $c->res->from_psgi_response(
90           $psgi_app->($env));
91       }
92
93   sub mounted :Local Args(1) {
94     my ($self, $c, $arg) = @_;
95     our $app ||= ref($c)->psgi_app;
96     my $env = $self->get_env($c);
97     $c->res->from_psgi_response(
98       $app->($env));
99   }
100
101   sub mount_arg :Path(/mounted) Arg(1) {
102     my ($self, $c, $arg) = @_;
103     my $uri =  $c->uri_for( $self->action_for('local_example_args1'),$arg);
104     $c->res->body("$uri");
105   }
106
107   sub mount_noarg :Path(/mounted_no_arg)  {
108     my ($self, $c) = @_;
109     my $uri =  $c->uri_for( $self->action_for('local_example_args1'),444);
110     $c->res->body("$uri");
111   }
112
113   
114   sub get_env {
115     my ($self, $c) = @_;
116     if($c->req->query_parameters->{path_prefix}) {
117       return $c->Catalyst::Utils::env_at_path_prefix;
118     } elsif($c->req->query_parameters->{env_path}) {
119       return $c->Catalyst::Utils::env_at_action;
120     } elsif($c->req->query_parameters->{path}) {
121       return $c->Catalyst::Utils::env_at_request_uri;
122     } else {
123       return $c->req->env;
124     }
125   }
126
127   package MyApp;
128   use Catalyst;
129   MyApp->setup;
130
131 }
132
133 use Test::More;
134 use Catalyst::Test 'MyApp';
135
136 {
137   my ($res, $c) = ctx_request('/docs/as_psgi');
138   is $res->content, 'as_psgi';
139 }
140
141 {
142   my ($res, $c) = ctx_request('/user/mounted/111?path_prefix=1');
143   is $c->action, 'user/mounted';
144   is $res->content, 'http://localhost/user/user/local_example_args1/111';
145   is_deeply $c->req->args, [111];
146 }
147
148 {
149   my ($res, $c) = ctx_request('/user/mounted/mounted_no_arg?env_path=1');
150   is $c->action, 'user/mounted';
151   is $res->content, 'http://localhost/user/mounted/user/local_example_args1/444';
152   is_deeply $c->req->args, ['mounted_no_arg'];
153 }
154
155 # BEGIN [user/local_example]
156 {
157   my ($res, $c) = ctx_request('/user/local_example');
158   is $c->action, 'user/local_example';
159   is $res->content, '/user/local_example';
160   is_deeply $c->req->args, [];
161 }
162
163 {
164   my ($res, $c) = ctx_request('/user/local_example/111/222');
165   is $c->action, 'user/local_example';
166   is $res->content, '/user/local_example/111/222';
167   is_deeply $c->req->args, [111,222];
168 }
169
170 {
171   my ($res, $c) = ctx_request('/user/local_example?path_prefix=1');
172   is $c->action, 'user/local_example';
173   is $res->content, '/local_example';
174   is_deeply $c->req->args, [];
175 }
176
177 {
178   my ($res, $c) = ctx_request('/user/local_example/111/222?path_prefix=1');
179   is $c->action, 'user/local_example';
180   is $res->content, '/local_example/111/222';
181   is_deeply $c->req->args, [111,222];
182 }
183
184 {
185   my ($res, $c) = ctx_request('/user/local_example?env_path=1');
186   is $c->action, 'user/local_example';
187   is $res->content, '/';
188   is_deeply $c->req->args, [];
189 }
190
191 {
192   my ($res, $c) = ctx_request('/user/local_example/111/222?env_path=1');
193   is $c->action, 'user/local_example';
194   is $res->content, '/111/222';
195   is_deeply $c->req->args, [111,222];
196 }
197
198 {
199   my ($res, $c) = ctx_request('/user/local_example?path=1');
200   is $c->action, 'user/local_example';
201   is $res->content, '/';
202   is_deeply $c->req->args, [];
203 }
204
205 {
206   my ($res, $c) = ctx_request('/user/local_example/111/222?path=1');
207   is $c->action, 'user/local_example';
208   is $res->content, '/';
209   is_deeply $c->req->args, [111,222];
210 }
211
212 # END [user/local_example]
213
214 # BEGIN [/user/local_example_args1/***/]
215
216 {
217   my ($res, $c) = ctx_request('/user/local_example_args1/333');
218   is $c->action, 'user/local_example_args1';
219   is $res->content, '/user/local_example_args1/333';
220   is_deeply $c->req->args, [333];
221 }
222
223 {
224   my ($res, $c) = ctx_request('/user/local_example_args1/333?path_prefix=1');
225   is $c->action, 'user/local_example_args1';
226   is $res->content, '/local_example_args1/333';
227   is_deeply $c->req->args, [333];
228 }
229
230 {
231   my ($res, $c) = ctx_request('/user/local_example_args1/333?env_path=1');
232   is $c->action, 'user/local_example_args1';
233   is $res->content, '/333';
234   is_deeply $c->req->args, [333];
235 }
236
237 {
238   my ($res, $c) = ctx_request('/user/local_example_args1/333?path=1');
239   is $c->action, 'user/local_example_args1';
240   is $res->content, '/';
241   is_deeply $c->req->args, [333];
242 }
243
244 # END [/user/local_example_args1/***/]
245
246 # BEGIN [/user/path-example] 
247
248 {
249   my ($res, $c) = ctx_request('/user/path-example');
250   is $c->action, 'user/path_example';
251   is $res->content, '/user/path-example';
252   is_deeply $c->req->args, [];
253 }
254
255 {
256   my ($res, $c) = ctx_request('/user/path-example?path_prefix=1');
257   is $c->action, 'user/path_example';
258   is $res->content, '/path-example';
259   is_deeply $c->req->args, [];
260 }
261
262 {
263   my ($res, $c) = ctx_request('/user/path-example?env_path=1');
264   is $c->action, 'user/path_example';
265   is $res->content, '/';
266   is_deeply $c->req->args, [];
267 }
268
269 {
270   my ($res, $c) = ctx_request('/user/path-example?path=1');
271   is $c->action, 'user/path_example';
272   is $res->content, '/';
273   is_deeply $c->req->args, [];
274 }
275
276
277 {
278   my ($res, $c) = ctx_request('/user/path-example/111/222');
279   is $c->action, 'user/path_example';
280   is $res->content, '/user/path-example/111/222';
281   is_deeply $c->req->args, [111,222];
282 }
283
284 {
285   my ($res, $c) = ctx_request('/user/path-example/111/222?path_prefix=1');
286   is $c->action, 'user/path_example';
287   is $res->content, '/path-example/111/222';
288   is_deeply $c->req->args, [111,222];
289 }
290
291 {
292   my ($res, $c) = ctx_request('/user/path-example/111/222?env_path=1');
293   is $c->action, 'user/path_example';
294   is $res->content, '/111/222';
295   is_deeply $c->req->args, [111,222];
296 }
297
298 {
299   my ($res, $c) = ctx_request('/user/path-example/111/222?path=1');
300   is $c->action, 'user/path_example';
301   is $res->content, '/';
302   is_deeply $c->req->args, [111,222];
303 }
304
305 {
306   my ($res, $c) = ctx_request('/user/path-example-args1/333');
307   is $c->action, 'user/path_example_args1';
308   is $res->content, '/user/path-example-args1/333';
309   is_deeply $c->req->args, [333];
310 }
311
312 {
313   my ($res, $c) = ctx_request('/user/path-example-args1/333?path_prefix=1');
314   is $c->action, 'user/path_example_args1';
315   is $res->content, '/path-example-args1/333';
316   is_deeply $c->req->args, [333];
317 }
318
319 {
320   my ($res, $c) = ctx_request('/user/path-example-args1/333?env_path=1');
321   is $c->action, 'user/path_example_args1';
322   is $res->content, '/333';
323   is_deeply $c->req->args, [333];
324 }
325
326 {
327   my ($res, $c) = ctx_request('/user/path-example-args1/333?path=1');
328   is $c->action, 'user/path_example_args1';
329   is $res->content, '/';
330   is_deeply $c->req->args, [333];
331 }
332
333 # Chaining test /user/end_chain/*
334 #
335 #
336
337 {
338   my ($res, $c) = ctx_request('/user/end_chain/444');
339   is $c->action, 'user/end_chain';
340   is $res->content, '/user/end_chain/444';
341   is_deeply $c->req->args, [444];
342 }
343
344 {
345   my ($res, $c) = ctx_request('/user/end_chain/444?path_prefix=1');
346   is $c->action, 'user/end_chain';
347   is $res->content, '/end_chain/444';
348   is_deeply $c->req->args, [444];
349 }
350
351 {
352   my ($res, $c) = ctx_request('/user/end_chain/444?env_path=1');
353   is $c->action, 'user/end_chain';
354   is $res->content, '/444';
355   is_deeply $c->req->args, [444];
356 }
357
358 {
359   my ($res, $c) = ctx_request('/user/end_chain/444?path=1');
360   is $c->action, 'user/end_chain';
361   is $res->content, '/';
362   is_deeply $c->req->args, [444];
363 }
364
365 {
366   my ($res, $c) = ctx_request('/docs/name');
367   is $c->action, 'docs/name';
368   is $res->content, '/';
369   is_deeply $c->req->args, [];
370 }
371
372 {
373   my ($res, $c) = ctx_request('/docs/name/111/222');
374   is $c->action, 'docs/name';
375   is $res->content, '/111/222';
376   is_deeply $c->req->args, [111,222];
377 }
378
379 {
380   my ($res, $c) = ctx_request('/docs/name_args/111');
381   is $c->action, 'docs/name_args';
382   is $res->content, '/111';
383   is_deeply $c->req->args, [111];
384 }
385
386 done_testing();