3 use HTTP::Request::Common;
8 eval "use Type::Tiny 1.000005; 1" || do {
9 plan skip_all => "Trouble loading Type::Tiny and friends => $@";
15 $INC{'MyApp/Types.pm'} = __FILE__;
21 use Types::Standard -types;
24 -declare => qw( UserId Heart User ContextLike );
26 extends "Types::Standard";
28 class_type User, { class => "MyApp::Model::User::user" };
29 duck_type ContextLike, [qw/model/];
39 # Tests using this are skipped pending deeper thought
42 via { $_->model('User')->find( $_->req->args->[0] ) };
46 package MyApp::Role::Controller;
47 $INC{'MyApp/Role/Controller.pm'} = __FILE__;
50 use MooseX::MethodAttributes::Role;
51 use MyApp::Types qw/Int Str/;
53 sub role_str :Path('role_test') Args(Str) {
54 my ($self, $c, $arg) = @_;
55 $c->res->body('role_str'.$arg);
58 sub role_int :Path('role_test') Args(Int) {
59 my ($self, $c, $arg) = @_;
60 $c->res->body('role_int'.$arg);
63 package MyApp::Model::User;
64 $INC{'MyApp/Model/User.pm'} = __FILE__;
66 use base 'Catalyst::Model';
69 1 => { name => 'john', age => 46 },
70 2 => { name => 'mary', age => 36 },
71 3 => { name => 'ian', age => 25 },
72 4 => { name => 'visha', age => 18 },
77 my $user = $users{$id} || return;
78 return bless $user, "MyApp::Model::User::user";
81 package MyApp::Controller::Root;
82 $INC{'MyApp/Controller/Root.pm'} = __FILE__;
85 use MooseX::MethodAttributes;
86 use Types::Standard qw/slurpy/;
87 use MyApp::Types qw/Tuple Int Str StrMatch ArrayRef UserId User Heart/;
89 extends 'Catalyst::Controller';
90 with 'MyApp::Role::Controller';
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}");
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}");
105 sub stringy_enum :Local Args('Int',Int) {
107 $c->res->body('enum');
110 sub an_int :Local Args(Int) {
111 my ($self, $c, $int) = @_;
112 $c->res->body('an_int');
115 sub two_ints :Local Args(Int,Int) {
116 my ($self, $c, $int) = @_;
117 $c->res->body('two_ints');
120 sub many_ints :Local Args(ArrayRef[Int]) {
121 my ($self, $c, @ints) = @_;
122 $c->res->body('many_ints');
125 sub tuple :Local Args(Tuple[Str,Int]) {
126 my ($self, $c, $str, $int) = @_;
127 $c->res->body('tuple');
130 sub slurpy_tuple :Local Args(Tuple[Str,Int, slurpy ArrayRef[Int]]) {
131 my ($self, $c, $str, $int) = @_;
132 $c->res->body('tuple');
135 sub match :Local Args(StrMatch[qr{\d\d-\d\d-\d\d}]) {
136 my ($self, $c, $int) = @_;
137 $c->res->body('match');
140 sub any_priority :Path('priority_test') Args(1) { $_[1]->res->body('any_priority') }
142 sub int_priority :Path('priority_test') Args(Int) { $_[1]->res->body('int_priority') }
144 sub chain_base :Chained(/) CaptureArgs(1) { }
146 sub any_priority_chain :GET Chained(chain_base) PathPart('') Args(1) { $_[1]->res->body('any_priority_chain') }
148 sub int_priority_chain :Chained(chain_base) PathPart('') Args(Int) { $_[1]->res->body('int_priority_chain') }
150 sub link_any :Chained(chain_base) PathPart('') CaptureArgs(1) { }
152 sub any_priority_link_any :Chained(link_any) PathPart('') Args(1) { $_[1]->res->body('any_priority_link_any') }
154 sub int_priority_link_any :Chained(link_any) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link_any') }
156 sub link_int :Chained(chain_base) PathPart('') CaptureArgs(Int) { }
158 sub any_priority_link :Chained(link_int) PathPart('') Args(1) { $_[1]->res->body('any_priority_link') }
160 sub int_priority_link :Chained(link_int) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link') }
162 sub link_int_int :Chained(chain_base) PathPart('') CaptureArgs(Int,Int) { }
164 sub any_priority_link2 :Chained(link_int_int) PathPart('') Args(1) { $_[1]->res->body('any_priority_link2') }
166 sub int_priority_link2 :Chained(link_int_int) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link2') }
168 sub link_tuple :Chained(chain_base) PathPart('') CaptureArgs(Tuple[Int,Int,Int]) { }
170 sub any_priority_link3 :Chained(link_tuple) PathPart('') Args(1) { $_[1]->res->body('any_priority_link3') }
172 sub int_priority_link3 :Chained(link_tuple) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link3') }
174 sub link2_int :Chained(link_tuple) PathPart('') CaptureArgs(UserId) { }
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') }
179 sub chain_base2 :Chained(/) CaptureArgs(1) { }
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') }
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') }
189 sub heart :Local Args(Heart) { }
191 sub utf8_base :Chained(/) CaptureArgs(Heart) { }
192 sub utf8_end :Chained(utf8_base) PathPart('') Args(Heart) { }
194 sub default :Default {
195 my ($self, $c, $int) = @_;
196 $c->res->body('default');
199 MyApp::Controller::Root->config(namespace=>'');
207 use Catalyst::Test 'MyApp';
210 my $res = request '/an_int/1';
211 is $res->content, 'an_int';
215 my $res = request '/an_int/aa';
216 is $res->content, 'default';
220 my $res = request '/many_ints/1';
221 is $res->content, 'many_ints';
225 my $res = request '/many_ints/1/2';
226 is $res->content, 'many_ints';
230 my $res = request '/many_ints/1/2/3';
231 is $res->content, 'many_ints';
235 my $res = request '/priority_test/1';
236 is $res->content, 'int_priority';
240 my $res = request '/priority_test/a';
241 is $res->content, 'any_priority';
245 my $res = request '/match/11-22-33';
246 is $res->content, 'match';
250 my $res = request '/match/aaa';
251 is $res->content, 'default';
255 my $res = request '/user/2';
256 is $res->content, 'name: mary, age: 36';
260 my $res = request '/user/20';
261 is $res->content, 'default';
266 skip "coercion support needs more thought", 1;
267 my $res = request '/user_object/20';
268 is $res->content, 'default';
272 skip "coercion support needs more thought", 1;
273 my $res = request '/user_object/2';
274 is $res->content, 'name: mary, age: 36';
278 my $res = request '/chain_base/capture/arg';
279 is $res->content, 'any_priority_chain';
283 my $res = request '/chain_base/cap1/100/arg';
284 is $res->content, 'any_priority_link';
288 my $res = request '/chain_base/cap1/101/102';
289 is $res->content, 'int_priority_link';
293 my $res = request '/chain_base/capture/100';
294 is $res->content, 'int_priority_chain', 'got expected';
298 my $res = request '/chain_base/cap1/a/arg';
299 is $res->content, 'any_priority_link_any';
303 my $res = request '/chain_base/cap1/a/102';
304 is $res->content, 'int_priority_link_any';
308 my $res = request '/two_ints/1/2';
309 is $res->content, 'two_ints';
313 my $res = request '/two_ints/aa/111';
314 is $res->content, 'default';
318 my $res = request '/tuple/aaa/aaa';
319 is $res->content, 'default';
323 my $res = request '/tuple/aaa/111';
324 is $res->content, 'tuple';
328 my $res = request '/tuple/aaa/111/111/111';
329 is $res->content, 'default';
333 my $res = request '/slurpy_tuple/aaa/111/111/111';
334 is $res->content, 'tuple';
339 my $res = request '/many_ints/1/2/a';
340 is $res->content, 'default';
344 my $res = request '/chain_base/100/100/100/100';
345 is $res->content, 'int_priority_link2';
349 my $res = request '/chain_base/100/ss/100/100';
350 is $res->content, 'default';
354 my $res = request '/chain_base/100/100/100/100/100';
355 is $res->content, 'int_priority_link3';
359 my $res = request '/chain_base/100/ss/100/100/100';
360 is $res->content, 'default';
364 my $res = request '/chain_base/1/2/3/3/3/6';
365 is $res->content, 'finally';
369 my $res = request '/chain_base/1/2/3/3/3/a';
370 is $res->content, 'finally2';
374 my $res = request '/chain_base/1/2/3/3/3/6/7/8/9';
375 is $res->content, 'finally2';
380 my $res = request PUT '/chain_base2/capture/1';
381 is $res->content, 'chained_zero3', "request PUT '/chain_base2/capture/1'";
385 my $res = request '/chain_base2/capture/1';
386 is $res->content, 'chained_zero3', "request '/chain_base2/capture/1'";
390 my $res = request POST '/chain_base2/capture/1';
391 is $res->content, 'chained_zero3', "request POST '/chain_base2/capture/1'";
395 my $res = request PUT '/chain_base2/capture';
396 is $res->content, 'chained_zero2', "request PUT '/chain_base2/capture'";
400 my $res = request '/chain_base2/capture';
401 is $res->content, 'chained_zero2', "request '/chain_base2/capture'";
405 my $res = request POST '/chain_base2/capture';
406 is $res->content, 'chained_zero2', "request POST '/chain_base2/capture'";
410 my $res = request '/stringy_enum/1/2';
411 is $res->content, 'enum', "request '/stringy_enum/a'";
415 my $res = request '/stringy_enum/b/2';
416 is $res->content, 'default', "request '/stringy_enum/a'";
420 my $res = request '/stringy_enum/1/a';
421 is $res->content, 'default', "request '/stringy_enum/a'";
426 | /chain_base/*/*/*/*/*/* | /chain_base (1)
427 | | -> /link_tuple (Tuple[Int,Int,Int])
428 | | -> /link2_int (UserId)
429 | | => GET /finally (Int)
435 my ($res, $c) = ctx_request '/';
438 ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('user'), 2) };
439 is $url, 'http://localhost/user/2';
443 ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('user'), [2]) };
444 is $url, 'http://localhost/user/2';
448 ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('user'), [20]) };
452 ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,4],6) };
453 is $url, 'http://localhost/chain_base/1/2/3/4/4/6';
457 ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,4,6]) };
458 is $url, 'http://localhost/chain_base/1/2/3/4/4/6';
462 ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,5,6]) };
466 ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('finally'), ['a',2,3,4,4,6]) };
467 is $url, 'http://localhost/chain_base/a/2/3/4/4/6';
471 ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('finally'), ['a','1',3,4,4,'a']) };
475 ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('finally'), ['a','a',3,4,4,'6']) };
479 ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('heart'), ['♥']) };
480 is $url, 'http://localhost/heart/%E2%99%A5';
484 ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('heart'), ['1']) };
488 ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('utf8_end'), ['♥','♥']) };
489 is $url, 'http://localhost/utf8_base/%E2%99%A5/%E2%99%A5';
493 ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('utf8_end'), ['2','1']) };
501 my $res = request '/role_test/1';
502 is $res->content, 'role_int1';
506 my $res = request '/role_test/a';
507 is $res->content, 'role_stra';