| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use Test::Fatal; |
| 5 |
use Dancer2::Core::Request; |
| 6 |
use Dancer2::Core::Route; |
| 7 |
use Capture::Tiny 0.12 'capture_stderr'; |
| 8 |
use Ref::Util qw<is_regexpref>; |
| 9 |
use lib 't/lib'; |
| 10 |
|
| 11 |
my @tests = ( |
| 12 |
[ [ 'get', '/', sub {11} ], '/', [ {}, 11 ] ], |
| 13 |
[ [ 'get', '/', sub {11} ], |
| 14 |
'/failure', |
| 15 |
[ undef, 11 ] |
| 16 |
], |
| 17 |
|
| 18 |
# token tests |
| 19 |
[ [ 'get', '/hello/:name', sub {22} ], |
| 20 |
'/hello/sukria', |
| 21 |
[ { name => 'sukria' }, 22 ] |
| 22 |
], |
| 23 |
[ [ 'get', '/hello/:name?', sub {22} ], |
| 24 |
'/hello/', |
| 25 |
[ { name => undef }, 22 ] |
| 26 |
], |
| 27 |
|
| 28 |
# prefix tests |
| 29 |
[ [ 'get', '/', sub {33}, '/forum' ], |
| 30 |
'/forum/', |
| 31 |
[ {}, 33 ] |
| 32 |
], |
| 33 |
[ [ 'get', '/', sub {33}, '/forum' ], |
| 34 |
'/forum/', |
| 35 |
[ {}, 33 ] |
| 36 |
], |
| 37 |
[ [ 'get', '/mywebsite', sub {33}, '/forum' ], |
| 38 |
'/forum/mywebsite', |
| 39 |
[ {}, 33 ] |
| 40 |
], |
| 41 |
[ [ 'get', '', sub {'concat'}, '/' ], |
| 42 |
'/', |
| 43 |
[ {}, 'concat' ] |
| 44 |
], |
| 45 |
|
| 46 |
# token in prefix tests |
| 47 |
[ [ 'get', 'name', sub {35}, '/hello/:' ], |
| 48 |
'/hello/sukria', |
| 49 |
[ { name => 'sukria' }, 35 ], |
| 50 |
], |
| 51 |
|
| 52 |
[ [ 'get', '/', sub {36}, '/hello/:name' ], |
| 53 |
'/hello/sukria/', |
| 54 |
[ { name => 'sukria' }, 36 ], |
| 55 |
], |
| 56 |
|
| 57 |
# splat test |
| 58 |
[ [ 'get', '/file/*.*', sub {44} ], |
| 59 |
'/file/dist.ini', |
| 60 |
[ { splat => [ 'dist', 'ini' ] }, 44 ] |
| 61 |
], |
| 62 |
|
| 63 |
# splat in prefix |
| 64 |
[ [ 'get', '', sub {42}, '/forum/*'], |
| 65 |
'/forum/dancer', |
| 66 |
[ { splat => [ 'dancer' ] }, 42 ] |
| 67 |
], |
| 68 |
|
| 69 |
# megasplat test |
| 70 |
[ [ 'get', '/file/**/*', sub {44} ], |
| 71 |
'/file/some/where/42', |
| 72 |
[ { splat => [ [ 'some', 'where' ], '42' ] }, 44 ] |
| 73 |
], |
| 74 |
|
| 75 |
# megasplat consistently handles multiple slashes |
| 76 |
[ [ 'get', '/foo/**', sub {'45a'} ], |
| 77 |
'/foo/bar///baz', |
| 78 |
[ { splat => [ [ 'bar', '', '', 'baz' ] ] }, '45a' ] |
| 79 |
], |
| 80 |
[ [ 'get', '/foo/**', sub {'45b'} ], |
| 81 |
'/foo/bar///', # empty trailing path segment |
| 82 |
[ { splat => [ [ 'bar', '', '', '' ] ] }, '45b' ] |
| 83 |
], |
| 84 |
|
| 85 |
# Optional megasplat test - with a value... |
| 86 |
[ [ 'get', '/foo/?**?', sub {46} ], |
| 87 |
'/foo/bar/baz', |
| 88 |
[ { splat => [ [ 'bar', 'baz' ] ] }, 46 ], |
| 89 |
], |
| 90 |
# ... and without |
| 91 |
[ [ 'get', '/foo/?**?', sub {47} ], |
| 92 |
'/foo', |
| 93 |
[ { splat => [ [ ] ] }, 47 ], |
| 94 |
], |
| 95 |
|
| 96 |
# mixed (mega)splat and tokens |
| 97 |
[ [ 'get', '/some/:id/**/*', sub {55} ], |
| 98 |
'/some/where/to/run/and/hide', |
| 99 |
[ { id => 'where', splat => [ [ 'to', 'run', 'and' ], 'hide' ] }, 55 ] |
| 100 |
], |
| 101 |
[ [ 'get', '/some/*/**/:id?', sub {55} ], |
| 102 |
'/some/one/to/say/boo/', |
| 103 |
[ { id => undef, splat => [ 'one', [ 'to', 'say', 'boo' ] ] }, 55 ] |
| 104 |
], |
| 105 |
|
| 106 |
# supplied regex |
| 107 |
[ [ 'get', qr{stuff(\d+)}, sub {44} ], '/stuff48', |
| 108 |
[ { splat => [48] }, 44 ] |
| 109 |
], |
| 110 |
[ [ 'get', qr{/stuff(\d+)}, sub {44}, '/foo' ], |
| 111 |
'/foo/stuff48', |
| 112 |
[ { splat => [48] }, 44 ], |
| 113 |
], |
| 114 |
|
| 115 |
# regex and tokens/splat |
| 116 |
[ |
| 117 |
[ 'get', '/(any|some)/thing/*', sub {60} ], |
| 118 |
# was causing 'Use of uninitialized value within @token_or_splat' warnings |
| 119 |
'/any/thing/else', |
| 120 |
[ { splat => ['any', 'else'] }, 60 ] |
| 121 |
], |
| 122 |
); |
| 123 |
|
| 124 |
|
| 125 |
plan tests => 116; |
| 126 |
|
| 127 |
for my $t (@tests) { |
| 128 |
my ( $route, $path, $expected ) = @$t; |
| 129 |
|
| 130 |
if ( is_regexpref($expected) ) { |
| 131 |
like( |
| 132 |
exception { |
| 133 |
my $r = Dancer2::Core::Route->new( |
| 134 |
method => $route->[0], |
| 135 |
regexp => $route->[1], |
| 136 |
code => $route->[2], |
| 137 |
prefix => $route->[3], |
| 138 |
); |
| 139 |
}, |
| 140 |
$expected, |
| 141 |
"got expected exception for $path", |
| 142 |
); |
| 143 |
} |
| 144 |
else { |
| 145 |
my $r = Dancer2::Core::Route->new( |
| 146 |
method => $route->[0], |
| 147 |
regexp => $route->[1], |
| 148 |
code => $route->[2], |
| 149 |
prefix => $route->[3], |
| 150 |
); |
| 151 |
isa_ok $r, 'Dancer2::Core::Route'; |
| 152 |
|
| 153 |
my $request = Dancer2::Core::Request->new( |
| 154 |
env => { |
| 155 |
PATH_INFO => $path, |
| 156 |
REQUEST_METHOD => $route->[0], |
| 157 |
} |
| 158 |
); |
| 159 |
my $m; |
| 160 |
is( capture_stderr { $m = $r->match($request) }, '', |
| 161 |
"no warnings generated for $path" ); |
| 162 |
is_deeply $m, $expected->[0], "got expected data for '$path'"; |
| 163 |
|
| 164 |
{ |
| 165 |
package App; use Dancer2; ## no critic |
| 166 |
} |
| 167 |
|
| 168 |
use Dancer2::Core::App; |
| 169 |
use Dancer2::Core::Response; |
| 170 |
my $app = Dancer2::Core::App->new( |
| 171 |
request => $request, |
| 172 |
response => Dancer2::Core::Response->new, |
| 173 |
); |
| 174 |
|
| 175 |
is $r->execute($app)->content, $expected->[1], "got expected result for '$path'"; |
| 176 |
|
| 177 |
# failing request |
| 178 |
my $failing_request = Dancer2::Core::Request->new( |
| 179 |
env => { |
| 180 |
PATH_INFO => '/something_that_doesnt_exist', |
| 181 |
REQUEST_METHOD => 'GET', |
| 182 |
}, |
| 183 |
); |
| 184 |
|
| 185 |
$m = $r->match($failing_request); |
| 186 |
is $m, undef, "don't match failing request"; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
subtest "named captures" => sub { |
| 191 |
|
| 192 |
## Regexp is parsed in compile time. So, eval with QUOTES to force to parse later. |
| 193 |
my $route_regex; |
| 194 |
|
| 195 |
## no critic |
| 196 |
|
| 197 |
eval q{ |
| 198 |
$route_regex = qr{/(?<class> user | content | post )/(?<action> delete | find )/(?<id> \d+ )}x; |
| 199 |
}; |
| 200 |
|
| 201 |
## use critic |
| 202 |
|
| 203 |
my $r = Dancer2::Core::Route->new( |
| 204 |
regexp => $route_regex, |
| 205 |
code => sub { |
| 206 |
'ok'; |
| 207 |
}, |
| 208 |
method => 'get', |
| 209 |
); |
| 210 |
|
| 211 |
my $request = Dancer2::Core::Request->new( |
| 212 |
env => { |
| 213 |
PATH_INFO => '/user/delete/234', |
| 214 |
REQUEST_METHOD => 'GET', |
| 215 |
}, |
| 216 |
); |
| 217 |
|
| 218 |
my $m = $r->match($request); |
| 219 |
|
| 220 |
is_deeply $m, |
| 221 |
{ captures => { |
| 222 |
class => 'user', |
| 223 |
action => 'delete', |
| 224 |
id => 234 |
| 225 |
} |
| 226 |
}, |
| 227 |
"named captures work"; |
| 228 |
}; |
| 229 |
|
| 230 |
note "routes with options"; { |
| 231 |
my $route_w_options = Dancer2::Core::Route->new( |
| 232 |
method => 'get', |
| 233 |
regexp => '/', |
| 234 |
code => sub {'options'}, |
| 235 |
options => { 'agent' => 'cURL' }, |
| 236 |
); |
| 237 |
|
| 238 |
my $req = Dancer2::Core::Request->new( |
| 239 |
path => '/', |
| 240 |
method => 'get', |
| 241 |
env => { 'HTTP_USER_AGENT' => 'mozilla' }, |
| 242 |
); |
| 243 |
|
| 244 |
my $m = $route_w_options->match($req); |
| 245 |
ok !defined $m, 'Route did not match'; |
| 246 |
|
| 247 |
$req = Dancer2::Core::Request->new( |
| 248 |
path => '/', |
| 249 |
method => 'get', |
| 250 |
env => { 'HTTP_USER_AGENT' => 'cURL' }, |
| 251 |
); |
| 252 |
|
| 253 |
$m = $route_w_options->match($req); |
| 254 |
ok defined $m, 'Route matched'; |
| 255 |
|
| 256 |
$route_w_options = Dancer2::Core::Route->new( |
| 257 |
method => 'get', |
| 258 |
regexp => '/', |
| 259 |
code => sub {'options'}, |
| 260 |
options => { |
| 261 |
'agent' => 'cURL', |
| 262 |
'content_type' => 'foo', |
| 263 |
}, |
| 264 |
); |
| 265 |
|
| 266 |
$req = Dancer2::Core::Request->new( |
| 267 |
path => '/', |
| 268 |
method => 'get', |
| 269 |
env => { 'HTTP_USER_AGENT' => 'cURL' }, |
| 270 |
); |
| 271 |
|
| 272 |
# Check match more than once (each iterator wasn't reset, for loop is ok ) |
| 273 |
$m = $route_w_options->match($req); |
| 274 |
ok !defined $m, 'More options - Route did not match - test 1'; |
| 275 |
$m = $route_w_options->match($req); |
| 276 |
ok !defined $m, 'More options - Route did not match - test 2'; |
| 277 |
} |
| 278 |
|
| 279 |
subtest "typed route params" => sub { |
| 280 |
my @tests = ( |
| 281 |
{ |
| 282 |
name => "good type check", |
| 283 |
route => { |
| 284 |
regexp => '/some/:id[Int]', |
| 285 |
}, |
| 286 |
request => '/some/34', |
| 287 |
match => { id => 34 }, |
| 288 |
}, |
| 289 |
{ |
| 290 |
name => "bad required type check", |
| 291 |
route => { |
| 292 |
regexp => '/some/:id[Int]', |
| 293 |
}, |
| 294 |
request => '/some/bad', |
| 295 |
}, |
| 296 |
{ |
| 297 |
name => "missing required type check", |
| 298 |
route => { |
| 299 |
regexp => '/some/:id[Int]', |
| 300 |
}, |
| 301 |
request => '/some/', |
| 302 |
}, |
| 303 |
{ |
| 304 |
name => "optional type check exists", |
| 305 |
route => { |
| 306 |
regexp => '/some/:id[Int]?', |
| 307 |
}, |
| 308 |
request => '/some/34', |
| 309 |
match => { id => 34 }, |
| 310 |
}, |
| 311 |
{ |
| 312 |
name => "optional type check with bad token", |
| 313 |
route => { |
| 314 |
regexp => '/some/:id[Int]?', |
| 315 |
}, |
| 316 |
request => '/some/bad', |
| 317 |
}, |
| 318 |
{ |
| 319 |
name => "optional type check with empty token", |
| 320 |
route => { |
| 321 |
regexp => '/some/:id[Int]?', |
| 322 |
}, |
| 323 |
request => '/some/', |
| 324 |
match => { id => undef }, |
| 325 |
}, |
| 326 |
{ |
| 327 |
name => "optional type check with empty token and optional missing trailing slash", |
| 328 |
route => { |
| 329 |
regexp => '/some/?:id[Int]?', |
| 330 |
}, |
| 331 |
request => '/some', |
| 332 |
match => { id => undef }, |
| 333 |
}, |
| 334 |
{ |
| 335 |
name => "bad type", |
| 336 |
route => { |
| 337 |
regexp => '/some/:id[MyDate]?', |
| 338 |
exception => qr/MyDate is not a known type constraint/, |
| 339 |
}, |
| 340 |
request => '/some/foo', |
| 341 |
match => { id => undef }, |
| 342 |
}, |
| 343 |
{ |
| 344 |
name => "custom type with good match", |
| 345 |
route => { |
| 346 |
regexp => '/date/:date[MyDate]', |
| 347 |
args => { type_library => 'TestTypeLibrary' }, |
| 348 |
}, |
| 349 |
request => '/date/2014-01-01', |
| 350 |
match => { date => '2014-01-01' }, |
| 351 |
}, |
| 352 |
{ |
| 353 |
name => "custom type with bad match", |
| 354 |
route => { |
| 355 |
regexp => '/date/:date[MyDate]', |
| 356 |
args => { type_library => 'TestTypeLibrary' }, |
| 357 |
}, |
| 358 |
request => '/date/X014-01-01', |
| 359 |
}, |
| 360 |
{ |
| 361 |
name => "type including type library but no type_library config setting", |
| 362 |
route => { |
| 363 |
regexp => '/date/:date[TestTypeLibrary::MyDate]', |
| 364 |
}, |
| 365 |
request => '/date/2014-01-01', |
| 366 |
match => { date => '2014-01-01' }, |
| 367 |
}, |
| 368 |
{ |
| 369 |
name => "union of types", |
| 370 |
route => { |
| 371 |
regexp => '/date/:date[Int|TestTypeLibrary::MyDate]', |
| 372 |
}, |
| 373 |
request => '/date/2014-01-01', |
| 374 |
match => { date => '2014-01-01' }, |
| 375 |
}, |
| 376 |
{ |
| 377 |
name => "union of types checking other type", |
| 378 |
route => { |
| 379 |
regexp => '/date/:date[Int|TestTypeLibrary::MyDate]', |
| 380 |
}, |
| 381 |
request => '/date/2014', |
| 382 |
match => { date => '2014' }, |
| 383 |
}, |
| 384 |
{ |
| 385 |
name => "multiple typed tokens plus other tokens and splats", |
| 386 |
route => { |
| 387 |
regexp => '/:id[Int]/:date[MyDate]/:foo/*/**', |
| 388 |
args => { type_library => 'TestTypeLibrary' }, |
| 389 |
}, |
| 390 |
request => '/42/2018-11-23/bar/dave/was/here', |
| 391 |
match => { |
| 392 |
id => 42, |
| 393 |
date => '2018-11-23', |
| 394 |
foo => 'bar', |
| 395 |
splat => [ 'dave', [ 'was', 'here' ] ], |
| 396 |
}, |
| 397 |
}, |
| 398 |
); |
| 399 |
|
| 400 |
for my $test (@tests) { |
| 401 |
my $method = $test->{route}{method} || 'get'; |
| 402 |
|
| 403 |
my %route_args = ( |
| 404 |
method => $method, |
| 405 |
regexp => $test->{route}{regexp}, |
| 406 |
code => $test->{route}{code} || sub { 'OK' }, |
| 407 |
$test->{route}{prefix} ? ( prefix => $test->{route}{prefix} ) : (), |
| 408 |
$test->{route}{args} ? %{ $test->{route}{args} } : (), |
| 409 |
); |
| 410 |
|
| 411 |
if ( my $exception = $test->{route}{exception} ) { |
| 412 |
like exception { Dancer2::Core::Route->new(%route_args) }, |
| 413 |
$exception, |
| 414 |
"'$test->{name}' throws expected exception in route constructor"; |
| 415 |
next; |
| 416 |
} |
| 417 |
|
| 418 |
my $route = Dancer2::Core::Route->new(%route_args); |
| 419 |
my $request = Dancer2::Core::Request->new( |
| 420 |
env => { |
| 421 |
PATH_INFO => $test->{request}, |
| 422 |
REQUEST_METHOD => $method, |
| 423 |
} |
| 424 |
); |
| 425 |
|
| 426 |
my $match; |
| 427 |
is exception { |
| 428 |
$match = $route->match($request) |
| 429 |
}, undef, "'$test->{name}' does not throw an exception"; |
| 430 |
|
| 431 |
my $expected = $test->{match}; |
| 432 |
if ( defined $expected ) { |
| 433 |
is_deeply $match, $expected, |
| 434 |
"... and route matched with expected captures" |
| 435 |
or diag explain $match; |
| 436 |
} |
| 437 |
else { |
| 438 |
ok !defined $match, "... and route did not match" |
| 439 |
or diag explain $match; |
| 440 |
} |
| 441 |
} |
| 442 |
}; |