3 use HTTP::Request::Common;
7 eval "use Type::Tiny; 1" || do {
8 plan skip_all => "Trouble loading Type::Tiny and friends => $@";
14 $INC{'MyApp/Types.pm'} = __FILE__;
20 use Types::Standard -types;
23 -declare => qw( UserId User ContextLike );
25 extends "Types::Standard";
27 class_type User, { class => "MyApp::Model::User::user" };
28 duck_type ContextLike, [qw/model/];
34 # Tests using this are skipped pending deeper thought
37 via { $_->model('User')->find( $_->req->args->[0] ) };
41 package MyApp::Model::User;
42 $INC{'MyApp/Model/User.pm'} = __FILE__;
44 use base 'Catalyst::Model';
47 1 => { name => 'john', age => 46 },
48 2 => { name => 'mary', age => 36 },
49 3 => { name => 'ian', age => 25 },
50 4 => { name => 'visha', age => 18 },
55 my $user = $users{$id} || return;
56 return bless $user, "MyApp::Model::User::user";
59 package MyApp::Controller::Root;
60 $INC{'MyApp/Controller/Root.pm'} = __FILE__;
63 use MooseX::MethodAttributes;
64 use MyApp::Types qw/Tuple Int Str StrMatch ArrayRef UserId User/;
66 extends 'Catalyst::Controller';
68 sub user :Local Args(UserId) {
69 my ($self, $c, $int) = @_;
70 my $user = $c->model("User")->find($int);
71 $c->res->body("name: $user->{name}, age: $user->{age}");
74 # Tests using this are current skipped pending coercion rethink
75 sub user_object :Local Args(User) Coerce(1) {
76 my ($self, $c, $user) = @_;
77 $c->res->body("name: $user->{name}, age: $user->{age}");
80 sub an_int :Local Args(Int) {
81 my ($self, $c, $int) = @_;
82 $c->res->body('an_int');
85 sub two_ints :Local Args(Int,Int) {
86 my ($self, $c, $int) = @_;
87 $c->res->body('two_ints');
90 sub many_ints :Local Args(ArrayRef[Int]) {
91 my ($self, $c, $int) = @_;
92 $c->res->body('many_ints');
95 sub tuple :Local Args(Tuple[Str,Int]) {
96 my ($self, $c, $str, $int) = @_;
97 $c->res->body('tuple');
100 sub match :Local Args(StrMatch[qr{\d\d-\d\d-\d\d}]) {
101 my ($self, $c, $int) = @_;
102 $c->res->body('match');
105 sub any_priority :Path('priority_test') Args(1) { $_[1]->res->body('any_priority') }
107 sub int_priority :Path('priority_test') Args(Int) { $_[1]->res->body('int_priority') }
109 sub chain_base :Chained(/) CaptureArgs(1) { }
111 sub any_priority_chain :GET Chained(chain_base) PathPart('') Args(1) { $_[1]->res->body('any_priority_chain') }
113 sub int_priority_chain :Chained(chain_base) PathPart('') Args(Int) { $_[1]->res->body('int_priority_chain') }
115 sub link_any :Chained(chain_base) PathPart('') CaptureArgs(1) { }
117 sub any_priority_link_any :Chained(link_any) PathPart('') Args(1) { $_[1]->res->body('any_priority_link_any') }
119 sub int_priority_link_any :Chained(link_any) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link_any') }
121 sub link_int :Chained(chain_base) PathPart('') CaptureArgs(Int) { }
123 sub any_priority_link :Chained(link_int) PathPart('') Args(1) { $_[1]->res->body('any_priority_link') }
125 sub int_priority_link :Chained(link_int) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link') }
127 sub link_int_int :Chained(chain_base) PathPart('') CaptureArgs(Int,Int) { }
129 sub any_priority_link2 :Chained(link_int_int) PathPart('') Args(1) { $_[1]->res->body('any_priority_link2') }
131 sub int_priority_link2 :Chained(link_int_int) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link2') }
133 sub link_tuple :Chained(chain_base) PathPart('') CaptureArgs(Tuple[Int,Int,Int]) { }
135 sub any_priority_link3 :Chained(link_tuple) PathPart('') Args(1) { $_[1]->res->body('any_priority_link3') }
137 sub int_priority_link3 :Chained(link_tuple) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link3') }
139 sub link2_int :Chained(link_tuple) PathPart('') CaptureArgs(UserId) { }
141 sub finally2 :GET Chained(link2_int) PathPart('') Args { $_[1]->res->body('finally2') }
142 sub finally :GET Chained(link2_int) PathPart('') Args(Int) { $_[1]->res->body('finally') }
144 sub chain_base2 :Chained(/) CaptureArgs(1) { }
146 sub chained_zero_again : Chained(chain_base2) PathPart('') Args(0) { $_[1]->res->body('chained_zero_again') }
147 sub chained_zero_post2 : Chained(chain_base2) PathPart('') Args(0) { $_[1]->res->body('chained_zero_post2') }
148 sub chained_zero2 : Chained(chain_base2) PathPart('') Args(0) { $_[1]->res->body('chained_zero2') }
150 sub chained_zero_post3 : Chained(chain_base2) PathPart('') Args(1) { $_[1]->res->body('chained_zero_post3') }
151 sub chained_zero3 : Chained(chain_base2) PathPart('') Args(1) { $_[1]->res->body('chained_zero3') }
154 sub default :Default {
155 my ($self, $c, $int) = @_;
156 $c->res->body('default');
159 MyApp::Controller::Root->config(namespace=>'');
167 use Catalyst::Test 'MyApp';
170 my $res = request '/an_int/1';
171 is $res->content, 'an_int';
175 my $res = request '/an_int/aa';
176 is $res->content, 'default';
180 my $res = request '/many_ints/1';
181 is $res->content, 'many_ints';
185 my $res = request '/many_ints/1/2';
186 is $res->content, 'many_ints';
190 my $res = request '/many_ints/1/2/3';
191 is $res->content, 'many_ints';
195 my $res = request '/priority_test/1';
196 is $res->content, 'int_priority';
200 my $res = request '/priority_test/a';
201 is $res->content, 'any_priority';
205 my $res = request '/match/11-22-33';
206 is $res->content, 'match';
210 my $res = request '/match/aaa';
211 is $res->content, 'default';
215 my $res = request '/user/2';
216 is $res->content, 'name: mary, age: 36';
220 my $res = request '/user/20';
221 is $res->content, 'default';
226 skip "coercion support needs more thought", 1;
227 my $res = request '/user_object/20';
228 is $res->content, 'default';
232 skip "coercion support needs more thought", 1;
233 my $res = request '/user_object/2';
234 is $res->content, 'name: mary, age: 36';
238 my $res = request '/chain_base/capture/arg';
239 is $res->content, 'any_priority_chain';
243 my $res = request '/chain_base/cap1/100/arg';
244 is $res->content, 'any_priority_link';
248 my $res = request '/chain_base/cap1/101/102';
249 is $res->content, 'int_priority_link';
253 my $res = request '/chain_base/capture/100';
254 is $res->content, 'int_priority_chain', 'got expected';
258 my $res = request '/chain_base/cap1/a/arg';
259 is $res->content, 'any_priority_link_any';
263 my $res = request '/chain_base/cap1/a/102';
264 is $res->content, 'int_priority_link_any';
268 my $res = request '/two_ints/1/2';
269 is $res->content, 'two_ints';
273 my $res = request '/two_ints/aa/111';
274 is $res->content, 'default';
278 my $res = request '/tuple/aaa/aaa';
279 is $res->content, 'default';
283 my $res = request '/tuple/aaa/111';
284 is $res->content, 'tuple';
288 my $res = request '/many_ints/1/2/a';
289 is $res->content, 'default';
293 my $res = request '/chain_base/100/100/100/100';
294 is $res->content, 'int_priority_link2';
298 my $res = request '/chain_base/100/ss/100/100';
299 is $res->content, 'default';
303 my $res = request '/chain_base/100/100/100/100/100';
304 is $res->content, 'int_priority_link3';
308 my $res = request '/chain_base/100/ss/100/100/100';
309 is $res->content, 'default';
313 my $res = request '/chain_base/1/2/3/3/3/6';
314 is $res->content, 'finally';
318 my $res = request '/chain_base/1/2/3/3/3/a';
319 is $res->content, 'finally2';
323 my $res = request '/chain_base/1/2/3/3/3/6/7/8/9';
324 is $res->content, 'finally2';
329 my $res = request PUT '/chain_base2/capture/1';
330 is $res->content, 'chained_zero3', "request PUT '/chain_base2/capture/1'";
334 my $res = request '/chain_base2/capture/1';
335 is $res->content, 'chained_zero3', "request '/chain_base2/capture/1'";
339 my $res = request POST '/chain_base2/capture/1';
340 is $res->content, 'chained_zero3', "request POST '/chain_base2/capture/1'";
344 my $res = request PUT '/chain_base2/capture';
345 is $res->content, 'chained_zero2', "request PUT '/chain_base2/capture'";
349 my $res = request '/chain_base2/capture';
350 is $res->content, 'chained_zero2', "request '/chain_base2/capture'";
354 my $res = request POST '/chain_base2/capture';
355 is $res->content, 'chained_zero2', "request POST '/chain_base2/capture'";
360 | /chain_base/*/*/*/*/*/* | /chain_base (1)
361 | | -> /link_tuple (Tuple[Int,Int,Int])
362 | | -> /link2_int (UserId)
363 | | => GET /finally (Int)
369 my ($res, $c) = ctx_request '/';
370 ok my $url1 = $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,5],6);
373 ok my $url2 = $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,5,6]);
376 ok my $url3 = $c->uri_for($c->controller('Root')->action_for('user'), 2);
379 ok my $url4 = $c->uri_for($c->controller('Root')->action_for('user'), [2]);