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::Model::User;
47 $INC{'MyApp/Model/User.pm'} = __FILE__;
49 use base 'Catalyst::Model';
52 1 => { name => 'john', age => 46 },
53 2 => { name => 'mary', age => 36 },
54 3 => { name => 'ian', age => 25 },
55 4 => { name => 'visha', age => 18 },
60 my $user = $users{$id} || return;
61 return bless $user, "MyApp::Model::User::user";
64 package MyApp::Controller::Root;
65 $INC{'MyApp/Controller/Root.pm'} = __FILE__;
68 use MooseX::MethodAttributes;
69 use Types::Standard qw/slurpy/;
70 use MyApp::Types qw/Tuple Int Str StrMatch ArrayRef UserId User Heart/;
72 extends 'Catalyst::Controller';
74 sub user :Local Args(UserId) {
75 my ($self, $c, $int) = @_;
76 my $user = $c->model("User")->find($int);
77 $c->res->body("name: $user->{name}, age: $user->{age}");
80 # Tests using this are current skipped pending coercion rethink
81 sub user_object :Local Args(User) Coerce(1) {
82 my ($self, $c, $user) = @_;
83 $c->res->body("name: $user->{name}, age: $user->{age}");
86 sub stringy_enum :Local Args('Int',Int) {
88 $c->res->body('enum');
91 sub an_int :Local Args(Int) {
92 my ($self, $c, $int) = @_;
93 $c->res->body('an_int');
96 sub two_ints :Local Args(Int,Int) {
97 my ($self, $c, $int) = @_;
98 $c->res->body('two_ints');
101 sub many_ints :Local Args(ArrayRef[Int]) {
102 my ($self, $c, @ints) = @_;
103 $c->res->body('many_ints');
106 sub tuple :Local Args(Tuple[Str,Int]) {
107 my ($self, $c, $str, $int) = @_;
108 $c->res->body('tuple');
111 sub slurpy_tuple :Local Args(Tuple[Str,Int, slurpy ArrayRef[Int]]) {
112 my ($self, $c, $str, $int) = @_;
113 $c->res->body('tuple');
116 sub match :Local Args(StrMatch[qr{\d\d-\d\d-\d\d}]) {
117 my ($self, $c, $int) = @_;
118 $c->res->body('match');
121 sub any_priority :Path('priority_test') Args(1) { $_[1]->res->body('any_priority') }
123 sub int_priority :Path('priority_test') Args(Int) { $_[1]->res->body('int_priority') }
125 sub chain_base :Chained(/) CaptureArgs(1) { }
127 sub any_priority_chain :GET Chained(chain_base) PathPart('') Args(1) { $_[1]->res->body('any_priority_chain') }
129 sub int_priority_chain :Chained(chain_base) PathPart('') Args(Int) { $_[1]->res->body('int_priority_chain') }
131 sub link_any :Chained(chain_base) PathPart('') CaptureArgs(1) { }
133 sub any_priority_link_any :Chained(link_any) PathPart('') Args(1) { $_[1]->res->body('any_priority_link_any') }
135 sub int_priority_link_any :Chained(link_any) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link_any') }
137 sub link_int :Chained(chain_base) PathPart('') CaptureArgs(Int) { }
139 sub any_priority_link :Chained(link_int) PathPart('') Args(1) { $_[1]->res->body('any_priority_link') }
141 sub int_priority_link :Chained(link_int) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link') }
143 sub link_int_int :Chained(chain_base) PathPart('') CaptureArgs(Int,Int) { }
145 sub any_priority_link2 :Chained(link_int_int) PathPart('') Args(1) { $_[1]->res->body('any_priority_link2') }
147 sub int_priority_link2 :Chained(link_int_int) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link2') }
149 sub link_tuple :Chained(chain_base) PathPart('') CaptureArgs(Tuple[Int,Int,Int]) { }
151 sub any_priority_link3 :Chained(link_tuple) PathPart('') Args(1) { $_[1]->res->body('any_priority_link3') }
153 sub int_priority_link3 :Chained(link_tuple) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link3') }
155 sub link2_int :Chained(link_tuple) PathPart('') CaptureArgs(UserId) { }
157 sub finally2 :GET Chained(link2_int) PathPart('') Args { $_[1]->res->body('finally2') }
158 sub finally :GET Chained(link2_int) PathPart('') Args(Int) { $_[1]->res->body('finally') }
160 sub chain_base2 :Chained(/) CaptureArgs(1) { }
162 sub chained_zero_again : Chained(chain_base2) PathPart('') Args(0) { $_[1]->res->body('chained_zero_again') }
163 sub chained_zero_post2 : Chained(chain_base2) PathPart('') Args(0) { $_[1]->res->body('chained_zero_post2') }
164 sub chained_zero2 : Chained(chain_base2) PathPart('') Args(0) { $_[1]->res->body('chained_zero2') }
166 sub chained_zero_post3 : Chained(chain_base2) PathPart('') Args(1) { $_[1]->res->body('chained_zero_post3') }
167 sub chained_zero3 : Chained(chain_base2) PathPart('') Args(1) { $_[1]->res->body('chained_zero3') }
170 sub heart :Local Args(Heart) { }
172 sub utf8_base :Chained(/) CaptureArgs(Heart) { }
173 sub utf8_end :Chained(utf8_base) PathPart('') Args(Heart) { }
175 sub default :Default {
176 my ($self, $c, $int) = @_;
177 $c->res->body('default');
180 MyApp::Controller::Root->config(namespace=>'');
188 use Catalyst::Test 'MyApp';
191 my $res = request '/an_int/1';
192 is $res->content, 'an_int';
196 my $res = request '/an_int/aa';
197 is $res->content, 'default';
201 my $res = request '/many_ints/1';
202 is $res->content, 'many_ints';
206 my $res = request '/many_ints/1/2';
207 is $res->content, 'many_ints';
211 my $res = request '/many_ints/1/2/3';
212 is $res->content, 'many_ints';
216 my $res = request '/priority_test/1';
217 is $res->content, 'int_priority';
221 my $res = request '/priority_test/a';
222 is $res->content, 'any_priority';
226 my $res = request '/match/11-22-33';
227 is $res->content, 'match';
231 my $res = request '/match/aaa';
232 is $res->content, 'default';
236 my $res = request '/user/2';
237 is $res->content, 'name: mary, age: 36';
241 my $res = request '/user/20';
242 is $res->content, 'default';
247 skip "coercion support needs more thought", 1;
248 my $res = request '/user_object/20';
249 is $res->content, 'default';
253 skip "coercion support needs more thought", 1;
254 my $res = request '/user_object/2';
255 is $res->content, 'name: mary, age: 36';
259 my $res = request '/chain_base/capture/arg';
260 is $res->content, 'any_priority_chain';
264 my $res = request '/chain_base/cap1/100/arg';
265 is $res->content, 'any_priority_link';
269 my $res = request '/chain_base/cap1/101/102';
270 is $res->content, 'int_priority_link';
274 my $res = request '/chain_base/capture/100';
275 is $res->content, 'int_priority_chain', 'got expected';
279 my $res = request '/chain_base/cap1/a/arg';
280 is $res->content, 'any_priority_link_any';
284 my $res = request '/chain_base/cap1/a/102';
285 is $res->content, 'int_priority_link_any';
289 my $res = request '/two_ints/1/2';
290 is $res->content, 'two_ints';
294 my $res = request '/two_ints/aa/111';
295 is $res->content, 'default';
299 my $res = request '/tuple/aaa/aaa';
300 is $res->content, 'default';
304 my $res = request '/tuple/aaa/111';
305 is $res->content, 'tuple';
309 my $res = request '/tuple/aaa/111/111/111';
310 is $res->content, 'default';
314 my $res = request '/slurpy_tuple/aaa/111/111/111';
315 is $res->content, 'tuple';
320 my $res = request '/many_ints/1/2/a';
321 is $res->content, 'default';
325 my $res = request '/chain_base/100/100/100/100';
326 is $res->content, 'int_priority_link2';
330 my $res = request '/chain_base/100/ss/100/100';
331 is $res->content, 'default';
335 my $res = request '/chain_base/100/100/100/100/100';
336 is $res->content, 'int_priority_link3';
340 my $res = request '/chain_base/100/ss/100/100/100';
341 is $res->content, 'default';
345 my $res = request '/chain_base/1/2/3/3/3/6';
346 is $res->content, 'finally';
350 my $res = request '/chain_base/1/2/3/3/3/a';
351 is $res->content, 'finally2';
355 my $res = request '/chain_base/1/2/3/3/3/6/7/8/9';
356 is $res->content, 'finally2';
361 my $res = request PUT '/chain_base2/capture/1';
362 is $res->content, 'chained_zero3', "request PUT '/chain_base2/capture/1'";
366 my $res = request '/chain_base2/capture/1';
367 is $res->content, 'chained_zero3', "request '/chain_base2/capture/1'";
371 my $res = request POST '/chain_base2/capture/1';
372 is $res->content, 'chained_zero3', "request POST '/chain_base2/capture/1'";
376 my $res = request PUT '/chain_base2/capture';
377 is $res->content, 'chained_zero2', "request PUT '/chain_base2/capture'";
381 my $res = request '/chain_base2/capture';
382 is $res->content, 'chained_zero2', "request '/chain_base2/capture'";
386 my $res = request POST '/chain_base2/capture';
387 is $res->content, 'chained_zero2', "request POST '/chain_base2/capture'";
391 my $res = request '/stringy_enum/1/2';
392 is $res->content, 'enum', "request '/stringy_enum/a'";
396 my $res = request '/stringy_enum/b/2';
397 is $res->content, 'default', "request '/stringy_enum/a'";
401 my $res = request '/stringy_enum/1/a';
402 is $res->content, 'default', "request '/stringy_enum/a'";
407 | /chain_base/*/*/*/*/*/* | /chain_base (1)
408 | | -> /link_tuple (Tuple[Int,Int,Int])
409 | | -> /link2_int (UserId)
410 | | => GET /finally (Int)
416 my ($res, $c) = ctx_request '/';
419 ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('user'), 2) };
420 is $url, 'http://localhost/user/2';
424 ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('user'), [2]) };
425 is $url, 'http://localhost/user/2';
429 ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('user'), [20]) };
433 ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,4],6) };
434 is $url, 'http://localhost/chain_base/1/2/3/4/4/6';
438 ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,4,6]) };
439 is $url, 'http://localhost/chain_base/1/2/3/4/4/6';
443 ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,5,6]) };
447 ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('finally'), ['a',2,3,4,4,6]) };
448 is $url, 'http://localhost/chain_base/a/2/3/4/4/6';
452 ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('finally'), ['a','1',3,4,4,'a']) };
456 ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('finally'), ['a','a',3,4,4,'6']) };
460 ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('heart'), ['♥']) };
461 is $url, 'http://localhost/heart/%E2%99%A5';
465 ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('heart'), ['1']) };
469 ok my $url = eval { $c->uri_for($c->controller('Root')->action_for('utf8_end'), ['♥','♥']) };
470 is $url, 'http://localhost/utf8_base/%E2%99%A5/%E2%99%A5';
474 ok my $url = ! eval { $c->uri_for($c->controller('Root')->action_for('utf8_end'), ['2','1']) };