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