2 use warnings FATAL => 'all';
4 use Test::More qw(no_plan);
6 use Web::Dispatch::Parser;
8 my $dp = Web::Dispatch::Parser->new;
11 my $all = $dp->parse('');
14 [ $all->({ REQUEST_METHOD => 'GET' }) ],
20 [ $all->({ REQUEST_METHOD => 'POST' }) ],
27 my $get = $dp->parse('GET');
30 [ $get->({ REQUEST_METHOD => 'GET' }) ],
36 [ $get->({ REQUEST_METHOD => 'POST' }) ],
43 my $html = $dp->parse('.html');
46 [ $html->({ PATH_INFO => '/foo/bar.html' }) ],
52 [ $html->({ PATH_INFO => '/foo/bar.xml' }) ],
54 '.xml does not match .html'
59 my $any_ext = $dp->parse('.*');
62 [ $any_ext->({ PATH_INFO => '/foo/bar.html' }) ],
64 '.html matches .* and extension returned'
68 [ $any_ext->({ PATH_INFO => '/foo/bar' }) ],
70 'no extension does not match .*'
75 my $slash = $dp->parse('/');
78 [ $slash->({ PATH_INFO => '/' }) ],
84 [ $slash->({ PATH_INFO => '/foo' }) ],
86 '/foo does not match /'
91 my $post = $dp->parse('/post/*');
94 [ $post->({ PATH_INFO => '/post/one' }) ],
96 '/post/one parses out one'
100 [ $post->({ PATH_INFO => '/post/one/' }) ],
102 '/post/one/ does not match'
106 [ $post->({ PATH_INFO => '/post/one.html' }) ],
108 '/post/one.html still parses out one'
113 my $post = $dp->parse('/foo-bar/*');
116 [ $post->({ PATH_INFO => '/foo-bar/one' }) ],
118 '/foo-bar/one parses out one'
122 [ $post->({ PATH_INFO => '/foo-bar/one/' }) ],
124 '/foo-bar/one/ does not match'
129 my $combi = $dp->parse('GET+/post/*');
132 [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'GET' }) ],
134 '/post/one parses out one'
138 [ $combi->({ PATH_INFO => '/post/one/', REQUEST_METHOD => 'GET' }) ],
140 '/post/one/ does not match'
144 [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'POST' }) ],
146 'POST /post/one does not match'
151 my $or = $dp->parse('GET|POST');
153 foreach my $meth (qw(GET POST)) {
156 [ $or->({ REQUEST_METHOD => $meth }) ],
158 'GET|POST matches method '.$meth
163 [ $or->({ REQUEST_METHOD => 'PUT' }) ],
165 'GET|POST does not match PUT'
170 my $or = $dp->parse('GET|POST|DELETE');
172 foreach my $meth (qw(GET POST DELETE)) {
175 [ $or->({ REQUEST_METHOD => $meth }) ],
177 'GET|POST|DELETE matches method '.$meth
182 [ $or->({ REQUEST_METHOD => 'PUT' }) ],
184 'GET|POST|DELETE does not match PUT'
189 my $nest = $dp->parse('(GET+/foo)|POST');
192 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'GET' }) ],
194 '(GET+/foo)|POST matches GET /foo'
198 [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'GET' }) ],
200 '(GET+/foo)|POST does not match GET /bar'
204 [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'POST' }) ],
206 '(GET+/foo)|POST matches POST /bar'
210 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'PUT' }) ],
212 '(GET+/foo)|POST does not match PUT /foo'
219 !eval { $dp->parse('/foo+(GET'); 1 },
220 'Death with missing closing )'
226 (s/^\n//s,s/\n $//s,s/^ //mg) for $err;
230 "Error $@ matches\n${err}\n"
235 my $not = $dp->parse('!.html+.*');
238 [ $not->({ PATH_INFO => '/foo.xml' }) ],
240 '!.html+.* matches /foo.xml'
244 [ $not->({ PATH_INFO => '/foo.html' }) ],
246 '!.html+.* does not match /foo.html'
250 [ $not->({ PATH_INFO => '/foo' }) ],
252 '!.html+.* does not match /foo'
257 my $ext = $dp->parse('/foo.bar');
260 [ $ext->({ PATH_INFO => '/foo.bar' }) ],
262 '/foo.bar matches /foo.bar'
266 [ $ext->({ PATH_INFO => '/foo.bar.ext' }) ],
268 '/foo.bar matches /foo.bar.ext'
272 [ $ext->({ PATH_INFO => '/foo.notbar' }) ],
274 '/foo.bar does not match /foo.notbar'
279 my $sub = $dp->parse('/foo/*/...');
282 [ $sub->({ PATH_INFO => '/foo/1/bar' }) ],
283 [ { PATH_INFO => '/bar', SCRIPT_NAME => '/foo/1' }, 1 ],
284 '/foo/*/... matches /foo/1/bar and strips to /bar'
288 [ $sub->({ PATH_INFO => '/foo/1/' }) ],
289 [ { PATH_INFO => '/', SCRIPT_NAME => '/foo/1' }, 1 ],
290 '/foo/*/... matches /foo/1/bar and strips to /'
294 [ $sub->({ PATH_INFO => '/foo/1' }) ],
296 '/foo/*/... does not match /foo/1 (no trailing /)'
301 my $sub = $dp->parse('/foo/**/belief');
302 my $match = 'barred/beyond';
304 [ $sub->({ PATH_INFO => "/foo/${match}/belief" }) ],
306 "/foo/**/belief matches /foo/${match}/belief"
312 my $sub = $dp->parse($match);
315 [ $sub->({ PATH_INFO => '/foo' }) ],
317 "$match does not match /foo"
321 [ $sub->({ PATH_INFO => '' }) ],
323 "$match matches empty path with empty env"
328 my $match = '/foo...';
329 my $sub = $dp->parse($match);
332 [ $sub->({ PATH_INFO => '/foobar' }) ],
334 "$match does not match /foobar"
338 [ $sub->({ PATH_INFO => '/foo/bar' }) ],
339 [ { PATH_INFO => '/bar', SCRIPT_NAME => '/foo' } ],
340 "$match matches /foo/bar and strips to /bar"
344 [ $sub->({ PATH_INFO => '/foo/' }) ],
345 [ { PATH_INFO => '/', SCRIPT_NAME => '/foo' } ],
346 "$match matches /foo/ and strips to /"
350 [ $sub->({ PATH_INFO => '/foo' }) ],
351 [ { PATH_INFO => '', SCRIPT_NAME => '/foo' } ],
352 "$match matches /foo and strips to empty path"
358 [ '/one/*' => 'two' ],
359 [ '/one/*.*' => 'two.three' ],
360 [ '/**' => 'one/two' ],
361 [ '/**.*' => 'one/two.three' ],
364 foreach my $p (@dot_pairs) {
366 [ $dp->parse($p->[0])->({ PATH_INFO => '/one/two.three' }) ],
368 "${\$p->[0]} matches /one/two.three and returns ${\$p->[1]}"
375 [ '/foo/*:foo_id' => '/foo/1' => { foo_id => 1 } ],
376 [ '/foo/:foo_id' => '/foo/1' => { foo_id => 1 } ],
377 [ '/foo/:id/**:rest' => '/foo/id/rest/of/the/path.ext'
378 => { id => 'id', rest => 'rest/of/the/path' } ],
379 [ '/foo/:id/**.*:rest' => '/foo/id/rest/of/the/path.ext'
380 => { id => 'id', rest => 'rest/of/the/path.ext' } ],
382 foreach my $n (@named) {
384 [ $dp->parse($n->[0])->({ PATH_INFO => $n->[1] }) ],
386 "${\$n->[0]} matches ${\$n->[1]} with correct captures"
395 my $q = 'foo=FOO&bar=BAR1&baz=one+two&quux=QUUX1&quux=QUUX2'
396 .'&bar=BAR2&quux=QUUX3&evil=%2F';
408 bar => [ qw(BAR1 BAR2) ],
409 baz => [ 'one two' ],
410 quux => [ qw(QUUX1 QUUX2 QUUX3) ],
414 foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') {
415 my $foo = $dp->parse($lose);
418 [ $foo->({ QUERY_STRING => '' }) ],
420 "${lose} fails with no query"
424 [ $foo->({ QUERY_STRING => 'bar=baz' }) ],
426 "${lose} fails with query missing foo key"
431 [ '?foo=' => 'FOO' ],
432 [ '?:foo=' => { foo => 'FOO' } ],
433 [ '?spoo~' => undef ],
436 [ '?:@spoo~' => { spoo => [] } ],
437 [ '?bar=' => 'BAR2' ],
438 [ '?:bar=' => { bar => 'BAR2' } ],
439 [ '?@bar=' => [ qw(BAR1 BAR2) ] ],
440 [ '?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
441 [ '?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
442 [ '?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
443 [ '?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
444 [ '?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
445 [ '?*' => \%all_single ],
446 [ '?@*' => \%all_multi ],
447 [ '?foo=&@*' => 'FOO', \%all_multi ],
448 [ '?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
449 [ '?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
451 my ($spec, @res) = @$win;
452 my $match = $dp->parse($spec);
453 #use Data::Dump::Streamer; warn Dump($match);
455 [ $match->({ QUERY_STRING => $q }) ],
457 "${spec} matches correctly"
462 # /path/info/ + query string
465 foreach my $lose2 ('/foo/bar/+?foo=','/foo/bar/+?:foo=','/foo/bar/+?@foo=','/foo/bar/+?:@foo=') {
466 my $foo = $dp->parse($lose2);
469 [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => '' }) ],
471 "${lose2} fails with no query"
475 [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => 'bar=baz' }) ],
477 "${lose2} fails with query missing foo key"
482 [ '/foo/bar/+?foo=' => 'FOO' ],
483 [ '/foo/bar/+?:foo=' => { foo => 'FOO' } ],
484 [ '/foo/bar/+?spoo~' => undef ],
485 [ '/foo/bar/+?:spoo~' => {} ],
486 [ '/foo/bar/+?@spoo~' => [] ],
487 [ '/foo/bar/+?:@spoo~' => { spoo => [] } ],
488 [ '/foo/bar/+?bar=' => 'BAR2' ],
489 [ '/foo/bar/+?:bar=' => { bar => 'BAR2' } ],
490 [ '/foo/bar/+?@bar=' => [ qw(BAR1 BAR2) ] ],
491 [ '/foo/bar/+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
492 [ '/foo/bar/+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
493 [ '/foo/bar/+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
494 [ '/foo/bar/+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
495 [ '/foo/bar/+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
496 [ '/foo/bar/+?*' => \%all_single ],
497 [ '/foo/bar/+?@*' => \%all_multi ],
498 [ '/foo/bar/+?foo=&@*' => 'FOO', \%all_multi ],
499 [ '/foo/bar/+?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
500 [ '/foo/bar/+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
502 my ($spec, @res) = @$win2;
503 my $match = $dp->parse($spec);
504 # use Data::Dump::Streamer; warn Dump($match);
506 [ $match->({ PATH_INFO => '/foo/bar/', QUERY_STRING => $q }) ],
508 "${spec} matches correctly"
513 # /path/info + query string
516 foreach my $lose3 ('/foo/bar+?foo=','/foo/bar+?:foo=','/foo/bar+?@foo=','/foo/bar+?:@foo=') {
517 my $foo = $dp->parse($lose3);
520 [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => '' }) ],
522 "${lose3} fails with no query"
526 [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => 'bar=baz' }) ],
528 "${lose3} fails with query missing foo key"
533 [ '/foo/bar+?foo=' => 'FOO' ],
534 [ '/foo/bar+?:foo=' => { foo => 'FOO' } ],
535 [ '/foo/bar+?spoo~' => undef ],
536 [ '/foo/bar+?:spoo~' => {} ],
537 [ '/foo/bar+?@spoo~' => [] ],
538 [ '/foo/bar+?:@spoo~' => { spoo => [] } ],
539 [ '/foo/bar+?bar=' => 'BAR2' ],
540 [ '/foo/bar+?:bar=' => { bar => 'BAR2' } ],
541 [ '/foo/bar+?@bar=' => [ qw(BAR1 BAR2) ] ],
542 [ '/foo/bar+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
543 [ '/foo/bar+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
544 [ '/foo/bar+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
545 [ '/foo/bar+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
546 [ '/foo/bar+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
547 [ '/foo/bar+?*' => \%all_single ],
548 [ '/foo/bar+?@*' => \%all_multi ],
549 [ '/foo/bar+?foo=&@*' => 'FOO', \%all_multi ],
550 [ '/foo/bar+?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
551 [ '/foo/bar+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
553 my ($spec, @res) = @$win3;
554 my $match = $dp->parse($spec);
555 # use Data::Dump::Streamer; warn Dump($match);
557 [ $match->({ PATH_INFO => '/foo/bar', QUERY_STRING => $q }) ],
559 "${spec} matches correctly"