allow uri_for_action to be called with captures and args in a single arrayref
[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
76988362 11plan tests => 39;
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
76cd735d 24warn $dispatcher->uri_for_action($private_action);
25
ad1783ae 26ok(!defined($dispatcher->uri_for_action($private_action)),
27 "Private action returns undef for URI");
28
8b13f357 29#
30# Path Action
31#
ad1783ae 32my $path_action = $dispatcher->get_action_by_path(
33 '/action/testrelative/relative'
34 );
35
36is($dispatcher->uri_for_action($path_action), "/action/relative/relative",
37 "Public path action returns correct URI");
38
39ok(!defined($dispatcher->uri_for_action($path_action, [ 'foo' ])),
40 "no URI returned for Path action when snippets are given");
41
8b13f357 42#
43# Regex Action
44#
ad1783ae 45my $regex_action = $dispatcher->get_action_by_path(
46 '/action/regexp/one'
47 );
48
49ok(!defined($dispatcher->uri_for_action($regex_action)),
50 "Regex action without captures returns undef");
51
792b40ac 52ok(!defined($dispatcher->uri_for_action($regex_action, [ 1, 2, 3 ])),
53 "Regex action with too many captures returns undef");
54
ad1783ae 55is($dispatcher->uri_for_action($regex_action, [ 'foo', 123 ]),
56 "/action/regexp/foo/123",
57 "Regex action interpolates captures correctly");
58
8b13f357 59#
60# Index Action
61#
ad1783ae 62my $index_action = $dispatcher->get_action_by_path(
63 '/action/index/index'
64 );
65
66ok(!defined($dispatcher->uri_for_action($index_action, [ 'foo' ])),
67 "no URI returned for index action when snippets are given");
68
69is($dispatcher->uri_for_action($index_action),
70 "/action/index",
71 "index action returns correct path");
72
8b13f357 73#
74# Chained Action
75#
5882c86e 76my $chained_action = $dispatcher->get_action_by_path(
77 '/action/chained/endpoint',
792b40ac 78 );
79
5882c86e 80ok(!defined($dispatcher->uri_for_action($chained_action)),
81 "Chained action without captures returns undef");
792b40ac 82
5882c86e 83ok(!defined($dispatcher->uri_for_action($chained_action, [ 1, 2 ])),
84 "Chained action with too many captures returns undef");
792b40ac 85
5882c86e 86is($dispatcher->uri_for_action($chained_action, [ 1 ]),
87 "/chained/foo/1/end",
88 "Chained action with correct captures returns correct path");
792b40ac 89
8b13f357 90#
91# Tests with Context
92#
ad1783ae 93my $request = Catalyst::Request->new( {
94 base => URI->new('http://127.0.0.1/foo')
95 } );
96
97my $context = TestApp->new( {
98 request => $request,
99 namespace => 'yada',
100 } );
101
7069eab5 102is($context->uri_for($context->controller('Action')),
103 "http://127.0.0.1/foo/yada/action/",
104 "uri_for a controller");
105
ad1783ae 106is($context->uri_for($path_action),
107 "http://127.0.0.1/foo/action/relative/relative",
108 "uri_for correct for path action");
109
110is($context->uri_for($path_action, qw/one two/, { q => 1 }),
111 "http://127.0.0.1/foo/action/relative/relative/one/two?q=1",
112 "uri_for correct for path action with args and query");
113
114ok(!defined($context->uri_for($path_action, [ 'blah' ])),
115 "no URI returned by uri_for for Path action with snippets");
116
117is($context->uri_for($regex_action, [ 'foo', 123 ], qw/bar baz/, { q => 1 }),
118 "http://127.0.0.1/foo/action/regexp/foo/123/bar/baz?q=1",
119 "uri_for correct for regex with captures, args and query");
792b40ac 120
5882c86e 121is($context->uri_for($chained_action, [ 1 ], 2, { q => 1 }),
122 "http://127.0.0.1/foo/chained/foo/1/end/2?q=1",
123 "uri_for correct for chained with captures, args and query");
8b13f357 124
125#
126# More Chained with Context Tests
127#
128{
833b385e 129 is( $context->uri_for_action( '/action/chained/endpoint2', [1,2], (3,4), { x => 5 } ),
8b13f357 130 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
833b385e 131 'uri_for_action correct for chained with multiple captures and args' );
8b13f357 132
76988362 133 is( $context->uri_for_action( '/action/chained/endpoint2', [1,2,3,4], { x => 5 } ),
134 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
135 'uri_for_action correct for chained with multiple captures and args combined' );
136
833b385e 137 is( $context->uri_for_action( '/action/chained/three_end', [1,2,3], (4,5,6) ),
8b13f357 138 'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
833b385e 139 'uri_for_action correct for chained with multiple capturing actions' );
8b13f357 140
76988362 141 is( $context->uri_for_action( '/action/chained/three_end', [1,2,3,4,5,6] ),
142 'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
143 'uri_for_action correct for chained with multiple capturing actions and args combined' );
144
833b385e 145 my $action_needs_two = '/action/chained/endpoint2';
8b13f357 146
833b385e 147 ok( ! defined( $context->uri_for_action($action_needs_two, [1], (2,3)) ),
148 'uri_for_action returns undef for not enough captures' );
8b13f357 149
833b385e 150 is( $context->uri_for_action($action_needs_two, [1,2], (2,3)),
8b13f357 151 'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
833b385e 152 'uri_for_action returns correct uri for correct captures' );
76988362 153
154 is( $context->uri_for_action($action_needs_two, [1,2,2,3]),
155 'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
156 'uri_for_action returns correct uri for correct captures and args combined' );
157
833b385e 158 ok( ! defined( $context->uri_for_action($action_needs_two, [1,2,3], (2,3)) ),
159 'uri_for_action returns undef for too many captures' );
8b13f357 160
833b385e 161 is( $context->uri_for_action($action_needs_two, [1,2], (3)),
8b13f357 162 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
833b385e 163 'uri_for_action returns uri with lesser args than specified on action' );
8b13f357 164
76988362 165 is( $context->uri_for_action($action_needs_two, [1,2,3]),
166 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
167 'uri_for_action returns uri with lesser args than specified on action with captures combined' );
168
833b385e 169 is( $context->uri_for_action($action_needs_two, [1,2], (3,4,5)),
8b13f357 170 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
833b385e 171 'uri_for_action returns uri with more args than specified on action' );
8b13f357 172
76988362 173 is( $context->uri_for_action($action_needs_two, [1,2,3,4,5]),
174 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
175 'uri_for_action returns uri with more args than specified on action with captures combined' );
176
833b385e 177 is( $context->uri_for_action($action_needs_two, [1,''], (3,4)),
8b13f357 178 'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
833b385e 179 'uri_for_action returns uri with empty capture on undef capture' );
8b13f357 180
76988362 181 is( $context->uri_for_action($action_needs_two, [1,'',3,4]),
182 'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
183 'uri_for_action returns uri with empty capture on undef capture and args combined' );
184
833b385e 185 is( $context->uri_for_action($action_needs_two, [1,2], ('',3)),
8b13f357 186 'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
833b385e 187 'uri_for_action returns uri with empty arg on undef argument' );
8b13f357 188
76988362 189 is( $context->uri_for_action($action_needs_two, [1,2,'',3]),
190 'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
191 'uri_for_action returns uri with empty arg on undef argument and args combined' );
192
833b385e 193 is( $context->uri_for_action($action_needs_two, [1,2], (3,'')),
8b13f357 194 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
833b385e 195 'uri_for_action returns uri with empty arg on undef last argument' );
8b13f357 196
76988362 197 is( $context->uri_for_action($action_needs_two, [1,2,3,'']),
198 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
199 'uri_for_action returns uri with empty arg on undef last argument with captures combined' );
200
833b385e 201 my $complex_chained = '/action/chained/empty_chain_f';
202 is( $context->uri_for_action( $complex_chained, [23], (13), {q => 3} ),
8b13f357 203 'http://127.0.0.1/foo/chained/empty/23/13?q=3',
833b385e 204 'uri_for_action returns correct uri for chain with many empty path parts' );
76988362 205 is( $context->uri_for_action( $complex_chained, [23,13], {q => 3} ),
206 'http://127.0.0.1/foo/chained/empty/23/13?q=3',
207 'uri_for_action returns correct uri for chain with many empty path parts with captures and args combined' );
8b13f357 208
4ac0b9cb 209 eval { $context->uri_for_action( '/does/not/exist' ) };
210 like $@, qr{^Can't find action for path '/does/not/exist'},
211 'uri_for_action croaks on nonexistent path';
68dae239 212
4ac0b9cb 213}
8b13f357 214