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");
60 my $index_action = $dispatcher->get_action_by_path(
64 ok(!defined($dispatcher->uri_for_action($index_action, [ 'foo' ])),
65 "no URI returned for index action when snippets are given");
67 is($dispatcher->uri_for_action($index_action),
69 "index action returns correct path");
74 my $chained_action = $dispatcher->get_action_by_path(
75 '/action/chained/endpoint',
78 ok(!defined($dispatcher->uri_for_action($chained_action)),
79 "Chained action without captures returns undef");
81 ok(!defined($dispatcher->uri_for_action($chained_action, [ 1, 2 ])),
82 "Chained action with too many captures returns undef");
84 is($dispatcher->uri_for_action($chained_action, [ 1 ]),
86 "Chained action with correct captures returns correct path");
91 my $request = Catalyst::Request->new( {
92 base => URI->new('http://127.0.0.1/foo')
95 my $context = TestApp->new( {
100 is($context->uri_for($context->controller('Action')),
101 "http://127.0.0.1/foo/yada/action/",
102 "uri_for a controller");
104 is($context->uri_for($path_action),
105 "http://127.0.0.1/foo/action/relative/relative",
106 "uri_for correct for path action");
108 is($context->uri_for($path_action, qw/one two/, { q => 1 }),
109 "http://127.0.0.1/foo/action/relative/relative/one/two?q=1",
110 "uri_for correct for path action with args and query");
112 ok(!defined($context->uri_for($path_action, [ 'blah' ])),
113 "no URI returned by uri_for for Path action with snippets");
115 is($context->uri_for($regex_action, [ 'foo', 123 ], qw/bar baz/, { q => 1 }),
116 "http://127.0.0.1/foo/action/regexp/foo/123/bar/baz?q=1",
117 "uri_for correct for regex with captures, args and query");
119 is($context->uri_for($regex_action, 'foo', 123, { q => 1 }),
120 "http://127.0.0.1/foo/action/regexp/foo/123?q=1",
121 "uri_for correct for regex no captures with args and query");
123 is($context->uri_for($chained_action, [ 1 ], 2, { q => 1 }),
124 "http://127.0.0.1/foo/chained/foo/1/end/2?q=1",
125 "uri_for correct for chained with captures, args and query");
127 is($context->uri_for($chained_action, 1, 2, { q => 1 }),
128 "http://127.0.0.1/foo/chained/foo/1/end/2?q=1",
129 "uri_for correct for chained no captures with args and query");
132 # More Chained with Context Tests
135 is( $context->uri_for_action( '/action/chained/endpoint2', [1,2], (3,4), { x => 5 } ),
136 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
137 'uri_for_action correct for chained with multiple captures and args' );
139 is( $context->uri_for_action( '/action/chained/endpoint2', qw(1 2 3 4), { x => 5 } ),
140 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
141 'uri_for_action correct for chained without captures with multiple args' );
143 is( $context->uri_for_action( '/action/chained/three_end', [1,2,3], (4,5,6) ),
144 'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
145 'uri_for_action correct for chained with multiple capturing actions' );
147 is( $context->uri_for_action( '/action/chained/three_end', qw(1 2 3 4 5 6) ),
148 'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
149 'uri_for_action correct for chained no captures multi capturing actions' );
151 ok( ! defined( $context->uri_for_action( '/action/chained/foo2' ) ),
152 'uri_for_action returns undef for chained action midpoints' );
154 my $action_needs_two = '/action/chained/endpoint2';
156 ok( ! defined( $context->uri_for_action($action_needs_two, [1], (2,3)) ),
157 'uri_for_action returns undef for not enough captures' );
159 ok( ! defined( $context->uri_for_action($action_needs_two, 1) ),
160 'uri_for_action returns undef for not enough captures/args total' );
162 is( $context->uri_for_action($action_needs_two, [1,2], (2,3)),
163 'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
164 'uri_for_action returns correct uri for correct captures' );
166 is( $context->uri_for_action($action_needs_two, qw(1 2 2 3)),
167 'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
168 'uri_for_action returns correct uri for correct captures/args total' );
170 ok( ! defined( $context->uri_for_action($action_needs_two, [1,2,3], (2,3)) ),
171 'uri_for_action returns undef for too many captures' );
173 is( $context->uri_for_action($action_needs_two, [1,2], (3)),
174 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
175 'uri_for_action returns uri with lesser args than specified on action' );
177 is( $context->uri_for_action($action_needs_two, qw(1 2 3)),
178 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
179 'uri_for_action returns uri with lesser args and no captures' );
181 is( $context->uri_for_action($action_needs_two, [1,2], (3,4,5)),
182 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
183 'uri_for_action returns uri with more args than specified on action' );
185 is( $context->uri_for_action($action_needs_two, qw(1 2 3 4 5)),
186 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
187 'uri_for_action returns uri with more args and no captures' );
189 is( $context->uri_for_action($action_needs_two, [1,''], (3,4)),
190 'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
191 'uri_for_action returns uri with empty capture on undef capture' );
193 is( $context->uri_for_action($action_needs_two, 1, '', 3, 4),
194 'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
195 'uri_for_action returns uri with no captures and empty arg' );
197 is( $context->uri_for_action($action_needs_two, [1,2], ('',3)),
198 'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
199 'uri_for_action returns uri with empty arg on undef argument' );
201 is( $context->uri_for_action($action_needs_two, 1, 2, '', 3),
202 'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
203 'uri_for_action returns uri no captures with empty arg on undef argument' );
205 is( $context->uri_for_action($action_needs_two, [1,2], (3,'')),
206 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
207 'uri_for_action returns uri with empty arg on undef last argument' );
209 is( $context->uri_for_action($action_needs_two, 1, 2, 3, ''),
210 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
211 'uri_for_action returns uri no captures empty arg undef last argument' );
213 my $complex_chained = '/action/chained/empty_chain_f';
214 is( $context->uri_for_action( $complex_chained, [23], (13), {q => 3} ),
215 'http://127.0.0.1/foo/chained/empty/23/13?q=3',
216 'uri_for_action returns correct uri for chain with many empty path parts' );
218 is( $context->uri_for_action( $complex_chained, 23, 13, {q => 3} ),
219 'http://127.0.0.1/foo/chained/empty/23/13?q=3',
220 'uri_for_action returns correct uri for chain no captures empty path parts' );
222 eval { $context->uri_for_action( '/does/not/exist' ) };
223 like $@, qr{^Can't find action for path '/does/not/exist'},
224 'uri_for_action croaks on nonexistent path';