4c29a43bc216ef32040396cf68a007336a7dc645
[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::Model::User;
47   $INC{'MyApp/Model/User.pm'} = __FILE__;
48
49   use base 'Catalyst::Model';
50
51   our %users = (
52     1 => { name => 'john', age => 46 },
53     2 => { name => 'mary', age => 36 },
54     3 => { name => 'ian', age => 25 },
55     4 => { name => 'visha', age => 18 },
56   );
57
58   sub find {
59     my ($self, $id) = @_;
60     my $user = $users{$id} || return;
61     return bless $user, "MyApp::Model::User::user";
62   }
63
64   package MyApp::Controller::Root;
65   $INC{'MyApp/Controller/Root.pm'} = __FILE__;
66
67   use Moose;
68   use MooseX::MethodAttributes;
69   use MyApp::Types qw/Tuple Int Str StrMatch ArrayRef UserId User Heart/;
70
71   extends 'Catalyst::Controller';
72
73   sub user :Local Args(UserId) {
74     my ($self, $c, $int) = @_;
75     my $user = $c->model("User")->find($int);
76     $c->res->body("name: $user->{name}, age: $user->{age}");
77   }
78
79   # Tests using this are current skipped pending coercion rethink
80   sub user_object :Local Args(User) Coerce(1) {
81     my ($self, $c, $user) = @_;
82     $c->res->body("name: $user->{name}, age: $user->{age}");
83   }
84
85   sub an_int :Local Args(Int) {
86     my ($self, $c, $int) = @_;
87     $c->res->body('an_int');
88   }
89
90   sub two_ints :Local Args(Int,Int) {
91     my ($self, $c, $int) = @_;
92     $c->res->body('two_ints');
93   }
94
95   sub many_ints :Local Args(ArrayRef[Int]) {
96     my ($self, $c, $int) = @_;
97     $c->res->body('many_ints');
98   }
99
100   sub tuple :Local Args(Tuple[Str,Int]) {
101     my ($self, $c, $str, $int) = @_;
102     $c->res->body('tuple');
103   }
104
105   sub match :Local Args(StrMatch[qr{\d\d-\d\d-\d\d}]) {
106     my ($self, $c, $int) = @_;
107     $c->res->body('match');
108   }
109
110   sub any_priority :Path('priority_test') Args(1) { $_[1]->res->body('any_priority') }
111
112   sub int_priority :Path('priority_test') Args(Int) { $_[1]->res->body('int_priority') }
113
114   sub chain_base :Chained(/) CaptureArgs(1) { }
115
116     sub any_priority_chain :GET Chained(chain_base) PathPart('') Args(1) { $_[1]->res->body('any_priority_chain') }
117
118     sub int_priority_chain :Chained(chain_base) PathPart('') Args(Int) { $_[1]->res->body('int_priority_chain') }
119
120     sub link_any :Chained(chain_base) PathPart('') CaptureArgs(1) { }
121
122       sub any_priority_link_any :Chained(link_any) PathPart('') Args(1) { $_[1]->res->body('any_priority_link_any') }
123
124       sub int_priority_link_any :Chained(link_any) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link_any') }
125     
126     sub link_int :Chained(chain_base) PathPart('') CaptureArgs(Int) { }
127
128       sub any_priority_link :Chained(link_int) PathPart('') Args(1) { $_[1]->res->body('any_priority_link') }
129
130       sub int_priority_link :Chained(link_int) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link') }
131
132     sub link_int_int :Chained(chain_base) PathPart('') CaptureArgs(Int,Int) { }
133
134       sub any_priority_link2 :Chained(link_int_int) PathPart('') Args(1) { $_[1]->res->body('any_priority_link2') }
135
136       sub int_priority_link2 :Chained(link_int_int) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link2') }
137
138     sub link_tuple :Chained(chain_base) PathPart('') CaptureArgs(Tuple[Int,Int,Int]) { }
139
140       sub any_priority_link3 :Chained(link_tuple) PathPart('') Args(1) { $_[1]->res->body('any_priority_link3') }
141
142       sub int_priority_link3 :Chained(link_tuple) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link3') }
143
144       sub link2_int :Chained(link_tuple) PathPart('') CaptureArgs(UserId) { }
145
146         sub finally2 :GET Chained(link2_int) PathPart('') Args { $_[1]->res->body('finally2') }
147         sub finally :GET Chained(link2_int) PathPart('') Args(Int) { $_[1]->res->body('finally') }
148
149   sub chain_base2 :Chained(/) CaptureArgs(1) { }
150
151     sub chained_zero_again : Chained(chain_base2) PathPart('') Args(0) { $_[1]->res->body('chained_zero_again') }
152     sub chained_zero_post2 : Chained(chain_base2) PathPart('') Args(0) { $_[1]->res->body('chained_zero_post2') }
153     sub chained_zero2      :     Chained(chain_base2) PathPart('') Args(0) { $_[1]->res->body('chained_zero2') }
154
155     sub chained_zero_post3 : Chained(chain_base2) PathPart('') Args(1) { $_[1]->res->body('chained_zero_post3') }
156     sub chained_zero3      :     Chained(chain_base2) PathPart('') Args(1) { $_[1]->res->body('chained_zero3') }
157
158
159   sub heart :Local Args(Heart) { }
160
161   sub utf8_base :Chained(/) CaptureArgs(Heart) { }
162     sub utf8_end :Chained(utf8_base) PathPart('') Args(Heart) { }
163
164   sub default :Default {
165     my ($self, $c, $int) = @_;
166     $c->res->body('default');
167   }
168
169   MyApp::Controller::Root->config(namespace=>'');
170
171   package MyApp;
172   use Catalyst;
173
174   MyApp->setup;
175 }
176
177 use Catalyst::Test 'MyApp';
178
179 {
180   my $res = request '/an_int/1';
181   is $res->content, 'an_int';
182 }
183
184 {
185   my $res = request '/an_int/aa';
186   is $res->content, 'default';
187 }
188
189 {
190   my $res = request '/many_ints/1';
191   is $res->content, 'many_ints';
192 }
193
194 {
195   my $res = request '/many_ints/1/2';
196   is $res->content, 'many_ints';
197 }
198
199 {
200   my $res = request '/many_ints/1/2/3';
201   is $res->content, 'many_ints';
202 }
203
204 {
205   my $res = request '/priority_test/1';
206   is $res->content, 'int_priority';
207 }
208
209 {
210   my $res = request '/priority_test/a';
211   is $res->content, 'any_priority';
212 }
213
214 {
215   my $res = request '/match/11-22-33';
216   is $res->content, 'match';
217 }
218
219 {
220   my $res = request '/match/aaa';
221   is $res->content, 'default';
222 }
223
224 {
225   my $res = request '/user/2';
226   is $res->content, 'name: mary, age: 36';
227 }
228
229 {
230   my $res = request '/user/20';
231   is $res->content, 'default';
232 }
233
234
235 SKIP: {
236   skip "coercion support needs more thought", 1;
237   my $res = request '/user_object/20';
238   is $res->content, 'default';
239 }
240
241 SKIP: {
242   skip "coercion support needs more thought", 1;
243   my $res = request '/user_object/2';
244   is $res->content, 'name: mary, age: 36';
245 }
246
247 {
248   my $res = request '/chain_base/capture/arg';
249   is $res->content, 'any_priority_chain';
250 }
251
252 {
253   my $res = request '/chain_base/cap1/100/arg';
254   is $res->content, 'any_priority_link';
255 }
256
257 {
258   my $res = request '/chain_base/cap1/101/102';
259   is $res->content, 'int_priority_link';
260 }
261
262 {
263   my $res = request '/chain_base/capture/100';
264   is $res->content, 'int_priority_chain', 'got expected';
265 }
266
267 {
268   my $res = request '/chain_base/cap1/a/arg';
269   is $res->content, 'any_priority_link_any';
270 }
271
272 {
273   my $res = request '/chain_base/cap1/a/102';
274   is $res->content, 'int_priority_link_any';
275 }
276
277 {
278   my $res = request '/two_ints/1/2';
279   is $res->content, 'two_ints';
280 }
281
282 {
283   my $res = request '/two_ints/aa/111';
284   is $res->content, 'default';
285 }
286
287 {
288   my $res = request '/tuple/aaa/aaa';
289   is $res->content, 'default';
290 }
291
292 {
293   my $res = request '/tuple/aaa/111';
294   is $res->content, 'tuple';
295 }
296
297 {
298   my $res = request '/many_ints/1/2/a';
299   is $res->content, 'default';
300 }
301
302 {
303   my $res = request '/chain_base/100/100/100/100';
304   is $res->content, 'int_priority_link2';
305 }
306
307 {
308   my $res = request '/chain_base/100/ss/100/100';
309   is $res->content, 'default';
310 }
311
312 {
313   my $res = request '/chain_base/100/100/100/100/100';
314   is $res->content, 'int_priority_link3';
315 }
316
317 {
318   my $res = request '/chain_base/100/ss/100/100/100';
319   is $res->content, 'default';
320 }
321
322 {
323   my $res = request '/chain_base/1/2/3/3/3/6';
324   is $res->content, 'finally';
325 }
326
327 {
328   my $res = request '/chain_base/1/2/3/3/3/a';
329   is $res->content, 'finally2';
330 }
331
332 {
333   my $res = request '/chain_base/1/2/3/3/3/6/7/8/9';
334   is $res->content, 'finally2';
335 }
336
337
338 {
339     my $res = request PUT '/chain_base2/capture/1';
340     is $res->content, 'chained_zero3', "request PUT '/chain_base2/capture/1'";
341 }
342
343 {
344     my $res = request '/chain_base2/capture/1';
345     is $res->content, 'chained_zero3', "request '/chain_base2/capture/1'";
346 }
347
348 {
349     my $res = request POST '/chain_base2/capture/1';
350     is $res->content, 'chained_zero3', "request POST '/chain_base2/capture/1'";
351 }
352
353 {
354     my $res = request PUT '/chain_base2/capture';
355     is $res->content, 'chained_zero2', "request PUT '/chain_base2/capture'";
356 }
357
358 {
359     my $res = request '/chain_base2/capture';
360     is $res->content, 'chained_zero2', "request '/chain_base2/capture'";
361 }
362
363 {
364     my $res = request POST '/chain_base2/capture';
365     is $res->content, 'chained_zero2', "request POST '/chain_base2/capture'";
366 }
367
368 =over
369
370 | /chain_base/*/*/*/*/*/*                 | /chain_base (1)
371 |                                         | -> /link_tuple (Tuple[Int,Int,Int])
372 |                                         | -> /link2_int (UserId)
373 |                                         | => GET /finally (Int)
374
375 =cut
376
377 {
378   # URI testing
379   my ($res, $c) = ctx_request '/';
380
381   {
382     ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('user'), 2) };
383     is $url, 'http://localhost/user/2';
384   }
385
386   {
387     ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('user'), [2]) };
388     is $url, 'http://localhost/user/2';
389   }
390
391   {
392     ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('user'), [20]) };
393   }
394
395   {
396     ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,4],6) };
397     is $url, 'http://localhost/chain_base/1/2/3/4/4/6';
398   }
399
400   {
401     ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,4,6]) };
402     is $url, 'http://localhost/chain_base/1/2/3/4/4/6';
403   }
404
405   {
406     ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,5,6]) };
407   }
408
409   {
410     ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('finally'), ['a',2,3,4,4,6]) };
411     is $url, 'http://localhost/chain_base/a/2/3/4/4/6';
412   }
413
414   {
415     ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('finally'), ['a','1',3,4,4,'a']) };
416   }
417
418   {
419     ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('finally'), ['a','a',3,4,4,'6']) };
420   }
421
422   {
423     ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('heart'), ['♥']) };
424     is $url, 'http://localhost/heart/%E2%99%A5';
425   }
426
427   {
428     ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('heart'), ['1']) };
429   }
430
431   {
432     ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('utf8_end'), ['♥','♥']) };
433     is $url, 'http://localhost/utf8_base/%E2%99%A5/%E2%99%A5';
434   }
435
436   {
437     ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('utf8_end'), ['2','1']) };
438   }
439
440 }
441
442 done_testing;