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 | |
4ac0b9cb |
11 | plan tests => 29; |
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 | |
8b13f357 |
57 | # |
58 | # Index Action |
59 | # |
ad1783ae |
60 | my $index_action = $dispatcher->get_action_by_path( |
61 | '/action/index/index' |
62 | ); |
63 | |
64 | ok(!defined($dispatcher->uri_for_action($index_action, [ 'foo' ])), |
65 | "no URI returned for index action when snippets are given"); |
66 | |
67 | is($dispatcher->uri_for_action($index_action), |
68 | "/action/index", |
69 | "index action returns correct path"); |
70 | |
8b13f357 |
71 | # |
72 | # Chained Action |
73 | # |
5882c86e |
74 | my $chained_action = $dispatcher->get_action_by_path( |
75 | '/action/chained/endpoint', |
792b40ac |
76 | ); |
77 | |
5882c86e |
78 | ok(!defined($dispatcher->uri_for_action($chained_action)), |
79 | "Chained action without captures returns undef"); |
792b40ac |
80 | |
5882c86e |
81 | ok(!defined($dispatcher->uri_for_action($chained_action, [ 1, 2 ])), |
82 | "Chained action with too many captures returns undef"); |
792b40ac |
83 | |
5882c86e |
84 | is($dispatcher->uri_for_action($chained_action, [ 1 ]), |
85 | "/chained/foo/1/end", |
86 | "Chained action with correct captures returns correct path"); |
792b40ac |
87 | |
8b13f357 |
88 | # |
89 | # Tests with Context |
90 | # |
ad1783ae |
91 | my $request = Catalyst::Request->new( { |
92 | base => URI->new('http://127.0.0.1/foo') |
93 | } ); |
94 | |
95 | my $context = TestApp->new( { |
96 | request => $request, |
97 | namespace => 'yada', |
98 | } ); |
99 | |
100 | is($context->uri_for($path_action), |
101 | "http://127.0.0.1/foo/action/relative/relative", |
102 | "uri_for correct for path action"); |
103 | |
104 | is($context->uri_for($path_action, qw/one two/, { q => 1 }), |
105 | "http://127.0.0.1/foo/action/relative/relative/one/two?q=1", |
106 | "uri_for correct for path action with args and query"); |
107 | |
108 | ok(!defined($context->uri_for($path_action, [ 'blah' ])), |
109 | "no URI returned by uri_for for Path action with snippets"); |
110 | |
111 | is($context->uri_for($regex_action, [ 'foo', 123 ], qw/bar baz/, { q => 1 }), |
112 | "http://127.0.0.1/foo/action/regexp/foo/123/bar/baz?q=1", |
113 | "uri_for correct for regex with captures, args and query"); |
792b40ac |
114 | |
5882c86e |
115 | is($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 | |
833b385e |
127 | is( $context->uri_for_action( '/action/chained/three_end', [1,2,3], (4,5,6) ), |
8b13f357 |
128 | 'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6', |
833b385e |
129 | 'uri_for_action correct for chained with multiple capturing actions' ); |
8b13f357 |
130 | |
833b385e |
131 | my $action_needs_two = '/action/chained/endpoint2'; |
8b13f357 |
132 | |
833b385e |
133 | ok( ! defined( $context->uri_for_action($action_needs_two, [1], (2,3)) ), |
134 | 'uri_for_action returns undef for not enough captures' ); |
8b13f357 |
135 | |
833b385e |
136 | is( $context->uri_for_action($action_needs_two, [1,2], (2,3)), |
8b13f357 |
137 | 'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3', |
833b385e |
138 | 'uri_for_action returns correct uri for correct captures' ); |
8b13f357 |
139 | |
833b385e |
140 | ok( ! defined( $context->uri_for_action($action_needs_two, [1,2,3], (2,3)) ), |
141 | 'uri_for_action returns undef for too many captures' ); |
8b13f357 |
142 | |
833b385e |
143 | is( $context->uri_for_action($action_needs_two, [1,2], (3)), |
8b13f357 |
144 | 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3', |
833b385e |
145 | 'uri_for_action returns uri with lesser args than specified on action' ); |
8b13f357 |
146 | |
833b385e |
147 | is( $context->uri_for_action($action_needs_two, [1,2], (3,4,5)), |
8b13f357 |
148 | 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5', |
833b385e |
149 | 'uri_for_action returns uri with more args than specified on action' ); |
8b13f357 |
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 | |
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 empty arg on undef argument' ); |
8b13f357 |
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 last argument' ); |
8b13f357 |
162 | |
833b385e |
163 | my $complex_chained = '/action/chained/empty_chain_f'; |
164 | is( $context->uri_for_action( $complex_chained, [23], (13), {q => 3} ), |
8b13f357 |
165 | 'http://127.0.0.1/foo/chained/empty/23/13?q=3', |
833b385e |
166 | 'uri_for_action returns correct uri for chain with many empty path parts' ); |
8b13f357 |
167 | |
4ac0b9cb |
168 | eval { $context->uri_for_action( '/does/not/exist' ) }; |
169 | like $@, qr{^Can't find action for path '/does/not/exist'}, |
170 | 'uri_for_action croaks on nonexistent path'; |
68dae239 |
171 | |
4ac0b9cb |
172 | } |
8b13f357 |
173 | |