File: unit_core_uri_for_action.t

package info (click to toggle)
libcatalyst-perl 5.90132-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,016 kB
  • sloc: perl: 11,061; makefile: 7
file content (210 lines) | stat: -rw-r--r-- 8,258 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/../lib";

use Test::More;

use_ok('TestApp');

my $dispatcher = TestApp->dispatcher;

#
#   Private Action
#
my $private_action = $dispatcher->get_action_by_path(
                       '/class_forward_test_method'
                     );

ok(!defined($dispatcher->uri_for_action($private_action)),
   "Private action returns undef for URI");

#
#   Path Action
#
my $path_action = $dispatcher->get_action_by_path(
                    '/action/testrelative/relative'
                  );

is($dispatcher->uri_for_action($path_action), "/action/relative/relative",
   "Public path action returns correct URI");

ok(!defined($dispatcher->uri_for_action($path_action, [ 'foo' ])),
   "no URI returned for Path action when snippets are given");

#
#   Index Action
#
my $index_action = $dispatcher->get_action_by_path(
                     '/action/index/index'
                   );

ok(!defined($dispatcher->uri_for_action($index_action, [ 'foo' ])),
   "no URI returned for index action when snippets are given");

is($dispatcher->uri_for_action($index_action),
   "/action/index",
   "index action returns correct path");

#
#   Chained Action
#
my $chained_action = $dispatcher->get_action_by_path(
                       '/action/chained/endpoint',
                     );

ok(!defined($dispatcher->uri_for_action($chained_action)),
   "Chained action without captures returns undef");

ok(!defined($dispatcher->uri_for_action($chained_action, [ 1, 2 ])),
   "Chained action with too many captures returns undef");

is($dispatcher->uri_for_action($chained_action, [ 1 ]),
   "/chained/foo/1/end",
   "Chained action with correct captures returns correct path");

#
#   Tests with Context
#
my $request = Catalyst::Request->new( {
                _log => Catalyst::Log->new,
                base => URI->new('http://127.0.0.1/foo')
              } );

my $context = TestApp->new( {
                request => $request,
                namespace => 'yada',
              } );

is($context->uri_for($context->controller('Action::Chained')->action_for('endpoint'), [ 1 ]),
   'http://127.0.0.1/foo/chained/foo/1/end',
   "uri_for a controller and action");

is( $context->uri_for_action( '/action/chained/endpoint', [ 1 ] ),
    'http://127.0.0.1/foo/chained/foo/1/end',
    "uri_for a controller and action as string");

is(TestApp->uri_for_action($context->controller('Action::Chained')->action_for('endpoint'), [ 1 ]),
    '/chained/foo/1/end',
    "uri_for a controller and action, called with only class name");

is(TestApp->uri_for_action('/action/chained/endpoint', [ 1 ] ),
    '/chained/foo/1/end',
    "uri_for a controller and action as string, called with only class name");

is(TestApp->uri_for_action(  $chained_action, [ 1 ]),
    '/chained/foo/1/end',
    "uri_for action via dispatcher, called with only class name");

is($context->uri_for($context->controller('Action')),
   "http://127.0.0.1/foo/yada/action/",
   "uri_for a controller");

is($context->uri_for($path_action),
   "http://127.0.0.1/foo/action/relative/relative",
   "uri_for correct for path action");

is($context->uri_for($path_action, qw/one two/, { q => 1 }),
   "http://127.0.0.1/foo/action/relative/relative/one/two?q=1",
   "uri_for correct for path action with args and query");

ok(!defined($context->uri_for($path_action, [ 'blah' ])),
   "no URI returned by uri_for for Path action with snippets");

is($context->uri_for($chained_action, [ 1 ], 2, { q => 1 }),
   "http://127.0.0.1/foo/chained/foo/1/end/2?q=1",
   "uri_for correct for chained with captures, args and query");

#
#   More Chained with Context Tests
#
{
    is( $context->uri_for_action( '/action/chained/endpoint2', [1,2], (3,4), { x => 5 } ),
        'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
        'uri_for_action correct for chained with multiple captures and args' );

    is( $context->uri_for_action( '/action/chained/endpoint2', [1,2,3,4], { x => 5 } ),
        'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
        'uri_for_action correct for chained with multiple captures and args combined' );

    is( $context->uri_for_action( '/action/chained/three_end', [1,2,3], (4,5,6) ),
        'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
        'uri_for_action correct for chained with multiple capturing actions' );

    is( $context->uri_for_action( '/action/chained/three_end', [1,2,3,4,5,6] ),
        'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
        'uri_for_action correct for chained with multiple capturing actions and args combined' );

    my $action_needs_two = '/action/chained/endpoint2';

    ok( ! defined( $context->uri_for_action($action_needs_two, [1],     (2,3)) ),
        'uri_for_action returns undef for not enough captures' );

    is( $context->uri_for_action($action_needs_two,            [1,2],   (2,3)),
        'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
        'uri_for_action returns correct uri for correct captures' );

    is( $context->uri_for_action($action_needs_two,            [1,2,2,3]),
        'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
        'uri_for_action returns correct uri for correct captures and args combined' );

    ok( ! defined( $context->uri_for_action($action_needs_two, [1,2,3], (2,3)) ),
        'uri_for_action returns undef for too many captures' );

    is( $context->uri_for_action($action_needs_two, [1,2],   (3)),
        'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
        'uri_for_action returns uri with lesser args than specified on action' );

    is( $context->uri_for_action($action_needs_two, [1,2,3]),
        'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
        'uri_for_action returns uri with lesser args than specified on action with captures combined' );

    is( $context->uri_for_action($action_needs_two, [1,2],   (3,4,5)),
        'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
        'uri_for_action returns uri with more args than specified on action' );

    is( $context->uri_for_action($action_needs_two, [1,2,3,4,5]),
        'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
        'uri_for_action returns uri with more args than specified on action with captures combined' );

    is( $context->uri_for_action($action_needs_two, [1,''], (3,4)),
        'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
        'uri_for_action returns uri with empty capture on undef capture' );

    is( $context->uri_for_action($action_needs_two, [1,'',3,4]),
        'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
        'uri_for_action returns uri with empty capture on undef capture and args combined' );

    is( $context->uri_for_action($action_needs_two, [1,2], ('',3)),
        'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
        'uri_for_action returns uri with empty arg on undef argument' );

    is( $context->uri_for_action($action_needs_two, [1,2,'',3]),
        'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
        'uri_for_action returns uri with empty arg on undef argument and args combined' );

    is( $context->uri_for_action($action_needs_two, [1,2], (3,'')),
        'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
        'uri_for_action returns uri with empty arg on undef last argument' );

    is( $context->uri_for_action($action_needs_two, [1,2,3,'']),
        'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
        'uri_for_action returns uri with empty arg on undef last argument with captures combined' );

    my $complex_chained = '/action/chained/empty_chain_f';
    is( $context->uri_for_action( $complex_chained, [23], (13), {q => 3} ),
        'http://127.0.0.1/foo/chained/empty/23/13?q=3',
        'uri_for_action returns correct uri for chain with many empty path parts' );
    is( $context->uri_for_action( $complex_chained, [23,13], {q => 3} ),
        'http://127.0.0.1/foo/chained/empty/23/13?q=3',
        'uri_for_action returns correct uri for chain with many empty path parts with captures and args combined' );

    eval { $context->uri_for_action( '/does/not/exist' ) };
    like $@, qr{^Can't find action for path '/does/not/exist'},
        'uri_for_action croaks on nonexistent path';

}

done_testing;