7 use lib "$FindBin::Bin/lib";
15 my $dispatcher = TestApp->dispatcher;
20 my $private_action = $dispatcher->get_action_by_path(
21 '/class_forward_test_method'
24 ok(!defined($dispatcher->uri_for_action($private_action)),
25 "Private action returns undef for URI");
30 my $path_action = $dispatcher->get_action_by_path(
31 '/action/testrelative/relative'
34 is($dispatcher->uri_for_action($path_action), "/action/relative/relative",
35 "Public path action returns correct URI");
37 ok(!defined($dispatcher->uri_for_action($path_action, [ 'foo' ])),
38 "no URI returned for Path action when snippets are given");
43 my $regex_action = $dispatcher->get_action_by_path(
47 ok(!defined($dispatcher->uri_for_action($regex_action)),
48 "Regex action without captures returns undef");
50 ok(!defined($dispatcher->uri_for_action($regex_action, [ 1, 2, 3 ])),
51 "Regex action with too many captures returns undef");
53 is($dispatcher->uri_for_action($regex_action, [ 'foo', 123 ]),
54 "/action/regexp/foo/123",
55 "Regex action interpolates captures correctly");
60 my $index_action = $dispatcher->get_action_by_path(
64 ok(!defined($dispatcher->uri_for_action($index_action, [ 'foo' ])),
65 "no URI returned for index action when snippets are given");
67 is($dispatcher->uri_for_action($index_action),
69 "index action returns correct path");
74 my $chained_action = $dispatcher->get_action_by_path(
75 '/action/chained/endpoint',
78 ok(!defined($dispatcher->uri_for_action($chained_action)),
79 "Chained action without captures returns undef");
81 ok(!defined($dispatcher->uri_for_action($chained_action, [ 1, 2 ])),
82 "Chained action with too many captures returns undef");
84 is($dispatcher->uri_for_action($chained_action, [ 1 ]),
86 "Chained action with correct captures returns correct path");
91 my $request = Catalyst::Request->new( {
92 base => URI->new('http://127.0.0.1/foo')
95 my $context = TestApp->new( {
100 is($context->uri_for($path_action),
101 "http://127.0.0.1/foo/action/relative/relative",
102 "uri_for correct for path action");
104 is($context->uri_for($path_action, qw/one two/, { q => 1 }),
105 "http://127.0.0.1/foo/action/relative/relative/one/two?q=1",
106 "uri_for correct for path action with args and query");
108 ok(!defined($context->uri_for($path_action, [ 'blah' ])),
109 "no URI returned by uri_for for Path action with snippets");
111 is($context->uri_for($regex_action, [ 'foo', 123 ], qw/bar baz/, { q => 1 }),
112 "http://127.0.0.1/foo/action/regexp/foo/123/bar/baz?q=1",
113 "uri_for correct for regex with captures, args and query");
115 is($context->uri_for($chained_action, [ 1 ], 2, { q => 1 }),
116 "http://127.0.0.1/foo/chained/foo/1/end/2?q=1",
117 "uri_for correct for chained with captures, args and query");
120 # More Chained with Context Tests
123 sub __action { $dispatcher->get_action_by_path( @_ ) }
125 is( $context->uri_for( __action( '/action/chained/endpoint2' ), [1,2], (3,4), { x => 5 } ),
126 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
127 'uri_for correct for chained with multiple captures and args' );
129 is( $context->uri_for( __action( '/action/chained/three_end' ), [1,2,3], (4,5,6) ),
130 'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
131 'uri_for correct for chained with multiple capturing actions' );
133 my $action_needs_two = __action( '/action/chained/endpoint2' );
135 ok( ! defined( $context->uri_for($action_needs_two, [1], (2,3)) ),
136 'uri_for returns undef for not enough captures' );
138 is( $context->uri_for($action_needs_two, [1,2], (2,3)),
139 'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
140 'uri_for returns correct uri for correct captures' );
142 ok( ! defined( $context->uri_for($action_needs_two, [1,2,3], (2,3)) ),
143 'uri_for returns undef for too many captures' );
145 is( $context->uri_for($action_needs_two, [1,2], (3)),
146 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
147 'uri_for returns uri with lesser args than specified on action' );
149 is( $context->uri_for($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 returns uri with more args than specified on action' );
153 is( $context->uri_for($action_needs_two, [1,''], (3,4)),
154 'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
155 'uri_for returns uri with empty capture on undef capture' );
157 is( $context->uri_for($action_needs_two, [1,2], ('',3)),
158 'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
159 'uri_for returns uri with empty arg on undef argument' );
161 is( $context->uri_for($action_needs_two, [1,2], (3,'')),
162 'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
163 'uri_for returns uri with empty arg on undef last argument' );
165 my $complex_chained = __action( '/action/chained/empty_chain_f' );
166 is( $context->uri_for( $complex_chained, [23], (13), {q => 3} ),
167 'http://127.0.0.1/foo/chained/empty/23/13?q=3',
168 'uri_for returns correct uri for chain with many empty path parts' );