fix uri_for class method tests
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_uri_for_action.t
CommitLineData
ad1783ae 1use strict;
2use warnings;
3
4use FindBin;
42da66a9 5use lib "$FindBin::Bin/../lib";
ad1783ae 6
7use Test::More;
8
ad1783ae 9use_ok('TestApp');
10
11my $dispatcher = TestApp->dispatcher;
12
8b13f357 13#
14# Private Action
15#
ad1783ae 16my $private_action = $dispatcher->get_action_by_path(
17 '/class_forward_test_method'
18 );
19
20ok(!defined($dispatcher->uri_for_action($private_action)),
21 "Private action returns undef for URI");
22
8b13f357 23#
24# Path Action
25#
ad1783ae 26my $path_action = $dispatcher->get_action_by_path(
27 '/action/testrelative/relative'
28 );
29
30is($dispatcher->uri_for_action($path_action), "/action/relative/relative",
31 "Public path action returns correct URI");
32
33ok(!defined($dispatcher->uri_for_action($path_action, [ 'foo' ])),
34 "no URI returned for Path action when snippets are given");
35
8b13f357 36#
8b13f357 37# Index Action
38#
ad1783ae 39my $index_action = $dispatcher->get_action_by_path(
40 '/action/index/index'
41 );
42
43ok(!defined($dispatcher->uri_for_action($index_action, [ 'foo' ])),
44 "no URI returned for index action when snippets are given");
45
46is($dispatcher->uri_for_action($index_action),
47 "/action/index",
48 "index action returns correct path");
49
8b13f357 50#
51# Chained Action
52#
5882c86e 53my $chained_action = $dispatcher->get_action_by_path(
54 '/action/chained/endpoint',
792b40ac 55 );
56
5882c86e 57ok(!defined($dispatcher->uri_for_action($chained_action)),
58 "Chained action without captures returns undef");
792b40ac 59
5882c86e 60ok(!defined($dispatcher->uri_for_action($chained_action, [ 1, 2 ])),
61 "Chained action with too many captures returns undef");
792b40ac 62
5882c86e 63is($dispatcher->uri_for_action($chained_action, [ 1 ]),
64 "/chained/foo/1/end",
65 "Chained action with correct captures returns correct path");
792b40ac 66
8b13f357 67#
68# Tests with Context
69#
ad1783ae 70my $request = Catalyst::Request->new( {
7c1c4dc6 71 _log => Catalyst::Log->new,
ad1783ae 72 base => URI->new('http://127.0.0.1/foo')
73 } );
74
75my $context = TestApp->new( {
76 request => $request,
77 namespace => 'yada',
78 } );
79
6c217fb3 80is($context->uri_for($context->controller('Action::Chained')->action_for('endpoint'), [ 1 ]),
81 'http://127.0.0.1/foo/chained/foo/1/end',
950282bc 82 "uri_for a controller and action");
83
6c217fb3 84is( $context->uri_for_action( '/action/chained/endpoint', [ 1 ] ),
85 'http://127.0.0.1/foo/chained/foo/1/end',
86 "uri_for a controller and action as string");
36bac472 87
6c217fb3 88is(TestApp->uri_for_action($context->controller('Action::Chained')->action_for('endpoint'), [ 1 ]),
89 '/chained/foo/1/end',
90 "uri_for a controller and action, called with only class name");
950282bc 91
6c217fb3 92is(TestApp->uri_for_action('/action/chained/endpoint', [ 1 ] ),
93 '/chained/foo/1/end',
94 "uri_for a controller and action as string, called with only class name");
950282bc 95
6c217fb3 96is(TestApp->uri_for_action( $chained_action, [ 1 ]),
97 '/chained/foo/1/end',
98 "uri_for action via dispatcher, called with only class name");
950282bc 99
7069eab5 100is($context->uri_for($context->controller('Action')),
101 "http://127.0.0.1/foo/yada/action/",
102 "uri_for a controller");
103
ad1783ae 104is($context->uri_for($path_action),
105 "http://127.0.0.1/foo/action/relative/relative",
106 "uri_for correct for path action");
107
108is($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");
111
112ok(!defined($context->uri_for($path_action, [ 'blah' ])),
113 "no URI returned by uri_for for Path action with snippets");
114
5882c86e 115is($context->uri_for($chained_action, [ 1 ], 2, { q => 1 }),
116 "http://127.0.0.1/foo/chained/foo/1/end/2?q=1",
117 "uri_for correct for chained with captures, args and query");
8b13f357 118
119#
120# More Chained with Context Tests
121#
122{
833b385e 123 is( $context->uri_for_action( '/action/chained/endpoint2', [1,2], (3,4), { x => 5 } ),
8b13f357 124 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
833b385e 125 'uri_for_action correct for chained with multiple captures and args' );
8b13f357 126
0cff119a 127 is( $context->uri_for_action( '/action/chained/endpoint2', [1,2,3,4], { x => 5 } ),
128 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
129 'uri_for_action correct for chained with multiple captures and args combined' );
130
833b385e 131 is( $context->uri_for_action( '/action/chained/three_end', [1,2,3], (4,5,6) ),
8b13f357 132 'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
833b385e 133 'uri_for_action correct for chained with multiple capturing actions' );
8b13f357 134
0cff119a 135 is( $context->uri_for_action( '/action/chained/three_end', [1,2,3,4,5,6] ),
136 'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
137 'uri_for_action correct for chained with multiple capturing actions and args combined' );
138
833b385e 139 my $action_needs_two = '/action/chained/endpoint2';
88e5a8b0 140
833b385e 141 ok( ! defined( $context->uri_for_action($action_needs_two, [1], (2,3)) ),
142 'uri_for_action returns undef for not enough captures' );
88e5a8b0 143
833b385e 144 is( $context->uri_for_action($action_needs_two, [1,2], (2,3)),
8b13f357 145 'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
833b385e 146 'uri_for_action returns correct uri for correct captures' );
0cff119a 147
148 is( $context->uri_for_action($action_needs_two, [1,2,2,3]),
149 'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
150 'uri_for_action returns correct uri for correct captures and args combined' );
151
833b385e 152 ok( ! defined( $context->uri_for_action($action_needs_two, [1,2,3], (2,3)) ),
153 'uri_for_action returns undef for too many captures' );
88e5a8b0 154
833b385e 155 is( $context->uri_for_action($action_needs_two, [1,2], (3)),
8b13f357 156 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
833b385e 157 'uri_for_action returns uri with lesser args than specified on action' );
8b13f357 158
0cff119a 159 is( $context->uri_for_action($action_needs_two, [1,2,3]),
160 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
161 'uri_for_action returns uri with lesser args than specified on action with captures combined' );
162
833b385e 163 is( $context->uri_for_action($action_needs_two, [1,2], (3,4,5)),
8b13f357 164 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
833b385e 165 'uri_for_action returns uri with more args than specified on action' );
8b13f357 166
0cff119a 167 is( $context->uri_for_action($action_needs_two, [1,2,3,4,5]),
168 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
169 'uri_for_action returns uri with more args than specified on action with captures combined' );
170
833b385e 171 is( $context->uri_for_action($action_needs_two, [1,''], (3,4)),
8b13f357 172 'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
833b385e 173 'uri_for_action returns uri with empty capture on undef capture' );
8b13f357 174
0cff119a 175 is( $context->uri_for_action($action_needs_two, [1,'',3,4]),
176 'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
177 'uri_for_action returns uri with empty capture on undef capture and args combined' );
178
833b385e 179 is( $context->uri_for_action($action_needs_two, [1,2], ('',3)),
8b13f357 180 'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
833b385e 181 'uri_for_action returns uri with empty arg on undef argument' );
8b13f357 182
0cff119a 183 is( $context->uri_for_action($action_needs_two, [1,2,'',3]),
184 'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
185 'uri_for_action returns uri with empty arg on undef argument and args combined' );
186
833b385e 187 is( $context->uri_for_action($action_needs_two, [1,2], (3,'')),
8b13f357 188 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
833b385e 189 'uri_for_action returns uri with empty arg on undef last argument' );
8b13f357 190
0cff119a 191 is( $context->uri_for_action($action_needs_two, [1,2,3,'']),
192 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
193 'uri_for_action returns uri with empty arg on undef last argument with captures combined' );
194
833b385e 195 my $complex_chained = '/action/chained/empty_chain_f';
196 is( $context->uri_for_action( $complex_chained, [23], (13), {q => 3} ),
8b13f357 197 'http://127.0.0.1/foo/chained/empty/23/13?q=3',
833b385e 198 'uri_for_action returns correct uri for chain with many empty path parts' );
0cff119a 199 is( $context->uri_for_action( $complex_chained, [23,13], {q => 3} ),
200 'http://127.0.0.1/foo/chained/empty/23/13?q=3',
201 'uri_for_action returns correct uri for chain with many empty path parts with captures and args combined' );
8b13f357 202
4ac0b9cb 203 eval { $context->uri_for_action( '/does/not/exist' ) };
204 like $@, qr{^Can't find action for path '/does/not/exist'},
205 'uri_for_action croaks on nonexistent path';
68dae239 206
4ac0b9cb 207}
8b13f357 208
0cff119a 209done_testing;
210