Commit | Line | Data |
d91504e3 |
1 | use warnings; |
2 | use strict; |
b4037086 |
3 | use HTTP::Request::Common; |
d2b583c3 |
4 | use utf8; |
842180f7 |
5 | |
6 | BEGIN { |
7 | use Test::More; |
8748abc5 |
8 | eval "use Type::Tiny 1.000005; 1" || do { |
ea3943b8 |
9 | plan skip_all => "Trouble loading Type::Tiny and friends => $@"; |
842180f7 |
10 | }; |
ea3943b8 |
11 | } |
6f0b85d2 |
12 | |
ea3943b8 |
13 | BEGIN { |
6f0b85d2 |
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, |
d2b583c3 |
24 | -declare => qw( UserId Heart User ContextLike ); |
6f0b85d2 |
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 | |
d2b583c3 |
35 | declare Heart, |
36 | as Str, |
37 | where { $_ eq '♥' }; |
38 | |
a7ab9aa9 |
39 | # Tests using this are skipped pending deeper thought |
6f0b85d2 |
40 | coerce User, |
41 | from ContextLike, |
42 | via { $_->model('User')->find( $_->req->args->[0] ) }; |
842180f7 |
43 | } |
d91504e3 |
44 | |
45 | { |
afa7a6c9 |
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 | |
6f0b85d2 |
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 | |
d91504e3 |
81 | package MyApp::Controller::Root; |
82 | $INC{'MyApp/Controller/Root.pm'} = __FILE__; |
83 | |
84 | use Moose; |
85 | use MooseX::MethodAttributes; |
d9f0a350 |
86 | use Types::Standard qw/slurpy/; |
d2b583c3 |
87 | use MyApp::Types qw/Tuple Int Str StrMatch ArrayRef UserId User Heart/; |
d91504e3 |
88 | |
89 | extends 'Catalyst::Controller'; |
afa7a6c9 |
90 | with 'MyApp::Role::Controller'; |
91 | |
d91504e3 |
92 | |
6f0b85d2 |
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 | |
a7ab9aa9 |
99 | # Tests using this are current skipped pending coercion rethink |
6f0b85d2 |
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 | |
75ce30d0 |
105 | sub stringy_enum :Local Args('Int',Int) { |
106 | my ($self, $c) = @_; |
107 | $c->res->body('enum'); |
108 | } |
109 | |
6d62355b |
110 | sub an_int :Local Args(Int) { |
111 | my ($self, $c, $int) = @_; |
6d62355b |
112 | $c->res->body('an_int'); |
113 | } |
114 | |
bf4f1643 |
115 | sub two_ints :Local Args(Int,Int) { |
116 | my ($self, $c, $int) = @_; |
117 | $c->res->body('two_ints'); |
118 | } |
119 | |
4a0218ca |
120 | sub many_ints :Local Args(ArrayRef[Int]) { |
d9f0a350 |
121 | my ($self, $c, @ints) = @_; |
4a0218ca |
122 | $c->res->body('many_ints'); |
123 | } |
124 | |
842180f7 |
125 | sub tuple :Local Args(Tuple[Str,Int]) { |
6f0b85d2 |
126 | my ($self, $c, $str, $int) = @_; |
842180f7 |
127 | $c->res->body('tuple'); |
128 | } |
129 | |
d9f0a350 |
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 | |
6f0b85d2 |
135 | sub match :Local Args(StrMatch[qr{\d\d-\d\d-\d\d}]) { |
136 | my ($self, $c, $int) = @_; |
137 | $c->res->body('match'); |
138 | } |
a82c96cf |
139 | |
e5604544 |
140 | sub any_priority :Path('priority_test') Args(1) { $_[1]->res->body('any_priority') } |
842180f7 |
141 | |
b7791bd7 |
142 | sub int_priority :Path('priority_test') Args(Int) { $_[1]->res->body('int_priority') } |
e5604544 |
143 | |
a82c96cf |
144 | sub chain_base :Chained(/) CaptureArgs(1) { } |
145 | |
90102012 |
146 | sub any_priority_chain :GET Chained(chain_base) PathPart('') Args(1) { $_[1]->res->body('any_priority_chain') } |
a82c96cf |
147 | |
148 | sub int_priority_chain :Chained(chain_base) PathPart('') Args(Int) { $_[1]->res->body('int_priority_chain') } |
149 | |
480d94b5 |
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 | |
a82c96cf |
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 | |
677c155c |
162 | sub link_int_int :Chained(chain_base) PathPart('') CaptureArgs(Int,Int) { } |
bf4f1643 |
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 | |
677c155c |
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 | |
b6847871 |
174 | sub link2_int :Chained(link_tuple) PathPart('') CaptureArgs(UserId) { } |
175 | |
79b7db20 |
176 | sub finally2 :GET Chained(link2_int) PathPart('') Args { $_[1]->res->body('finally2') } |
90102012 |
177 | sub finally :GET Chained(link2_int) PathPart('') Args(Int) { $_[1]->res->body('finally') } |
a82c96cf |
178 | |
aef0cb5d |
179 | sub chain_base2 :Chained(/) CaptureArgs(1) { } |
180 | |
70949f28 |
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') } |
aef0cb5d |
183 | sub chained_zero2 : Chained(chain_base2) PathPart('') Args(0) { $_[1]->res->body('chained_zero2') } |
184 | |
70949f28 |
185 | sub chained_zero_post3 : Chained(chain_base2) PathPart('') Args(1) { $_[1]->res->body('chained_zero_post3') } |
aef0cb5d |
186 | sub chained_zero3 : Chained(chain_base2) PathPart('') Args(1) { $_[1]->res->body('chained_zero3') } |
187 | |
188 | |
d2b583c3 |
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 | |
6d62355b |
194 | sub default :Default { |
195 | my ($self, $c, $int) = @_; |
196 | $c->res->body('default'); |
d91504e3 |
197 | } |
198 | |
199 | MyApp::Controller::Root->config(namespace=>''); |
200 | |
fc036112 |
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 | |
225 | sub an_int :Local Args(Int) { |
226 | my ($self, $c, $int) = @_; |
227 | $c->res->body('an_int (withrole)'); |
228 | } |
229 | |
230 | package MyApp::Controller::WithRole; |
231 | $INC{'MyApp/Controller/WithRole.pm'} = __FILE__; |
232 | |
233 | use Moose; |
234 | use MooseX::MethodAttributes; |
235 | |
236 | extends 'Catalyst::Controller'; |
237 | |
238 | with 'MyApp::Role'; |
239 | |
240 | MyApp::Controller::WithRole->config(namespace=>'withrole'); |
241 | |
d91504e3 |
242 | package MyApp; |
243 | use Catalyst; |
244 | |
245 | MyApp->setup; |
246 | } |
247 | |
248 | use Catalyst::Test 'MyApp'; |
249 | |
250 | { |
6d62355b |
251 | my $res = request '/an_int/1'; |
252 | is $res->content, 'an_int'; |
253 | } |
254 | |
255 | { |
337a627a |
256 | my $res = request '/an_int/aa'; |
257 | is $res->content, 'default'; |
258 | } |
259 | |
260 | { |
4a0218ca |
261 | my $res = request '/many_ints/1'; |
262 | is $res->content, 'many_ints'; |
263 | } |
264 | |
265 | { |
266 | my $res = request '/many_ints/1/2'; |
267 | is $res->content, 'many_ints'; |
268 | } |
269 | |
270 | { |
271 | my $res = request '/many_ints/1/2/3'; |
272 | is $res->content, 'many_ints'; |
273 | } |
274 | |
275 | { |
e5604544 |
276 | my $res = request '/priority_test/1'; |
277 | is $res->content, 'int_priority'; |
278 | } |
842180f7 |
279 | |
e5604544 |
280 | { |
281 | my $res = request '/priority_test/a'; |
282 | is $res->content, 'any_priority'; |
283 | } |
284 | |
842180f7 |
285 | { |
6f0b85d2 |
286 | my $res = request '/match/11-22-33'; |
287 | is $res->content, 'match'; |
288 | } |
81436df9 |
289 | |
6f0b85d2 |
290 | { |
291 | my $res = request '/match/aaa'; |
292 | is $res->content, 'default'; |
293 | } |
294 | |
295 | { |
296 | my $res = request '/user/2'; |
297 | is $res->content, 'name: mary, age: 36'; |
298 | } |
299 | |
300 | { |
301 | my $res = request '/user/20'; |
302 | is $res->content, 'default'; |
303 | } |
304 | |
a7ab9aa9 |
305 | |
306 | SKIP: { |
307 | skip "coercion support needs more thought", 1; |
6f0b85d2 |
308 | my $res = request '/user_object/20'; |
309 | is $res->content, 'default'; |
310 | } |
311 | |
a7ab9aa9 |
312 | SKIP: { |
313 | skip "coercion support needs more thought", 1; |
6f0b85d2 |
314 | my $res = request '/user_object/2'; |
315 | is $res->content, 'name: mary, age: 36'; |
316 | } |
317 | |
a82c96cf |
318 | { |
319 | my $res = request '/chain_base/capture/arg'; |
320 | is $res->content, 'any_priority_chain'; |
321 | } |
322 | |
323 | { |
324 | my $res = request '/chain_base/cap1/100/arg'; |
325 | is $res->content, 'any_priority_link'; |
326 | } |
327 | |
328 | { |
329 | my $res = request '/chain_base/cap1/101/102'; |
330 | is $res->content, 'int_priority_link'; |
331 | } |
332 | |
333 | { |
334 | my $res = request '/chain_base/capture/100'; |
335 | is $res->content, 'int_priority_chain', 'got expected'; |
336 | } |
337 | |
480d94b5 |
338 | { |
339 | my $res = request '/chain_base/cap1/a/arg'; |
340 | is $res->content, 'any_priority_link_any'; |
341 | } |
342 | |
343 | { |
344 | my $res = request '/chain_base/cap1/a/102'; |
345 | is $res->content, 'int_priority_link_any'; |
346 | } |
347 | |
bf4f1643 |
348 | { |
349 | my $res = request '/two_ints/1/2'; |
350 | is $res->content, 'two_ints'; |
351 | } |
352 | |
353 | { |
354 | my $res = request '/two_ints/aa/111'; |
355 | is $res->content, 'default'; |
356 | } |
357 | |
358 | { |
359 | my $res = request '/tuple/aaa/aaa'; |
360 | is $res->content, 'default'; |
361 | } |
362 | |
363 | { |
364 | my $res = request '/tuple/aaa/111'; |
365 | is $res->content, 'tuple'; |
366 | } |
367 | |
368 | { |
d9f0a350 |
369 | my $res = request '/tuple/aaa/111/111/111'; |
370 | is $res->content, 'default'; |
371 | } |
372 | |
373 | { |
374 | my $res = request '/slurpy_tuple/aaa/111/111/111'; |
375 | is $res->content, 'tuple'; |
376 | } |
377 | |
378 | |
379 | { |
bf4f1643 |
380 | my $res = request '/many_ints/1/2/a'; |
381 | is $res->content, 'default'; |
382 | } |
383 | |
384 | { |
385 | my $res = request '/chain_base/100/100/100/100'; |
386 | is $res->content, 'int_priority_link2'; |
387 | } |
388 | |
389 | { |
390 | my $res = request '/chain_base/100/ss/100/100'; |
391 | is $res->content, 'default'; |
392 | } |
393 | |
677c155c |
394 | { |
395 | my $res = request '/chain_base/100/100/100/100/100'; |
396 | is $res->content, 'int_priority_link3'; |
397 | } |
398 | |
399 | { |
400 | my $res = request '/chain_base/100/ss/100/100/100'; |
401 | is $res->content, 'default'; |
402 | } |
403 | |
79b7db20 |
404 | { |
405 | my $res = request '/chain_base/1/2/3/3/3/6'; |
406 | is $res->content, 'finally'; |
407 | } |
408 | |
409 | { |
410 | my $res = request '/chain_base/1/2/3/3/3/a'; |
411 | is $res->content, 'finally2'; |
412 | } |
bf4f1643 |
413 | |
79b7db20 |
414 | { |
415 | my $res = request '/chain_base/1/2/3/3/3/6/7/8/9'; |
416 | is $res->content, 'finally2'; |
417 | } |
418 | |
b4037086 |
419 | |
420 | { |
aef0cb5d |
421 | my $res = request PUT '/chain_base2/capture/1'; |
70949f28 |
422 | is $res->content, 'chained_zero3', "request PUT '/chain_base2/capture/1'"; |
aef0cb5d |
423 | } |
424 | |
425 | { |
426 | my $res = request '/chain_base2/capture/1'; |
70949f28 |
427 | is $res->content, 'chained_zero3', "request '/chain_base2/capture/1'"; |
aef0cb5d |
428 | } |
429 | |
430 | { |
431 | my $res = request POST '/chain_base2/capture/1'; |
70949f28 |
432 | is $res->content, 'chained_zero3', "request POST '/chain_base2/capture/1'"; |
aef0cb5d |
433 | } |
434 | |
435 | { |
436 | my $res = request PUT '/chain_base2/capture'; |
70949f28 |
437 | is $res->content, 'chained_zero2', "request PUT '/chain_base2/capture'"; |
b4037086 |
438 | } |
439 | |
440 | { |
aef0cb5d |
441 | my $res = request '/chain_base2/capture'; |
70949f28 |
442 | is $res->content, 'chained_zero2', "request '/chain_base2/capture'"; |
b4037086 |
443 | } |
444 | |
445 | { |
aef0cb5d |
446 | my $res = request POST '/chain_base2/capture'; |
70949f28 |
447 | is $res->content, 'chained_zero2', "request POST '/chain_base2/capture'"; |
b4037086 |
448 | } |
449 | |
75ce30d0 |
450 | { |
451 | my $res = request '/stringy_enum/1/2'; |
452 | is $res->content, 'enum', "request '/stringy_enum/a'"; |
453 | } |
454 | |
455 | { |
456 | my $res = request '/stringy_enum/b/2'; |
457 | is $res->content, 'default', "request '/stringy_enum/a'"; |
458 | } |
459 | |
460 | { |
461 | my $res = request '/stringy_enum/1/a'; |
462 | is $res->content, 'default', "request '/stringy_enum/a'"; |
463 | } |
464 | |
fc036112 |
465 | { |
466 | my $res = request '/autoclean/an_int/1'; |
467 | is $res->content, 'an_int (autoclean)'; |
468 | } |
469 | |
470 | { |
471 | my $res = request '/withrole/an_int/1'; |
472 | is $res->content, 'an_int (withrole)'; |
473 | } |
474 | |
b4037086 |
475 | =over |
476 | |
477 | | /chain_base/*/*/*/*/*/* | /chain_base (1) |
478 | | | -> /link_tuple (Tuple[Int,Int,Int]) |
479 | | | -> /link2_int (UserId) |
480 | | | => GET /finally (Int) |
481 | |
b6847871 |
482 | =cut |
483 | |
484 | { |
485 | # URI testing |
486 | my ($res, $c) = ctx_request '/'; |
b6847871 |
487 | |
86a399db |
488 | { |
489 | ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('user'), 2) }; |
490 | is $url, 'http://localhost/user/2'; |
491 | } |
c1192f1e |
492 | |
86a399db |
493 | { |
494 | ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('user'), [2]) }; |
495 | is $url, 'http://localhost/user/2'; |
496 | } |
c1192f1e |
497 | |
86a399db |
498 | { |
499 | ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('user'), [20]) }; |
500 | } |
501 | |
502 | { |
503 | ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,4],6) }; |
504 | is $url, 'http://localhost/chain_base/1/2/3/4/4/6'; |
505 | } |
506 | |
507 | { |
508 | ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,4,6]) }; |
509 | is $url, 'http://localhost/chain_base/1/2/3/4/4/6'; |
510 | } |
511 | |
512 | { |
513 | ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,5,6]) }; |
514 | } |
515 | |
516 | { |
517 | ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('finally'), ['a',2,3,4,4,6]) }; |
518 | is $url, 'http://localhost/chain_base/a/2/3/4/4/6'; |
519 | } |
520 | |
521 | { |
522 | ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('finally'), ['a','1',3,4,4,'a']) }; |
523 | } |
524 | |
525 | { |
526 | ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('finally'), ['a','a',3,4,4,'6']) }; |
527 | } |
c1192f1e |
528 | |
d2b583c3 |
529 | { |
530 | ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('heart'), ['♥']) }; |
531 | is $url, 'http://localhost/heart/%E2%99%A5'; |
532 | } |
cbe13760 |
533 | |
d2b583c3 |
534 | { |
535 | ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('heart'), ['1']) }; |
536 | } |
86a399db |
537 | |
d2b583c3 |
538 | { |
539 | ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('utf8_end'), ['♥','♥']) }; |
540 | is $url, 'http://localhost/utf8_base/%E2%99%A5/%E2%99%A5'; |
541 | } |
cbe13760 |
542 | |
d2b583c3 |
543 | { |
544 | ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('utf8_end'), ['2','1']) }; |
545 | } |
cbe13760 |
546 | |
d2b583c3 |
547 | } |
86a399db |
548 | |
afa7a6c9 |
549 | # Test Roles |
550 | |
551 | { |
552 | my $res = request '/role_test/1'; |
553 | is $res->content, 'role_int1'; |
554 | } |
555 | |
556 | { |
557 | my $res = request '/role_test/a'; |
558 | is $res->content, 'role_stra'; |
559 | } |
560 | |
561 | |
d2b583c3 |
562 | done_testing; |