Fix not stripping backslashes in DispatchType::Regex::uri_for_action
[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 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
72 #
73 #   Index Action
74 #
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
86 #
87 #   Chained Action
88 #
89 my $chained_action = $dispatcher->get_action_by_path(
90                        '/action/chained/endpoint',
91                      );
92
93 ok(!defined($dispatcher->uri_for_action($chained_action)),
94    "Chained action without captures returns undef");
95
96 ok(!defined($dispatcher->uri_for_action($chained_action, [ 1, 2 ])),
97    "Chained action with too many captures returns undef");
98
99 is($dispatcher->uri_for_action($chained_action, [ 1 ]),
100    "/chained/foo/1/end",
101    "Chained action with correct captures returns correct path");
102
103 #
104 #   Tests with Context
105 #
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
115 is($context->uri_for($context->controller('Action')),
116    "http://127.0.0.1/foo/yada/action/",
117    "uri_for a controller");
118
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");
133
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");
137
138 #
139 #   More Chained with Context Tests
140 #
141 {
142     is( $context->uri_for_action( '/action/chained/endpoint2', [1,2], (3,4), { x => 5 } ),
143         'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
144         'uri_for_action correct for chained with multiple captures and args' );
145
146     is( $context->uri_for_action( '/action/chained/three_end', [1,2,3], (4,5,6) ),
147         'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
148         'uri_for_action correct for chained with multiple capturing actions' );
149
150     my $action_needs_two = '/action/chained/endpoint2';
151     
152     ok( ! defined( $context->uri_for_action($action_needs_two, [1],     (2,3)) ),
153         'uri_for_action returns undef for not enough captures' );
154         
155     is( $context->uri_for_action($action_needs_two,            [1,2],   (2,3)),
156         'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
157         'uri_for_action returns correct uri for correct captures' );
158         
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' );
161     
162     is( $context->uri_for_action($action_needs_two, [1,2],   (3)),
163         'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
164         'uri_for_action returns uri with lesser args than specified on action' );
165
166     is( $context->uri_for_action($action_needs_two, [1,2],   (3,4,5)),
167         'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
168         'uri_for_action returns uri with more args than specified on action' );
169
170     is( $context->uri_for_action($action_needs_two, [1,''], (3,4)),
171         'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
172         'uri_for_action returns uri with empty capture on undef capture' );
173
174     is( $context->uri_for_action($action_needs_two, [1,2], ('',3)),
175         'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
176         'uri_for_action returns uri with empty arg on undef argument' );
177
178     is( $context->uri_for_action($action_needs_two, [1,2], (3,'')),
179         'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
180         'uri_for_action returns uri with empty arg on undef last argument' );
181
182     my $complex_chained = '/action/chained/empty_chain_f';
183     is( $context->uri_for_action( $complex_chained, [23], (13), {q => 3} ),
184         'http://127.0.0.1/foo/chained/empty/23/13?q=3',
185         'uri_for_action returns correct uri for chain with many empty path parts' );
186
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';
190
191 }
192