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