5ef97e53ed916118423f32a7e3c6b7d559ffa1dd
[catagits/Catalyst-Runtime.git] / t / arg_constraints.t
1 use warnings;
2 use strict;
3 use HTTP::Request::Common;
4 use utf8;
5
6 BEGIN {
7   use Test::More;
8   eval "use Type::Tiny 1.000005; 1" || do {
9     plan skip_all => "Trouble loading Type::Tiny and friends => $@";
10   };
11 }
12
13 BEGIN {
14   package MyApp::Types;
15   $INC{'MyApp/Types.pm'} = __FILE__;
16
17   use strict;
18   use warnings;
19  
20   use Type::Utils -all;
21   use Types::Standard -types;
22   use Type::Library
23    -base,
24    -declare => qw( UserId Heart User ContextLike );
25
26   extends "Types::Standard"; 
27
28   class_type User, { class => "MyApp::Model::User::user" };
29   duck_type ContextLike, [qw/model/];
30
31   declare UserId,
32    as Int,
33    where { $_ < 5 };
34
35   declare Heart,
36    as Str,
37    where { $_ eq '♥' };
38
39   # Tests using this are skipped pending deeper thought
40   coerce User,
41    from ContextLike,
42      via { $_->model('User')->find( $_->req->args->[0] ) };
43 }
44
45 {
46   package MyApp::Role::Controller;
47   $INC{'MyApp/Role/Controller.pm'} = __FILE__;
48
49   use Moose::Role;
50   use MooseX::MethodAttributes::Role;
51   use MyApp::Types qw/Int Str/;
52
53   sub role_str :Path('role_test') Args(Str) {
54     my ($self, $c, $arg) = @_;
55     $c->res->body('role_str'.$arg);
56   }
57
58   sub role_int :Path('role_test') Args(Int) {
59     my ($self, $c, $arg) = @_;
60     $c->res->body('role_int'.$arg);
61   }
62
63   package MyApp::Model::User;
64   $INC{'MyApp/Model/User.pm'} = __FILE__;
65
66   use base 'Catalyst::Model';
67
68   our %users = (
69     1 => { name => 'john', age => 46 },
70     2 => { name => 'mary', age => 36 },
71     3 => { name => 'ian', age => 25 },
72     4 => { name => 'visha', age => 18 },
73   );
74
75   sub find {
76     my ($self, $id) = @_;
77     my $user = $users{$id} || return;
78     return bless $user, "MyApp::Model::User::user";
79   }
80
81   package MyApp::Controller::Root;
82   $INC{'MyApp/Controller/Root.pm'} = __FILE__;
83
84   use Moose;
85   use MooseX::MethodAttributes;
86   use Types::Standard qw/slurpy/;
87   use MyApp::Types qw/Tuple Int Str StrMatch ArrayRef UserId User Heart/;
88
89   extends 'Catalyst::Controller';
90   with 'MyApp::Role::Controller';
91
92
93   sub user :Local Args(UserId) {
94     my ($self, $c, $int) = @_;
95     my $user = $c->model("User")->find($int);
96     $c->res->body("name: $user->{name}, age: $user->{age}");
97   }
98
99   # Tests using this are current skipped pending coercion rethink
100   sub user_object :Local Args(User) Coerce(1) {
101     my ($self, $c, $user) = @_;
102     $c->res->body("name: $user->{name}, age: $user->{age}");
103   }
104
105   sub stringy_enum :Local Args('Int',Int) {
106     my ($self, $c) = @_;
107     $c->res->body('enum');
108   }
109
110   sub an_int :Local Args(Int) {
111     my ($self, $c, $int) = @_;
112     $c->res->body('an_int');
113   }
114
115   sub two_ints :Local Args(Int,Int) {
116     my ($self, $c, $int) = @_;
117     $c->res->body('two_ints');
118   }
119
120   sub many_ints :Local Args(ArrayRef[Int]) {
121     my ($self, $c, @ints) = @_;
122     $c->res->body('many_ints');
123   }
124
125   sub tuple :Local Args(Tuple[Str,Int]) {
126     my ($self, $c, $str, $int) = @_;
127     $c->res->body('tuple');
128   }
129
130   sub slurpy_tuple :Local Args(Tuple[Str,Int, slurpy ArrayRef[Int]]) {
131     my ($self, $c, $str, $int) = @_;
132     $c->res->body('tuple');
133   }
134
135   sub match :Local Args(StrMatch[qr{\d\d-\d\d-\d\d}]) {
136     my ($self, $c, $int) = @_;
137     $c->res->body('match');
138   }
139
140   sub any_priority :Path('priority_test') Args(1) { $_[1]->res->body('any_priority') }
141
142   sub int_priority :Path('priority_test') Args(Int) { $_[1]->res->body('int_priority') }
143
144   sub chain_base :Chained(/) CaptureArgs(1) { }
145
146     sub any_priority_chain :GET Chained(chain_base) PathPart('') Args(1) { $_[1]->res->body('any_priority_chain') }
147
148     sub int_priority_chain :Chained(chain_base) PathPart('') Args(Int) { $_[1]->res->body('int_priority_chain') }
149
150     sub link_any :Chained(chain_base) PathPart('') CaptureArgs(1) { }
151
152       sub any_priority_link_any :Chained(link_any) PathPart('') Args(1) { $_[1]->res->body('any_priority_link_any') }
153
154       sub int_priority_link_any :Chained(link_any) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link_any') }
155     
156     sub link_int :Chained(chain_base) PathPart('') CaptureArgs(Int) { }
157
158       sub any_priority_link :Chained(link_int) PathPart('') Args(1) { $_[1]->res->body('any_priority_link') }
159
160       sub int_priority_link :Chained(link_int) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link') }
161
162     sub link_int_int :Chained(chain_base) PathPart('') CaptureArgs(Int,Int) { }
163
164       sub any_priority_link2 :Chained(link_int_int) PathPart('') Args(1) { $_[1]->res->body('any_priority_link2') }
165
166       sub int_priority_link2 :Chained(link_int_int) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link2') }
167
168     sub link_tuple :Chained(chain_base) PathPart('') CaptureArgs(Tuple[Int,Int,Int]) { }
169
170       sub any_priority_link3 :Chained(link_tuple) PathPart('') Args(1) { $_[1]->res->body('any_priority_link3') }
171
172       sub int_priority_link3 :Chained(link_tuple) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link3') }
173
174       sub link2_int :Chained(link_tuple) PathPart('') CaptureArgs(UserId) { }
175
176         sub finally2 :GET Chained(link2_int) PathPart('') Args { $_[1]->res->body('finally2') }
177         sub finally :GET Chained(link2_int) PathPart('') Args(Int) { $_[1]->res->body('finally') }
178
179   sub chain_base2 :Chained(/) CaptureArgs(1) { }
180
181     sub chained_zero_again : Chained(chain_base2) PathPart('') Args(0) { $_[1]->res->body('chained_zero_again') }
182     sub chained_zero_post2 : Chained(chain_base2) PathPart('') Args(0) { $_[1]->res->body('chained_zero_post2') }
183     sub chained_zero2      :     Chained(chain_base2) PathPart('') Args(0) { $_[1]->res->body('chained_zero2') }
184
185     sub chained_zero_post3 : Chained(chain_base2) PathPart('') Args(1) { $_[1]->res->body('chained_zero_post3') }
186     sub chained_zero3      :     Chained(chain_base2) PathPart('') Args(1) { $_[1]->res->body('chained_zero3') }
187
188
189   sub heart :Local Args(Heart) { }
190
191   sub utf8_base :Chained(/) CaptureArgs(Heart) { }
192     sub utf8_end :Chained(utf8_base) PathPart('') Args(Heart) { }
193
194   sub default :Default {
195     my ($self, $c, $int) = @_;
196     $c->res->body('default');
197   }
198
199   MyApp::Controller::Root->config(namespace=>'');
200
201   package MyApp::Controller::Autoclean;
202   $INC{'MyApp/Controller/Autoclean.pm'} = __FILE__;
203
204   use Moose;
205   use MooseX::MethodAttributes;
206   use namespace::autoclean;
207
208   use MyApp::Types qw/Int/;
209
210   extends 'Catalyst::Controller';
211
212   sub an_int :Local Args(Int) {
213     my ($self, $c, $int) = @_;
214     $c->res->body('an_int (autoclean)');
215   }
216
217   MyApp::Controller::Autoclean->config(namespace=>'autoclean');
218
219   package MyApp::Role;
220   $INC{'MyApp/Role.pm'} = __FILE__;
221
222   use Moose::Role;
223   use MooseX::MethodAttributes::Role;
224   use MyApp::Types qw/Int/;
225
226   sub an_int :Local Args(Int) {
227     my ($self, $c, $int) = @_;
228     $c->res->body('an_int (withrole)');
229   }
230
231   package MyApp::Controller::WithRole;
232   $INC{'MyApp/Controller/WithRole.pm'} = __FILE__;
233
234   use Moose;
235   use MooseX::MethodAttributes;
236
237   extends 'Catalyst::Controller';
238
239   with 'MyApp::Role';
240
241   MyApp::Controller::WithRole->config(namespace=>'withrole');
242
243   package MyApp;
244   use Catalyst;
245
246   MyApp->setup;
247 }
248
249 use Catalyst::Test 'MyApp';
250
251 {
252   my $res = request '/an_int/1';
253   is $res->content, 'an_int';
254 }
255
256 {
257   my $res = request '/an_int/aa';
258   is $res->content, 'default';
259 }
260
261 {
262   my $res = request '/many_ints/1';
263   is $res->content, 'many_ints';
264 }
265
266 {
267   my $res = request '/many_ints/1/2';
268   is $res->content, 'many_ints';
269 }
270
271 {
272   my $res = request '/many_ints/1/2/3';
273   is $res->content, 'many_ints';
274 }
275
276 {
277   my $res = request '/priority_test/1';
278   is $res->content, 'int_priority';
279 }
280
281 {
282   my $res = request '/priority_test/a';
283   is $res->content, 'any_priority';
284 }
285
286 {
287   my $res = request '/match/11-22-33';
288   is $res->content, 'match';
289 }
290
291 {
292   my $res = request '/match/aaa';
293   is $res->content, 'default';
294 }
295
296 {
297   my $res = request '/user/2';
298   is $res->content, 'name: mary, age: 36';
299 }
300
301 {
302   my $res = request '/user/20';
303   is $res->content, 'default';
304 }
305
306
307 SKIP: {
308   skip "coercion support needs more thought", 1;
309   my $res = request '/user_object/20';
310   is $res->content, 'default';
311 }
312
313 SKIP: {
314   skip "coercion support needs more thought", 1;
315   my $res = request '/user_object/2';
316   is $res->content, 'name: mary, age: 36';
317 }
318
319 {
320   my $res = request '/chain_base/capture/arg';
321   is $res->content, 'any_priority_chain';
322 }
323
324 {
325   my $res = request '/chain_base/cap1/100/arg';
326   is $res->content, 'any_priority_link';
327 }
328
329 {
330   my $res = request '/chain_base/cap1/101/102';
331   is $res->content, 'int_priority_link';
332 }
333
334 {
335   my $res = request '/chain_base/capture/100';
336   is $res->content, 'int_priority_chain', 'got expected';
337 }
338
339 {
340   my $res = request '/chain_base/cap1/a/arg';
341   is $res->content, 'any_priority_link_any';
342 }
343
344 {
345   my $res = request '/chain_base/cap1/a/102';
346   is $res->content, 'int_priority_link_any';
347 }
348
349 {
350   my $res = request '/two_ints/1/2';
351   is $res->content, 'two_ints';
352 }
353
354 {
355   my $res = request '/two_ints/aa/111';
356   is $res->content, 'default';
357 }
358
359 {
360   my $res = request '/tuple/aaa/aaa';
361   is $res->content, 'default';
362 }
363
364 {
365   my $res = request '/tuple/aaa/111';
366   is $res->content, 'tuple';
367 }
368
369 {
370   my $res = request '/tuple/aaa/111/111/111';
371   is $res->content, 'default';
372 }
373
374 {
375   my $res = request '/slurpy_tuple/aaa/111/111/111';
376   is $res->content, 'tuple';
377 }
378
379
380 {
381   my $res = request '/many_ints/1/2/a';
382   is $res->content, 'default';
383 }
384
385 {
386   my $res = request '/chain_base/100/100/100/100';
387   is $res->content, 'int_priority_link2';
388 }
389
390 {
391   my $res = request '/chain_base/100/ss/100/100';
392   is $res->content, 'default';
393 }
394
395 {
396   my $res = request '/chain_base/100/100/100/100/100';
397   is $res->content, 'int_priority_link3';
398 }
399
400 {
401   my $res = request '/chain_base/100/ss/100/100/100';
402   is $res->content, 'default';
403 }
404
405 {
406   my $res = request '/chain_base/1/2/3/3/3/6';
407   is $res->content, 'finally';
408 }
409
410 {
411   my $res = request '/chain_base/1/2/3/3/3/a';
412   is $res->content, 'finally2';
413 }
414
415 {
416   my $res = request '/chain_base/1/2/3/3/3/6/7/8/9';
417   is $res->content, 'finally2';
418 }
419
420
421 {
422     my $res = request PUT '/chain_base2/capture/1';
423     is $res->content, 'chained_zero3', "request PUT '/chain_base2/capture/1'";
424 }
425
426 {
427     my $res = request '/chain_base2/capture/1';
428     is $res->content, 'chained_zero3', "request '/chain_base2/capture/1'";
429 }
430
431 {
432     my $res = request POST '/chain_base2/capture/1';
433     is $res->content, 'chained_zero3', "request POST '/chain_base2/capture/1'";
434 }
435
436 {
437     my $res = request PUT '/chain_base2/capture';
438     is $res->content, 'chained_zero2', "request PUT '/chain_base2/capture'";
439 }
440
441 {
442     my $res = request '/chain_base2/capture';
443     is $res->content, 'chained_zero2', "request '/chain_base2/capture'";
444 }
445
446 {
447     my $res = request POST '/chain_base2/capture';
448     is $res->content, 'chained_zero2', "request POST '/chain_base2/capture'";
449 }
450
451 {
452     my $res = request '/stringy_enum/1/2';
453     is $res->content, 'enum', "request '/stringy_enum/a'";
454 }
455
456 {
457     my $res = request '/stringy_enum/b/2';
458     is $res->content, 'default', "request '/stringy_enum/a'";
459 }
460
461 {
462     my $res = request '/stringy_enum/1/a';
463     is $res->content, 'default', "request '/stringy_enum/a'";
464 }
465
466 {
467   my $res = request '/autoclean/an_int/1';
468   is $res->content, 'an_int (autoclean)';
469 }
470
471 {
472   my $res = request '/withrole/an_int/1';
473   is $res->content, 'an_int (withrole)';
474 }
475
476 =over
477
478 | /chain_base/*/*/*/*/*/*                 | /chain_base (1)
479 |                                         | -> /link_tuple (Tuple[Int,Int,Int])
480 |                                         | -> /link2_int (UserId)
481 |                                         | => GET /finally (Int)
482
483 =cut
484
485 {
486   # URI testing
487   my ($res, $c) = ctx_request '/';
488
489   {
490     ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('user'), 2) };
491     is $url, 'http://localhost/user/2';
492   }
493
494   {
495     ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('user'), [2]) };
496     is $url, 'http://localhost/user/2';
497   }
498
499   {
500     ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('user'), [20]) };
501   }
502
503   {
504     ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,4],6) };
505     is $url, 'http://localhost/chain_base/1/2/3/4/4/6';
506   }
507
508   {
509     ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,4,6]) };
510     is $url, 'http://localhost/chain_base/1/2/3/4/4/6';
511   }
512
513   {
514     ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,5,6]) };
515   }
516
517   {
518     ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('finally'), ['a',2,3,4,4,6]) };
519     is $url, 'http://localhost/chain_base/a/2/3/4/4/6';
520   }
521
522   {
523     ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('finally'), ['a','1',3,4,4,'a']) };
524   }
525
526   {
527     ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('finally'), ['a','a',3,4,4,'6']) };
528   }
529
530   {
531     ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('heart'), ['♥']) };
532     is $url, 'http://localhost/heart/%E2%99%A5';
533   }
534
535   {
536     ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('heart'), ['1']) };
537   }
538
539   {
540     ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('utf8_end'), ['♥','♥']) };
541     is $url, 'http://localhost/utf8_base/%E2%99%A5/%E2%99%A5';
542   }
543
544   {
545     ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('utf8_end'), ['2','1']) };
546   }
547
548 }
549
550 # Test Roles
551
552 {
553     my $res = request '/role_test/1';
554     is $res->content, 'role_int1';
555 }
556
557 {
558     my $res = request '/role_test/a';
559     is $res->content, 'role_stra';
560 }
561
562
563 done_testing;