summaryrefslogtreecommitdiff
path: root/t/dsl/uri_for_route.t
blob: c327b884ba94b30ec8c2192986f4c86f3226a2be (plain)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use strict;
use warnings;
use Test::More 'tests' => 3;
use Plack::Test;
use Plack::Builder;
use HTTP::Request::Common;
use JSON::MaybeXS;

{
    package App;
    use Dancer2;
    our $tested;

    # Static with route params
    # Static with code
    # Static with options and code
    get 'view_entry_static1' => '/view1/:id' => sub {1};
    get 'view_entry_static2' => '/view2/:id' => { 'user_agent' => 'UA/1.0' }, sub {1};

    # static with typed route param
    get 'view_user' => '/:prefix/user/:username[Str]' => sub {1};

    # splat / megasplat
    get 'view_entry_splat' => '/viewsplat/*/*/**' => sub {1};

    # Mixed with splat/megasplat
    # Different method
    patch 'view_entry_mixed' => '/view_mixed/*/**/:id' => sub {1};

    # Regexp - fails
    get 'view_entry_regexp1' => qr{/rview1/[0-9]+} => sub {1};

    post '/uri_for_route' => sub {
        my $params = JSON::MaybeXS::decode_json( request->content );
        return uri_for_route(
            $params->{'route_name'},
            $params->{'route_params'},
            $params->{'query_params'} // {},
            !!$params->{'dont_escape'},
        );
    };

    get '/fail_uri_for_route' => sub {
        my $failed = 0;
        eval {
            uri_for_route('vvv');
            1;
        } or do {
            ::like(
                $@,
                qr/\QCannot find route named 'vvv'\E/xms,
                'Cannot retrieve nonexistent route',
            );

            $failed++;
        };

        return $failed;
    };

    get '/fail_uri_for_route_splat_args' => sub {
        my $failed = 0;
        eval {
            uri_for_route(
                'view_entry_splat',
                ['foo'],
            );

            1;
        } or do {
            ::like(
                $@,
                qr/\QMismatch in amount of splat args and splat elements\E/xms,
                'Cannot handle mismatched splat args and elements',
            );

            $failed++;
        };

        return $failed;
    };

    get '/fail_uri_for_route_leftovers' => sub {
        my $failed = 0;
        eval {
            uri_for_route('view_entry_static1');
            1;
        } or do {
            my $msg = 'Route view_entry_static1 uses the parameter \'id\', '
                    . 'which was not provided';

            ::like(
                $@,
                qr/\Q$msg\E/xms,
                'Cannot handle leftover route parameters',
            );

            $failed++;
        };

        return $failed;
    };

    # Error defining two routes with the same name, regardless of method
    eval {
        get 'view_entry_splat' => '/' => sub {1};
        1;
    } or do {
        ::like(
            $@,
            qr/\QRoute with this name (view_entry_splat) already exists\E/xms,
            'Cannot register two routes with same name',
        );

        $tested = 1;
    };
}

sub test_app {
    my ( $app, $mount_path ) = @_;

    my $prefix = 'http://localhost';
    $mount_path
        and $prefix .= $mount_path;

    my ( $path, $res );

    # Test static paths
    foreach my $idx ( 1 .. 2 ) {
        $res = $app->request(
            POST(
                "$prefix/uri_for_route",
                'Content' => JSON::MaybeXS::encode_json({
                    'route_name'   => "view_entry_static$idx",
                    'route_params' => { 'id'  => $idx },
                    'query_params' => { 'foo' => $idx },
                }),
            )
        );

        $path = "$prefix/view$idx/$idx?foo=$idx";
        ok( $res->is_success, 'Successful request' );
        is( $res->content, $path, "Correct path: $path" );
    }

    # Test splat + megasplat
    $res = $app->request(
        POST(
            "$prefix/uri_for_route",
            'Content' => JSON::MaybeXS::encode_json({
                'route_name'   => 'view_entry_splat',
                'route_params' => [ 'foo', 'bar', [ 'baz', 'quux' ] ],
                'query_params' => { 'id' => 'di' },
            }),
        )
    );

    $path = "$prefix/viewsplat/foo/bar/baz/quux?id=di";
    ok( $res->is_success, 'Successful request' );
    is( $res->content, $path, "Correct path: $path" );

    # Test mixed
    $res = $app->request(
        POST(
            "$prefix/uri_for_route",
            'Content' => JSON::MaybeXS::encode_json(
                {   'route_name'   => 'view_entry_mixed',
                    'route_params' => {
                        'id'    => 'di',
                        'splat' => ['foo', ['bar', 'baz']]
                    },
                    'query_params' => {'foo' => 'bar'},
                }
            ),
        )
    );

    $path = "$prefix/view_mixed/foo/bar/baz/di?foo=bar";
    ok( $res->is_success, 'Successful request' );
    is( $res->content, $path, "Correct path: $path" );

    # Test escaping
    $res = $app->request(
        POST(
            "$prefix/uri_for_route",
            'Content' => JSON::MaybeXS::encode_json({
                'route_name'   => 'view_entry_static1',
                'route_params' => { 'id' => '!@£$%' },
            }),
        )
    );

    $path = "$prefix/view1/!@%C3%82%C2%A3\$%";
    ok( $res->is_success, 'Successful request' );
    is( $res->content, $path, "Correct path: $path" );

    # Test nonexistent route name
    $res = $app->request( GET "$prefix/fail_uri_for_route" );
    ok( $res->is_success, 'Successful request' );
    is( $res->content, '1', 'Successfully tested nonexistent failure mode' );

    # Test splat + megasplat (incorrect amount)
    $res = $app->request( GET "$prefix/fail_uri_for_route_splat_args" );
    ok( $res->is_success, 'Successful request' );
    is( $res->content, '1', 'Successfully tested mismatch splat args/elements failure mode' );

    # Test mixed with not all filled (named args left)
    $res = $app->request( GET "$prefix/fail_uri_for_route_leftovers" );
    ok( $res->is_success, 'Successful request' );
    is( $res->content, '1', 'Successfully tested leftover args failure mode' );

    # Static with typed route parameters
    $res = $app->request(
        POST(
            "$prefix/uri_for_route",
            'Content' => JSON::MaybeXS::encode_json({
                'route_name' => 'view_user',
                'route_params' => { 'prefix' => 'foo', 'username' => 'sawyer' },
                'query_params' => { 'foo' => 1 },
            }),
        )
    );

    $path = "$prefix/foo/user/sawyer?foo=1";
    ok( $res->is_success, 'Successful request' );
    is( $res->content, $path, "Correct path for typed route param: $path" );
}

subtest 'Non-mounted app' => sub {
    my $app = Plack::Test->create( App->to_app );
    test_app($app);
    ok( $App::tested, 'Check for duplicate route names done successfully' );
};

subtest 'Mounted app' => sub {
    my $app = Plack::Test->create(
        builder {
            mount '/mount' => App->to_app;
            mount '/'      => sub {
                return { Plack::Response->new(200, [], ['OK'] ) }
            },
        }
    );

    test_app( $app, '/mount' );
};