Merged latest trunk changes to contextual_uri_for branch
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_uri_for_action.t
CommitLineData
ad1783ae 1#!perl
2
3use strict;
4use warnings;
5
6use FindBin;
42da66a9 7use lib "$FindBin::Bin/../lib";
ad1783ae 8
9use Test::More;
10
21c94d83 11plan tests => 42;
ad1783ae 12
13use_ok('TestApp');
14
15my $dispatcher = TestApp->dispatcher;
16
8b13f357 17#
18# Private Action
19#
ad1783ae 20my $private_action = $dispatcher->get_action_by_path(
21 '/class_forward_test_method'
22 );
23
24ok(!defined($dispatcher->uri_for_action($private_action)),
25 "Private action returns undef for URI");
26
8b13f357 27#
28# Path Action
29#
ad1783ae 30my $path_action = $dispatcher->get_action_by_path(
31 '/action/testrelative/relative'
32 );
33
34is($dispatcher->uri_for_action($path_action), "/action/relative/relative",
35 "Public path action returns correct URI");
36
37ok(!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 43my $regex_action = $dispatcher->get_action_by_path(
44 '/action/regexp/one'
45 );
46
47ok(!defined($dispatcher->uri_for_action($regex_action)),
48 "Regex action without captures returns undef");
49
792b40ac 50ok(!defined($dispatcher->uri_for_action($regex_action, [ 1, 2, 3 ])),
51 "Regex action with too many captures returns undef");
52
ad1783ae 53is($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 60my $index_action = $dispatcher->get_action_by_path(
61 '/action/index/index'
62 );
63
64ok(!defined($dispatcher->uri_for_action($index_action, [ 'foo' ])),
65 "no URI returned for index action when snippets are given");
66
67is($dispatcher->uri_for_action($index_action),
68 "/action/index",
69 "index action returns correct path");
70
8b13f357 71#
72# Chained Action
73#
5882c86e 74my $chained_action = $dispatcher->get_action_by_path(
75 '/action/chained/endpoint',
792b40ac 76 );
77
5882c86e 78ok(!defined($dispatcher->uri_for_action($chained_action)),
79 "Chained action without captures returns undef");
792b40ac 80
5882c86e 81ok(!defined($dispatcher->uri_for_action($chained_action, [ 1, 2 ])),
82 "Chained action with too many captures returns undef");
792b40ac 83
5882c86e 84is($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 91my $request = Catalyst::Request->new( {
92 base => URI->new('http://127.0.0.1/foo')
93 } );
94
95my $context = TestApp->new( {
96 request => $request,
97 namespace => 'yada',
98 } );
99
7069eab5 100is($context->uri_for($context->controller('Action')),
101 "http://127.0.0.1/foo/yada/action/",
102 "uri_for a controller");
103
ad1783ae 104is($context->uri_for($path_action),
105 "http://127.0.0.1/foo/action/relative/relative",
106 "uri_for correct for path action");
107
108is($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
112ok(!defined($context->uri_for($path_action, [ 'blah' ])),
113 "no URI returned by uri_for for Path action with snippets");
114
115is($context->uri_for($regex_action, [ 'foo', 123 ], qw/bar baz/, { q => 1 }),
116 "http://127.0.0.1/foo/action/regexp/foo/123/bar/baz?q=1",
117 "uri_for correct for regex with captures, args and query");
792b40ac 118
21c94d83 119is($context->uri_for($regex_action, 'foo', 123, { q => 1 }),
120 "http://127.0.0.1/foo/action/regexp/foo/123?q=1",
121 "uri_for correct for regex no captures no args with query");
122
5882c86e 123is($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");
8b13f357 126
21c94d83 127is($context->uri_for($chained_action, 1, 2, { q => 1 }),
128 "http://127.0.0.1/foo/chained/foo/1/end/2?q=1",
129 "uri_for correct for chained no captures with args and query");
130
8b13f357 131#
132# More Chained with Context Tests
133#
134{
833b385e 135 is( $context->uri_for_action( '/action/chained/endpoint2', [1,2], (3,4), { x => 5 } ),
8b13f357 136 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
833b385e 137 'uri_for_action correct for chained with multiple captures and args' );
8b13f357 138
21c94d83 139 is( $context->uri_for_action( '/action/chained/endpoint2', 1,2, (3,4), { x => 5 } ),
140 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
141 'uri_for_action correct for chained without captures with multiple args' );
142
833b385e 143 is( $context->uri_for_action( '/action/chained/three_end', [1,2,3], (4,5,6) ),
8b13f357 144 'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
833b385e 145 'uri_for_action correct for chained with multiple capturing actions' );
8b13f357 146
21c94d83 147 is( $context->uri_for_action( '/action/chained/three_end', 1,2,3, (4,5,6) ),
148 'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
149 'uri_for_action correct for chained no captures multiple capturing actions' );
150
833b385e 151 my $action_needs_two = '/action/chained/endpoint2';
8b13f357 152
833b385e 153 ok( ! defined( $context->uri_for_action($action_needs_two, [1], (2,3)) ),
154 'uri_for_action returns undef for not enough captures' );
21c94d83 155
156 is( $context->uri_for_action($action_needs_two, 1, (2,3)),
157 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
158 'uri_for_action returns correct uri no captures not enough args' );
159
833b385e 160 is( $context->uri_for_action($action_needs_two, [1,2], (2,3)),
8b13f357 161 'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
833b385e 162 'uri_for_action returns correct uri for correct captures' );
8b13f357 163
21c94d83 164 is( $context->uri_for_action($action_needs_two, qw(1 2 2 3)),
165 'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
166 'uri_for_action returns correct uri for correct captures/args total' );
167
833b385e 168 ok( ! defined( $context->uri_for_action($action_needs_two, [1,2,3], (2,3)) ),
169 'uri_for_action returns undef for too many captures' );
8b13f357 170
833b385e 171 is( $context->uri_for_action($action_needs_two, [1,2], (3)),
8b13f357 172 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
833b385e 173 'uri_for_action returns uri with lesser args than specified on action' );
8b13f357 174
21c94d83 175 is( $context->uri_for_action($action_needs_two, qw(1 2 3)),
176 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
177 'uri_for_action returns uri with lesser args and no captures' );
178
833b385e 179 is( $context->uri_for_action($action_needs_two, [1,2], (3,4,5)),
8b13f357 180 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
833b385e 181 'uri_for_action returns uri with more args than specified on action' );
8b13f357 182
21c94d83 183 is( $context->uri_for_action($action_needs_two, qw(1 2 3 4 5)),
184 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
185 'uri_for_action returns uri with more args and no captures' );
186
833b385e 187 is( $context->uri_for_action($action_needs_two, [1,''], (3,4)),
8b13f357 188 'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
833b385e 189 'uri_for_action returns uri with empty capture on undef capture' );
8b13f357 190
21c94d83 191 is( $context->uri_for_action($action_needs_two, 1, '', 3, 4),
192 'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
193 'uri_for_action returns uri with no captures and empty arg' );
194
833b385e 195 is( $context->uri_for_action($action_needs_two, [1,2], ('',3)),
8b13f357 196 'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
833b385e 197 'uri_for_action returns uri with empty arg on undef argument' );
8b13f357 198
21c94d83 199 is( $context->uri_for_action($action_needs_two, 1, 2, '', 3),
200 'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
201 'uri_for_action returns uri no captures with empty arg on undef argument' );
202
833b385e 203 is( $context->uri_for_action($action_needs_two, [1,2], (3,'')),
8b13f357 204 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
833b385e 205 'uri_for_action returns uri with empty arg on undef last argument' );
8b13f357 206
21c94d83 207 is( $context->uri_for_action($action_needs_two, 1, 2, 3, ''),
208 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
209 'uri_for_action returns uri no captures empty arg undef last argument' );
210
833b385e 211 my $complex_chained = '/action/chained/empty_chain_f';
212 is( $context->uri_for_action( $complex_chained, [23], (13), {q => 3} ),
8b13f357 213 'http://127.0.0.1/foo/chained/empty/23/13?q=3',
833b385e 214 'uri_for_action returns correct uri for chain with many empty path parts' );
8b13f357 215
21c94d83 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 no captures empty path parts' );
219
4ac0b9cb 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';
68dae239 223
4ac0b9cb 224}
8b13f357 225