| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More 'tests' => 3; |
| 4 |
use Plack::Test; |
| 5 |
use Plack::Builder; |
| 6 |
use HTTP::Request::Common; |
| 7 |
use JSON::MaybeXS; |
| 8 |
|
| 9 |
{ |
| 10 |
package App; |
| 11 |
use Dancer2; |
| 12 |
our $tested; |
| 13 |
|
| 14 |
# Static with route params |
| 15 |
# Static with code |
| 16 |
# Static with options and code |
| 17 |
get 'view_entry_static1' => '/view1/:id' => sub {1}; |
| 18 |
get 'view_entry_static2' => '/view2/:id' => { 'user_agent' => 'UA/1.0' }, sub {1}; |
| 19 |
|
| 20 |
# static with typed route param |
| 21 |
get 'view_user' => '/:prefix/user/:username[Str]' => sub {1}; |
| 22 |
|
| 23 |
# splat / megasplat |
| 24 |
get 'view_entry_splat' => '/viewsplat/*/*/**' => sub {1}; |
| 25 |
|
| 26 |
# Mixed with splat/megasplat |
| 27 |
# Different method |
| 28 |
patch 'view_entry_mixed' => '/view_mixed/*/**/:id' => sub {1}; |
| 29 |
|
| 30 |
# Regexp - fails |
| 31 |
get 'view_entry_regexp1' => qr{/rview1/[0-9]+} => sub {1}; |
| 32 |
|
| 33 |
post '/uri_for_route' => sub { |
| 34 |
my $params = JSON::MaybeXS::decode_json( request->content ); |
| 35 |
return uri_for_route( |
| 36 |
$params->{'route_name'}, |
| 37 |
$params->{'route_params'}, |
| 38 |
$params->{'query_params'} // {}, |
| 39 |
!!$params->{'dont_escape'}, |
| 40 |
); |
| 41 |
}; |
| 42 |
|
| 43 |
get '/fail_uri_for_route' => sub { |
| 44 |
my $failed = 0; |
| 45 |
eval { |
| 46 |
uri_for_route('vvv'); |
| 47 |
1; |
| 48 |
} or do { |
| 49 |
::like( |
| 50 |
$@, |
| 51 |
qr/\QCannot find route named 'vvv'\E/xms, |
| 52 |
'Cannot retrieve nonexistent route', |
| 53 |
); |
| 54 |
|
| 55 |
$failed++; |
| 56 |
}; |
| 57 |
|
| 58 |
return $failed; |
| 59 |
}; |
| 60 |
|
| 61 |
get '/fail_uri_for_route_splat_args' => sub { |
| 62 |
my $failed = 0; |
| 63 |
eval { |
| 64 |
uri_for_route( |
| 65 |
'view_entry_splat', |
| 66 |
['foo'], |
| 67 |
); |
| 68 |
|
| 69 |
1; |
| 70 |
} or do { |
| 71 |
::like( |
| 72 |
$@, |
| 73 |
qr/\QMismatch in amount of splat args and splat elements\E/xms, |
| 74 |
'Cannot handle mismatched splat args and elements', |
| 75 |
); |
| 76 |
|
| 77 |
$failed++; |
| 78 |
}; |
| 79 |
|
| 80 |
return $failed; |
| 81 |
}; |
| 82 |
|
| 83 |
get '/fail_uri_for_route_leftovers' => sub { |
| 84 |
my $failed = 0; |
| 85 |
eval { |
| 86 |
uri_for_route('view_entry_static1'); |
| 87 |
1; |
| 88 |
} or do { |
| 89 |
my $msg = 'Route view_entry_static1 uses the parameter \'id\', ' |
| 90 |
. 'which was not provided'; |
| 91 |
|
| 92 |
::like( |
| 93 |
$@, |
| 94 |
qr/\Q$msg\E/xms, |
| 95 |
'Cannot handle leftover route parameters', |
| 96 |
); |
| 97 |
|
| 98 |
$failed++; |
| 99 |
}; |
| 100 |
|
| 101 |
return $failed; |
| 102 |
}; |
| 103 |
|
| 104 |
# Error defining two routes with the same name, regardless of method |
| 105 |
eval { |
| 106 |
get 'view_entry_splat' => '/' => sub {1}; |
| 107 |
1; |
| 108 |
} or do { |
| 109 |
::like( |
| 110 |
$@, |
| 111 |
qr/\QRoute with this name (view_entry_splat) already exists\E/xms, |
| 112 |
'Cannot register two routes with same name', |
| 113 |
); |
| 114 |
|
| 115 |
$tested = 1; |
| 116 |
}; |
| 117 |
} |
| 118 |
|
| 119 |
sub test_app { |
| 120 |
my ( $app, $mount_path ) = @_; |
| 121 |
|
| 122 |
my $prefix = 'http://localhost'; |
| 123 |
$mount_path |
| 124 |
and $prefix .= $mount_path; |
| 125 |
|
| 126 |
my ( $path, $res ); |
| 127 |
|
| 128 |
# Test static paths |
| 129 |
foreach my $idx ( 1 .. 2 ) { |
| 130 |
$res = $app->request( |
| 131 |
POST( |
| 132 |
"$prefix/uri_for_route", |
| 133 |
'Content' => JSON::MaybeXS::encode_json({ |
| 134 |
'route_name' => "view_entry_static$idx", |
| 135 |
'route_params' => { 'id' => $idx }, |
| 136 |
'query_params' => { 'foo' => $idx }, |
| 137 |
}), |
| 138 |
) |
| 139 |
); |
| 140 |
|
| 141 |
$path = "$prefix/view$idx/$idx?foo=$idx"; |
| 142 |
ok( $res->is_success, 'Successful request' ); |
| 143 |
is( $res->content, $path, "Correct path: $path" ); |
| 144 |
} |
| 145 |
|
| 146 |
# Test splat + megasplat |
| 147 |
$res = $app->request( |
| 148 |
POST( |
| 149 |
"$prefix/uri_for_route", |
| 150 |
'Content' => JSON::MaybeXS::encode_json({ |
| 151 |
'route_name' => 'view_entry_splat', |
| 152 |
'route_params' => [ 'foo', 'bar', [ 'baz', 'quux' ] ], |
| 153 |
'query_params' => { 'id' => 'di' }, |
| 154 |
}), |
| 155 |
) |
| 156 |
); |
| 157 |
|
| 158 |
$path = "$prefix/viewsplat/foo/bar/baz/quux?id=di"; |
| 159 |
ok( $res->is_success, 'Successful request' ); |
| 160 |
is( $res->content, $path, "Correct path: $path" ); |
| 161 |
|
| 162 |
# Test mixed |
| 163 |
$res = $app->request( |
| 164 |
POST( |
| 165 |
"$prefix/uri_for_route", |
| 166 |
'Content' => JSON::MaybeXS::encode_json( |
| 167 |
{ 'route_name' => 'view_entry_mixed', |
| 168 |
'route_params' => { |
| 169 |
'id' => 'di', |
| 170 |
'splat' => ['foo', ['bar', 'baz']] |
| 171 |
}, |
| 172 |
'query_params' => {'foo' => 'bar'}, |
| 173 |
} |
| 174 |
), |
| 175 |
) |
| 176 |
); |
| 177 |
|
| 178 |
$path = "$prefix/view_mixed/foo/bar/baz/di?foo=bar"; |
| 179 |
ok( $res->is_success, 'Successful request' ); |
| 180 |
is( $res->content, $path, "Correct path: $path" ); |
| 181 |
|
| 182 |
# Test escaping |
| 183 |
$res = $app->request( |
| 184 |
POST( |
| 185 |
"$prefix/uri_for_route", |
| 186 |
'Content' => JSON::MaybeXS::encode_json({ |
| 187 |
'route_name' => 'view_entry_static1', |
| 188 |
'route_params' => { 'id' => '!@£$%' }, |
| 189 |
}), |
| 190 |
) |
| 191 |
); |
| 192 |
|
| 193 |
$path = "$prefix/view1/!@%C3%82%C2%A3\$%"; |
| 194 |
ok( $res->is_success, 'Successful request' ); |
| 195 |
is( $res->content, $path, "Correct path: $path" ); |
| 196 |
|
| 197 |
# Test nonexistent route name |
| 198 |
$res = $app->request( GET "$prefix/fail_uri_for_route" ); |
| 199 |
ok( $res->is_success, 'Successful request' ); |
| 200 |
is( $res->content, '1', 'Successfully tested nonexistent failure mode' ); |
| 201 |
|
| 202 |
# Test splat + megasplat (incorrect amount) |
| 203 |
$res = $app->request( GET "$prefix/fail_uri_for_route_splat_args" ); |
| 204 |
ok( $res->is_success, 'Successful request' ); |
| 205 |
is( $res->content, '1', 'Successfully tested mismatch splat args/elements failure mode' ); |
| 206 |
|
| 207 |
# Test mixed with not all filled (named args left) |
| 208 |
$res = $app->request( GET "$prefix/fail_uri_for_route_leftovers" ); |
| 209 |
ok( $res->is_success, 'Successful request' ); |
| 210 |
is( $res->content, '1', 'Successfully tested leftover args failure mode' ); |
| 211 |
|
| 212 |
# Static with typed route parameters |
| 213 |
$res = $app->request( |
| 214 |
POST( |
| 215 |
"$prefix/uri_for_route", |
| 216 |
'Content' => JSON::MaybeXS::encode_json({ |
| 217 |
'route_name' => 'view_user', |
| 218 |
'route_params' => { 'prefix' => 'foo', 'username' => 'sawyer' }, |
| 219 |
'query_params' => { 'foo' => 1 }, |
| 220 |
}), |
| 221 |
) |
| 222 |
); |
| 223 |
|
| 224 |
$path = "$prefix/foo/user/sawyer?foo=1"; |
| 225 |
ok( $res->is_success, 'Successful request' ); |
| 226 |
is( $res->content, $path, "Correct path for typed route param: $path" ); |
| 227 |
} |
| 228 |
|
| 229 |
subtest 'Non-mounted app' => sub { |
| 230 |
my $app = Plack::Test->create( App->to_app ); |
| 231 |
test_app($app); |
| 232 |
ok( $App::tested, 'Check for duplicate route names done successfully' ); |
| 233 |
}; |
| 234 |
|
| 235 |
subtest 'Mounted app' => sub { |
| 236 |
my $app = Plack::Test->create( |
| 237 |
builder { |
| 238 |
mount '/mount' => App->to_app; |
| 239 |
mount '/' => sub { |
| 240 |
return { Plack::Response->new(200, [], ['OK'] ) } |
| 241 |
}, |
| 242 |
} |
| 243 |
); |
| 244 |
|
| 245 |
test_app( $app, '/mount' ); |
| 246 |
}; |