7 use lib "$FindBin::Bin/../lib";
15 my $dispatcher = TestApp->dispatcher;
20 my $private_action = $dispatcher->get_action_by_path(
21 '/class_forward_test_method'
24 ok(!defined($dispatcher->uri_for_action($private_action)),
25 "Private action returns undef for URI");
30 my $path_action = $dispatcher->get_action_by_path(
31 '/action/testrelative/relative'
34 is($dispatcher->uri_for_action($path_action), "/action/relative/relative",
35 "Public path action returns correct URI");
37 ok(!defined($dispatcher->uri_for_action($path_action, [ 'foo' ])),
38 "no URI returned for Path action when snippets are given");
43 my $regex_action = $dispatcher->get_action_by_path(
47 ok(!defined($dispatcher->uri_for_action($regex_action)),
48 "Regex action without captures returns undef");
50 ok(!defined($dispatcher->uri_for_action($regex_action, [ 1, 2, 3 ])),
51 "Regex action with too many captures returns undef");
53 is($dispatcher->uri_for_action($regex_action, [ 'foo', 123 ]),
54 "/action/regexp/foo/123",
55 "Regex action interpolates captures correctly");
57 my $regex_action_bs = $dispatcher->get_action_by_path(
58 '/action/regexp/one_backslashes'
61 ok(!defined($dispatcher->uri_for_action($regex_action_bs)),
62 "Regex action without captures returns undef");
64 ok(!defined($dispatcher->uri_for_action($regex_action_bs, [ 1, 2, 3 ])),
65 "Regex action with too many captures returns undef");
67 is($dispatcher->uri_for_action($regex_action_bs, [ 'foo', 123 ]),
68 "/action/regexp/foo/123.html",
69 "Regex action interpolates captures correctly");
75 my $index_action = $dispatcher->get_action_by_path(
79 ok(!defined($dispatcher->uri_for_action($index_action, [ 'foo' ])),
80 "no URI returned for index action when snippets are given");
82 is($dispatcher->uri_for_action($index_action),
84 "index action returns correct path");
89 my $chained_action = $dispatcher->get_action_by_path(
90 '/action/chained/endpoint',
93 ok(!defined($dispatcher->uri_for_action($chained_action)),
94 "Chained action without captures returns undef");
96 ok(!defined($dispatcher->uri_for_action($chained_action, [ 1, 2 ])),
97 "Chained action with too many captures returns undef");
99 is($dispatcher->uri_for_action($chained_action, [ 1 ]),
100 "/chained/foo/1/end",
101 "Chained action with correct captures returns correct path");
106 my $request = Catalyst::Request->new( {
107 base => URI->new('http://127.0.0.1/foo')
110 my $context = TestApp->new( {
115 is($context->uri_for($context->controller('Action')),
116 "http://127.0.0.1/foo/yada/action/",
117 "uri_for a controller");
119 is($context->uri_for($path_action),
120 "http://127.0.0.1/foo/action/relative/relative",
121 "uri_for correct for path action");
123 is($context->uri_for($path_action, qw/one two/, { q => 1 }),
124 "http://127.0.0.1/foo/action/relative/relative/one/two?q=1",
125 "uri_for correct for path action with args and query");
127 ok(!defined($context->uri_for($path_action, [ 'blah' ])),
128 "no URI returned by uri_for for Path action with snippets");
130 is($context->uri_for($regex_action, [ 'foo', 123 ], qw/bar baz/, { q => 1 }),
131 "http://127.0.0.1/foo/action/regexp/foo/123/bar/baz?q=1",
132 "uri_for correct for regex with captures, args and query");
134 is($context->uri_for($chained_action, [ 1 ], 2, { q => 1 }),
135 "http://127.0.0.1/foo/chained/foo/1/end/2?q=1",
136 "uri_for correct for chained with captures, args and query");
139 # More Chained with Context Tests
142 is( $context->uri_for_action( '/action/chained/endpoint2', [1,2], (3,4), { x => 5 } ),
143 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
144 'uri_for_action correct for chained with multiple captures and args' );
146 is( $context->uri_for_action( '/action/chained/three_end', [1,2,3], (4,5,6) ),
147 'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
148 'uri_for_action correct for chained with multiple capturing actions' );
150 my $action_needs_two = '/action/chained/endpoint2';
152 ok( ! defined( $context->uri_for_action($action_needs_two, [1], (2,3)) ),
153 'uri_for_action returns undef for not enough captures' );
155 is( $context->uri_for_action($action_needs_two, [1,2], (2,3)),
156 'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
157 'uri_for_action returns correct uri for correct captures' );
159 ok( ! defined( $context->uri_for_action($action_needs_two, [1,2,3], (2,3)) ),
160 'uri_for_action returns undef for too many captures' );
162 is( $context->uri_for_action($action_needs_two, [1,2], (3)),
163 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
164 'uri_for_action returns uri with lesser args than specified on action' );
166 is( $context->uri_for_action($action_needs_two, [1,2], (3,4,5)),
167 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
168 'uri_for_action returns uri with more args than specified on action' );
170 is( $context->uri_for_action($action_needs_two, [1,''], (3,4)),
171 'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
172 'uri_for_action returns uri with empty capture on undef capture' );
174 is( $context->uri_for_action($action_needs_two, [1,2], ('',3)),
175 'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
176 'uri_for_action returns uri with empty arg on undef argument' );
178 is( $context->uri_for_action($action_needs_two, [1,2], (3,'')),
179 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
180 'uri_for_action returns uri with empty arg on undef last argument' );
182 my $complex_chained = '/action/chained/empty_chain_f';
183 is( $context->uri_for_action( $complex_chained, [23], (13), {q => 3} ),
184 'http://127.0.0.1/foo/chained/empty/23/13?q=3',
185 'uri_for_action returns correct uri for chain with many empty path parts' );
187 eval { $context->uri_for_action( '/does/not/exist' ) };
188 like $@, qr{^Can't find action for path '/does/not/exist'},
189 'uri_for_action croaks on nonexistent path';