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