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