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 warn $dispatcher->uri_for_action($private_action);
26 ok(!defined($dispatcher->uri_for_action($private_action)),
27 "Private action returns undef for URI");
32 my $path_action = $dispatcher->get_action_by_path(
33 '/action/testrelative/relative'
36 is($dispatcher->uri_for_action($path_action), "/action/relative/relative",
37 "Public path action returns correct URI");
39 ok(!defined($dispatcher->uri_for_action($path_action, [ 'foo' ])),
40 "no URI returned for Path action when snippets are given");
45 my $regex_action = $dispatcher->get_action_by_path(
49 ok(!defined($dispatcher->uri_for_action($regex_action)),
50 "Regex action without captures returns undef");
52 ok(!defined($dispatcher->uri_for_action($regex_action, [ 1, 2, 3 ])),
53 "Regex action with too many captures returns undef");
55 is($dispatcher->uri_for_action($regex_action, [ 'foo', 123 ]),
56 "/action/regexp/foo/123",
57 "Regex action interpolates captures correctly");
62 my $index_action = $dispatcher->get_action_by_path(
66 ok(!defined($dispatcher->uri_for_action($index_action, [ 'foo' ])),
67 "no URI returned for index action when snippets are given");
69 is($dispatcher->uri_for_action($index_action),
71 "index action returns correct path");
76 my $chained_action = $dispatcher->get_action_by_path(
77 '/action/chained/endpoint',
80 ok(!defined($dispatcher->uri_for_action($chained_action)),
81 "Chained action without captures returns undef");
83 ok(!defined($dispatcher->uri_for_action($chained_action, [ 1, 2 ])),
84 "Chained action with too many captures returns undef");
86 is($dispatcher->uri_for_action($chained_action, [ 1 ]),
88 "Chained action with correct captures returns correct path");
93 my $request = Catalyst::Request->new( {
94 base => URI->new('http://127.0.0.1/foo')
97 my $context = TestApp->new( {
102 is($context->uri_for($context->controller('Action')),
103 "http://127.0.0.1/foo/yada/action/",
104 "uri_for a controller");
106 is($context->uri_for($path_action),
107 "http://127.0.0.1/foo/action/relative/relative",
108 "uri_for correct for path action");
110 is($context->uri_for($path_action, qw/one two/, { q => 1 }),
111 "http://127.0.0.1/foo/action/relative/relative/one/two?q=1",
112 "uri_for correct for path action with args and query");
114 ok(!defined($context->uri_for($path_action, [ 'blah' ])),
115 "no URI returned by uri_for for Path action with snippets");
117 is($context->uri_for($regex_action, [ 'foo', 123 ], qw/bar baz/, { q => 1 }),
118 "http://127.0.0.1/foo/action/regexp/foo/123/bar/baz?q=1",
119 "uri_for correct for regex with captures, args and query");
121 is($context->uri_for($chained_action, [ 1 ], 2, { q => 1 }),
122 "http://127.0.0.1/foo/chained/foo/1/end/2?q=1",
123 "uri_for correct for chained with captures, args and query");
126 # More Chained with Context Tests
129 is( $context->uri_for_action( '/action/chained/endpoint2', [1,2], (3,4), { x => 5 } ),
130 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
131 'uri_for_action correct for chained with multiple captures and args' );
133 is( $context->uri_for_action( '/action/chained/three_end', [1,2,3], (4,5,6) ),
134 'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
135 'uri_for_action correct for chained with multiple capturing actions' );
137 my $action_needs_two = '/action/chained/endpoint2';
139 ok( ! defined( $context->uri_for_action($action_needs_two, [1], (2,3)) ),
140 'uri_for_action returns undef for not enough captures' );
142 is( $context->uri_for_action($action_needs_two, [1,2], (2,3)),
143 'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
144 'uri_for_action returns correct uri for correct captures' );
146 ok( ! defined( $context->uri_for_action($action_needs_two, [1,2,3], (2,3)) ),
147 'uri_for_action returns undef for too many captures' );
149 is( $context->uri_for_action($action_needs_two, [1,2], (3)),
150 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
151 'uri_for_action returns uri with lesser args than specified on action' );
153 is( $context->uri_for_action($action_needs_two, [1,2], (3,4,5)),
154 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
155 'uri_for_action returns uri with more args than specified on action' );
157 is( $context->uri_for_action($action_needs_two, [1,''], (3,4)),
158 'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
159 'uri_for_action returns uri with empty capture on undef capture' );
161 is( $context->uri_for_action($action_needs_two, [1,2], ('',3)),
162 'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
163 'uri_for_action returns uri with empty arg on undef argument' );
165 is( $context->uri_for_action($action_needs_two, [1,2], (3,'')),
166 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
167 'uri_for_action returns uri with empty arg on undef last argument' );
169 my $complex_chained = '/action/chained/empty_chain_f';
170 is( $context->uri_for_action( $complex_chained, [23], (13), {q => 3} ),
171 'http://127.0.0.1/foo/chained/empty/23/13?q=3',
172 'uri_for_action returns correct uri for chain with many empty path parts' );
174 eval { $context->uri_for_action( '/does/not/exist' ) };
175 like $@, qr{^Can't find action for path '/does/not/exist'},
176 'uri_for_action croaks on nonexistent path';