5 use lib "$FindBin::Bin/../lib";
11 my $dispatcher = TestApp->dispatcher;
16 my $private_action = $dispatcher->get_action_by_path(
17 '/class_forward_test_method'
20 ok(!defined($dispatcher->uri_for_action($private_action)),
21 "Private action returns undef for URI");
26 my $path_action = $dispatcher->get_action_by_path(
27 '/action/testrelative/relative'
30 is($dispatcher->uri_for_action($path_action), "/action/relative/relative",
31 "Public path action returns correct URI");
33 ok(!defined($dispatcher->uri_for_action($path_action, [ 'foo' ])),
34 "no URI returned for Path action when snippets are given");
39 my $index_action = $dispatcher->get_action_by_path(
43 ok(!defined($dispatcher->uri_for_action($index_action, [ 'foo' ])),
44 "no URI returned for index action when snippets are given");
46 is($dispatcher->uri_for_action($index_action),
48 "index action returns correct path");
53 my $chained_action = $dispatcher->get_action_by_path(
54 '/action/chained/endpoint',
57 ok(!defined($dispatcher->uri_for_action($chained_action)),
58 "Chained action without captures returns undef");
60 ok(!defined($dispatcher->uri_for_action($chained_action, [ 1, 2 ])),
61 "Chained action with too many captures returns undef");
63 is($dispatcher->uri_for_action($chained_action, [ 1 ]),
65 "Chained action with correct captures returns correct path");
70 my $request = Catalyst::Request->new( {
71 _log => Catalyst::Log->new,
72 base => URI->new('http://127.0.0.1/foo')
75 my $context = TestApp->new( {
83 # JNAP: I'm going to todo these tests, calling uri_for as a class method
84 # should work, but its not really useful so I think theres not much harm
85 # if someone needs this for a business case they are welcome to figure out
89 local $TODO = "Need to fix using uri_for and uri_for_action as a class method";
92 # this works, using $ctx
93 is($context->uri_for($context->controller('Action::Chained')->action_for('endpoint')),
94 "http://127.0.0.1/foo/yada/chained/foo/end",
95 "uri_for a controller and action");
98 # this fails, uri_for returns undef, why isn't this one working??
99 is( $context->uri_for_action( '/action/chained/endpoint' ),
100 'http://127.0.0.1/chained/foo/end',
101 "uri_for a controller and action as string");
103 # this fails, uri_for returns undef
104 is(TestApp->uri_for_action($context->controller('Action::Chained')->action_for('endpoint')),
106 "uri_for a controller and action, called with only class name");
108 # this fails, uri_for returns undef
109 is(TestApp->uri_for_action('/action/chained/endpoint' ),
111 "uri_for a controller and action as string, called with only class name");
113 # this fails, uri_for returns undef
114 is(TestApp->uri_for_action( $chained_action),
116 "uri_for action via dispatcher, called with only class name");
120 is($context->uri_for($context->controller('Action')),
121 "http://127.0.0.1/foo/yada/action/",
122 "uri_for a controller");
124 is($context->uri_for($path_action),
125 "http://127.0.0.1/foo/action/relative/relative",
126 "uri_for correct for path action");
128 is($context->uri_for($path_action, qw/one two/, { q => 1 }),
129 "http://127.0.0.1/foo/action/relative/relative/one/two?q=1",
130 "uri_for correct for path action with args and query");
132 ok(!defined($context->uri_for($path_action, [ 'blah' ])),
133 "no URI returned by uri_for for Path action with snippets");
135 is($context->uri_for($chained_action, [ 1 ], 2, { q => 1 }),
136 "http://127.0.0.1/foo/chained/foo/1/end/2?q=1",
137 "uri_for correct for chained with captures, args and query");
140 # More Chained with Context Tests
143 is( $context->uri_for_action( '/action/chained/endpoint2', [1,2], (3,4), { x => 5 } ),
144 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
145 'uri_for_action correct for chained with multiple captures and args' );
147 is( $context->uri_for_action( '/action/chained/endpoint2', [1,2,3,4], { x => 5 } ),
148 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
149 'uri_for_action correct for chained with multiple captures and args combined' );
151 is( $context->uri_for_action( '/action/chained/three_end', [1,2,3], (4,5,6) ),
152 'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
153 'uri_for_action correct for chained with multiple capturing actions' );
155 is( $context->uri_for_action( '/action/chained/three_end', [1,2,3,4,5,6] ),
156 'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
157 'uri_for_action correct for chained with multiple capturing actions and args combined' );
159 my $action_needs_two = '/action/chained/endpoint2';
161 ok( ! defined( $context->uri_for_action($action_needs_two, [1], (2,3)) ),
162 'uri_for_action returns undef for not enough captures' );
164 is( $context->uri_for_action($action_needs_two, [1,2], (2,3)),
165 'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
166 'uri_for_action returns correct uri for correct captures' );
168 is( $context->uri_for_action($action_needs_two, [1,2,2,3]),
169 'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
170 'uri_for_action returns correct uri for correct captures and args combined' );
172 ok( ! defined( $context->uri_for_action($action_needs_two, [1,2,3], (2,3)) ),
173 'uri_for_action returns undef for too many captures' );
175 is( $context->uri_for_action($action_needs_two, [1,2], (3)),
176 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
177 'uri_for_action returns uri with lesser args than specified on action' );
179 is( $context->uri_for_action($action_needs_two, [1,2,3]),
180 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
181 'uri_for_action returns uri with lesser args than specified on action with captures combined' );
183 is( $context->uri_for_action($action_needs_two, [1,2], (3,4,5)),
184 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
185 'uri_for_action returns uri with more args than specified on action' );
187 is( $context->uri_for_action($action_needs_two, [1,2,3,4,5]),
188 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
189 'uri_for_action returns uri with more args than specified on action with captures combined' );
191 is( $context->uri_for_action($action_needs_two, [1,''], (3,4)),
192 'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
193 'uri_for_action returns uri with empty capture on undef capture' );
195 is( $context->uri_for_action($action_needs_two, [1,'',3,4]),
196 'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
197 'uri_for_action returns uri with empty capture on undef capture and args combined' );
199 is( $context->uri_for_action($action_needs_two, [1,2], ('',3)),
200 'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
201 'uri_for_action returns uri with empty arg on undef argument' );
203 is( $context->uri_for_action($action_needs_two, [1,2,'',3]),
204 'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
205 'uri_for_action returns uri with empty arg on undef argument and args combined' );
207 is( $context->uri_for_action($action_needs_two, [1,2], (3,'')),
208 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
209 'uri_for_action returns uri with empty arg on undef last argument' );
211 is( $context->uri_for_action($action_needs_two, [1,2,3,'']),
212 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
213 'uri_for_action returns uri with empty arg on undef last argument with captures combined' );
215 my $complex_chained = '/action/chained/empty_chain_f';
216 is( $context->uri_for_action( $complex_chained, [23], (13), {q => 3} ),
217 'http://127.0.0.1/foo/chained/empty/23/13?q=3',
218 'uri_for_action returns correct uri for chain with many empty path parts' );
219 is( $context->uri_for_action( $complex_chained, [23,13], {q => 3} ),
220 'http://127.0.0.1/foo/chained/empty/23/13?q=3',
221 'uri_for_action returns correct uri for chain with many empty path parts with captures and args combined' );
223 eval { $context->uri_for_action( '/does/not/exist' ) };
224 like $@, qr{^Can't find action for path '/does/not/exist'},
225 'uri_for_action croaks on nonexistent path';