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