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