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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
use strict;
use warnings;
use Test::More;
use Test::Fatal;
use Dancer2::Core::Request;
use Dancer2::Core::Route;
use Capture::Tiny 0.12 'capture_stderr';
use Ref::Util qw<is_regexpref>;
use lib 't/lib';
my @tests = (
[ [ 'get', '/', sub {11} ], '/', [ {}, 11 ] ],
[ [ 'get', '/', sub {11} ],
'/failure',
[ undef, 11 ]
],
# token tests
[ [ 'get', '/hello/:name', sub {22} ],
'/hello/sukria',
[ { name => 'sukria' }, 22 ]
],
[ [ 'get', '/hello/:name?', sub {22} ],
'/hello/',
[ { name => undef }, 22 ]
],
# prefix tests
[ [ 'get', '/', sub {33}, '/forum' ],
'/forum/',
[ {}, 33 ]
],
[ [ 'get', '/', sub {33}, '/forum' ],
'/forum/',
[ {}, 33 ]
],
[ [ 'get', '/mywebsite', sub {33}, '/forum' ],
'/forum/mywebsite',
[ {}, 33 ]
],
[ [ 'get', '', sub {'concat'}, '/' ],
'/',
[ {}, 'concat' ]
],
# token in prefix tests
[ [ 'get', 'name', sub {35}, '/hello/:' ],
'/hello/sukria',
[ { name => 'sukria' }, 35 ],
],
[ [ 'get', '/', sub {36}, '/hello/:name' ],
'/hello/sukria/',
[ { name => 'sukria' }, 36 ],
],
# splat test
[ [ 'get', '/file/*.*', sub {44} ],
'/file/dist.ini',
[ { splat => [ 'dist', 'ini' ] }, 44 ]
],
# splat in prefix
[ [ 'get', '', sub {42}, '/forum/*'],
'/forum/dancer',
[ { splat => [ 'dancer' ] }, 42 ]
],
# megasplat test
[ [ 'get', '/file/**/*', sub {44} ],
'/file/some/where/42',
[ { splat => [ [ 'some', 'where' ], '42' ] }, 44 ]
],
# megasplat consistently handles multiple slashes
[ [ 'get', '/foo/**', sub {'45a'} ],
'/foo/bar///baz',
[ { splat => [ [ 'bar', '', '', 'baz' ] ] }, '45a' ]
],
[ [ 'get', '/foo/**', sub {'45b'} ],
'/foo/bar///', # empty trailing path segment
[ { splat => [ [ 'bar', '', '', '' ] ] }, '45b' ]
],
# Optional megasplat test - with a value...
[ [ 'get', '/foo/?**?', sub {46} ],
'/foo/bar/baz',
[ { splat => [ [ 'bar', 'baz' ] ] }, 46 ],
],
# ... and without
[ [ 'get', '/foo/?**?', sub {47} ],
'/foo',
[ { splat => [ [ ] ] }, 47 ],
],
# mixed (mega)splat and tokens
[ [ 'get', '/some/:id/**/*', sub {55} ],
'/some/where/to/run/and/hide',
[ { id => 'where', splat => [ [ 'to', 'run', 'and' ], 'hide' ] }, 55 ]
],
[ [ 'get', '/some/*/**/:id?', sub {55} ],
'/some/one/to/say/boo/',
[ { id => undef, splat => [ 'one', [ 'to', 'say', 'boo' ] ] }, 55 ]
],
# supplied regex
[ [ 'get', qr{stuff(\d+)}, sub {44} ], '/stuff48',
[ { splat => [48] }, 44 ]
],
[ [ 'get', qr{/stuff(\d+)}, sub {44}, '/foo' ],
'/foo/stuff48',
[ { splat => [48] }, 44 ],
],
# regex and tokens/splat
[
[ 'get', '/(any|some)/thing/*', sub {60} ],
# was causing 'Use of uninitialized value within @token_or_splat' warnings
'/any/thing/else',
[ { splat => ['any', 'else'] }, 60 ]
],
);
plan tests => 116;
for my $t (@tests) {
my ( $route, $path, $expected ) = @$t;
if ( is_regexpref($expected) ) {
like(
exception {
my $r = Dancer2::Core::Route->new(
method => $route->[0],
regexp => $route->[1],
code => $route->[2],
prefix => $route->[3],
);
},
$expected,
"got expected exception for $path",
);
}
else {
my $r = Dancer2::Core::Route->new(
method => $route->[0],
regexp => $route->[1],
code => $route->[2],
prefix => $route->[3],
);
isa_ok $r, 'Dancer2::Core::Route';
my $request = Dancer2::Core::Request->new(
env => {
PATH_INFO => $path,
REQUEST_METHOD => $route->[0],
}
);
my $m;
is( capture_stderr { $m = $r->match($request) }, '',
"no warnings generated for $path" );
is_deeply $m, $expected->[0], "got expected data for '$path'";
{
package App; use Dancer2; ## no critic
}
use Dancer2::Core::App;
use Dancer2::Core::Response;
my $app = Dancer2::Core::App->new(
request => $request,
response => Dancer2::Core::Response->new,
);
is $r->execute($app)->content, $expected->[1], "got expected result for '$path'";
# failing request
my $failing_request = Dancer2::Core::Request->new(
env => {
PATH_INFO => '/something_that_doesnt_exist',
REQUEST_METHOD => 'GET',
},
);
$m = $r->match($failing_request);
is $m, undef, "don't match failing request";
}
}
subtest "named captures" => sub {
## Regexp is parsed in compile time. So, eval with QUOTES to force to parse later.
my $route_regex;
## no critic
eval q{
$route_regex = qr{/(?<class> user | content | post )/(?<action> delete | find )/(?<id> \d+ )}x;
};
## use critic
my $r = Dancer2::Core::Route->new(
regexp => $route_regex,
code => sub {
'ok';
},
method => 'get',
);
my $request = Dancer2::Core::Request->new(
env => {
PATH_INFO => '/user/delete/234',
REQUEST_METHOD => 'GET',
},
);
my $m = $r->match($request);
is_deeply $m,
{ captures => {
class => 'user',
action => 'delete',
id => 234
}
},
"named captures work";
};
note "routes with options"; {
my $route_w_options = Dancer2::Core::Route->new(
method => 'get',
regexp => '/',
code => sub {'options'},
options => { 'agent' => 'cURL' },
);
my $req = Dancer2::Core::Request->new(
path => '/',
method => 'get',
env => { 'HTTP_USER_AGENT' => 'mozilla' },
);
my $m = $route_w_options->match($req);
ok !defined $m, 'Route did not match';
$req = Dancer2::Core::Request->new(
path => '/',
method => 'get',
env => { 'HTTP_USER_AGENT' => 'cURL' },
);
$m = $route_w_options->match($req);
ok defined $m, 'Route matched';
$route_w_options = Dancer2::Core::Route->new(
method => 'get',
regexp => '/',
code => sub {'options'},
options => {
'agent' => 'cURL',
'content_type' => 'foo',
},
);
$req = Dancer2::Core::Request->new(
path => '/',
method => 'get',
env => { 'HTTP_USER_AGENT' => 'cURL' },
);
# Check match more than once (each iterator wasn't reset, for loop is ok )
$m = $route_w_options->match($req);
ok !defined $m, 'More options - Route did not match - test 1';
$m = $route_w_options->match($req);
ok !defined $m, 'More options - Route did not match - test 2';
}
subtest "typed route params" => sub {
my @tests = (
{
name => "good type check",
route => {
regexp => '/some/:id[Int]',
},
request => '/some/34',
match => { id => 34 },
},
{
name => "bad required type check",
route => {
regexp => '/some/:id[Int]',
},
request => '/some/bad',
},
{
name => "missing required type check",
route => {
regexp => '/some/:id[Int]',
},
request => '/some/',
},
{
name => "optional type check exists",
route => {
regexp => '/some/:id[Int]?',
},
request => '/some/34',
match => { id => 34 },
},
{
name => "optional type check with bad token",
route => {
regexp => '/some/:id[Int]?',
},
request => '/some/bad',
},
{
name => "optional type check with empty token",
route => {
regexp => '/some/:id[Int]?',
},
request => '/some/',
match => { id => undef },
},
{
name => "optional type check with empty token and optional missing trailing slash",
route => {
regexp => '/some/?:id[Int]?',
},
request => '/some',
match => { id => undef },
},
{
name => "bad type",
route => {
regexp => '/some/:id[MyDate]?',
exception => qr/MyDate is not a known type constraint/,
},
request => '/some/foo',
match => { id => undef },
},
{
name => "custom type with good match",
route => {
regexp => '/date/:date[MyDate]',
args => { type_library => 'TestTypeLibrary' },
},
request => '/date/2014-01-01',
match => { date => '2014-01-01' },
},
{
name => "custom type with bad match",
route => {
regexp => '/date/:date[MyDate]',
args => { type_library => 'TestTypeLibrary' },
},
request => '/date/X014-01-01',
},
{
name => "type including type library but no type_library config setting",
route => {
regexp => '/date/:date[TestTypeLibrary::MyDate]',
},
request => '/date/2014-01-01',
match => { date => '2014-01-01' },
},
{
name => "union of types",
route => {
regexp => '/date/:date[Int|TestTypeLibrary::MyDate]',
},
request => '/date/2014-01-01',
match => { date => '2014-01-01' },
},
{
name => "union of types checking other type",
route => {
regexp => '/date/:date[Int|TestTypeLibrary::MyDate]',
},
request => '/date/2014',
match => { date => '2014' },
},
{
name => "multiple typed tokens plus other tokens and splats",
route => {
regexp => '/:id[Int]/:date[MyDate]/:foo/*/**',
args => { type_library => 'TestTypeLibrary' },
},
request => '/42/2018-11-23/bar/dave/was/here',
match => {
id => 42,
date => '2018-11-23',
foo => 'bar',
splat => [ 'dave', [ 'was', 'here' ] ],
},
},
);
for my $test (@tests) {
my $method = $test->{route}{method} || 'get';
my %route_args = (
method => $method,
regexp => $test->{route}{regexp},
code => $test->{route}{code} || sub { 'OK' },
$test->{route}{prefix} ? ( prefix => $test->{route}{prefix} ) : (),
$test->{route}{args} ? %{ $test->{route}{args} } : (),
);
if ( my $exception = $test->{route}{exception} ) {
like exception { Dancer2::Core::Route->new(%route_args) },
$exception,
"'$test->{name}' throws expected exception in route constructor";
next;
}
my $route = Dancer2::Core::Route->new(%route_args);
my $request = Dancer2::Core::Request->new(
env => {
PATH_INFO => $test->{request},
REQUEST_METHOD => $method,
}
);
my $match;
is exception {
$match = $route->match($request)
}, undef, "'$test->{name}' does not throw an exception";
my $expected = $test->{match};
if ( defined $expected ) {
is_deeply $match, $expected,
"... and route matched with expected captures"
or diag explain $match;
}
else {
ok !defined $match, "... and route did not match"
or diag explain $match;
}
}
};
|