Merge branch captures_and_args_combined
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_uri_for_action.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use FindBin;
7 use lib "$FindBin::Bin/../lib";
8
9 use Test::More 0.88;
10
11 use_ok('TestApp');
12
13 my $dispatcher = TestApp->dispatcher;
14
15 #
16 #   Private Action
17 #
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
25 #
26 #   Path Action
27 #
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
38 #
39 #   Regex Action
40 #
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
48 ok(!defined($dispatcher->uri_for_action($regex_action, [ 1, 2, 3 ])),
49    "Regex action with too many captures returns undef");
50
51 is($dispatcher->uri_for_action($regex_action, [ 'foo', 123 ]),
52    "/action/regexp/foo/123",
53    "Regex action interpolates captures correctly");
54
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
70 #
71 #   Index Action
72 #
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
84 #
85 #   Chained Action
86 #
87 my $chained_action = $dispatcher->get_action_by_path(
88                        '/action/chained/endpoint',
89                      );
90
91 ok(!defined($dispatcher->uri_for_action($chained_action)),
92    "Chained action without captures returns undef");
93
94 ok(!defined($dispatcher->uri_for_action($chained_action, [ 1, 2 ])),
95    "Chained action with too many captures returns undef");
96
97 is($dispatcher->uri_for_action($chained_action, [ 1 ]),
98    "/chained/foo/1/end",
99    "Chained action with correct captures returns correct path");
100
101 #
102 #   Tests with Context
103 #
104 my $request = Catalyst::Request->new( {
105                 base => URI->new('http://127.0.0.1/foo')
106               } );
107
108 my $context = TestApp->new( {
109                 request => $request,
110                 namespace => 'yada',
111               } );
112
113 is($context->uri_for($context->controller('Action')),
114    "http://127.0.0.1/foo/yada/action/",
115    "uri_for a controller");
116
117 is($context->uri_for($path_action),
118    "http://127.0.0.1/foo/action/relative/relative",
119    "uri_for correct for path action");
120
121 is($context->uri_for($path_action, qw/one two/, { q => 1 }),
122    "http://127.0.0.1/foo/action/relative/relative/one/two?q=1",
123    "uri_for correct for path action with args and query");
124
125 ok(!defined($context->uri_for($path_action, [ 'blah' ])),
126    "no URI returned by uri_for for Path action with snippets");
127
128 is($context->uri_for($regex_action, [ 'foo', 123 ], qw/bar baz/, { q => 1 }),
129    "http://127.0.0.1/foo/action/regexp/foo/123/bar/baz?q=1",
130    "uri_for correct for regex with captures, args and query");
131
132 is($context->uri_for($chained_action, [ 1 ], 2, { q => 1 }),
133    "http://127.0.0.1/foo/chained/foo/1/end/2?q=1",
134    "uri_for correct for chained with captures, args and query");
135
136 #
137 #   More Chained with Context Tests
138 #
139 {
140     is( $context->uri_for_action( '/action/chained/endpoint2', [1,2], (3,4), { x => 5 } ),
141         'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
142         'uri_for_action correct for chained with multiple captures and args' );
143
144     is( $context->uri_for_action( '/action/chained/endpoint2', [1,2,3,4], { x => 5 } ),
145         'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
146         'uri_for_action correct for chained with multiple captures and args combined' );
147
148     is( $context->uri_for_action( '/action/chained/three_end', [1,2,3], (4,5,6) ),
149         'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
150         'uri_for_action correct for chained with multiple capturing actions' );
151
152     is( $context->uri_for_action( '/action/chained/three_end', [1,2,3,4,5,6] ),
153         'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
154         'uri_for_action correct for chained with multiple capturing actions and args combined' );
155
156     my $action_needs_two = '/action/chained/endpoint2';
157     
158     ok( ! defined( $context->uri_for_action($action_needs_two, [1],     (2,3)) ),
159         'uri_for_action returns undef for not enough captures' );
160         
161     is( $context->uri_for_action($action_needs_two,            [1,2],   (2,3)),
162         'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
163         'uri_for_action returns correct uri for correct captures' );
164
165     is( $context->uri_for_action($action_needs_two,            [1,2,2,3]),
166         'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
167         'uri_for_action returns correct uri for correct captures and args combined' );
168
169     ok( ! defined( $context->uri_for_action($action_needs_two, [1,2,3], (2,3)) ),
170         'uri_for_action returns undef for too many captures' );
171     
172     is( $context->uri_for_action($action_needs_two, [1,2],   (3)),
173         'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
174         'uri_for_action returns uri with lesser args than specified on action' );
175
176     is( $context->uri_for_action($action_needs_two, [1,2,3]),
177         'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
178         'uri_for_action returns uri with lesser args than specified on action with captures combined' );
179
180     is( $context->uri_for_action($action_needs_two, [1,2],   (3,4,5)),
181         'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
182         'uri_for_action returns uri with more args than specified on action' );
183
184     is( $context->uri_for_action($action_needs_two, [1,2,3,4,5]),
185         'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
186         'uri_for_action returns uri with more args than specified on action with captures combined' );
187
188     is( $context->uri_for_action($action_needs_two, [1,''], (3,4)),
189         'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
190         'uri_for_action returns uri with empty capture on undef capture' );
191
192     is( $context->uri_for_action($action_needs_two, [1,'',3,4]),
193         'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
194         'uri_for_action returns uri with empty capture on undef capture and args combined' );
195
196     is( $context->uri_for_action($action_needs_two, [1,2], ('',3)),
197         'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
198         'uri_for_action returns uri with empty arg on undef argument' );
199
200     is( $context->uri_for_action($action_needs_two, [1,2,'',3]),
201         'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
202         'uri_for_action returns uri with empty arg on undef argument and args combined' );
203
204     is( $context->uri_for_action($action_needs_two, [1,2], (3,'')),
205         'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
206         'uri_for_action returns uri with empty arg on undef last argument' );
207
208     is( $context->uri_for_action($action_needs_two, [1,2,3,'']),
209         'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
210         'uri_for_action returns uri with empty arg on undef last argument with captures combined' );
211
212     my $complex_chained = '/action/chained/empty_chain_f';
213     is( $context->uri_for_action( $complex_chained, [23], (13), {q => 3} ),
214         'http://127.0.0.1/foo/chained/empty/23/13?q=3',
215         'uri_for_action returns correct uri for chain with many empty path parts' );
216     is( $context->uri_for_action( $complex_chained, [23,13], {q => 3} ),
217         'http://127.0.0.1/foo/chained/empty/23/13?q=3',
218         'uri_for_action returns correct uri for chain with many empty path parts with captures and args combined' );
219
220     eval { $context->uri_for_action( '/does/not/exist' ) };
221     like $@, qr{^Can't find action for path '/does/not/exist'},
222         'uri_for_action croaks on nonexistent path';
223
224 }
225
226 done_testing;
227