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 .'&foo.bar=FOOBAR1&foo.bar=FOOBAR2&foo.baz=FOOBAZ'
397 .'&bar=BAR2&quux=QUUX3&evil=%2F';
405 'foo.baz' => 'FOOBAZ',
406 'foo.bar' => 'FOOBAR2',
411 bar => [ qw(BAR1 BAR2) ],
412 baz => [ 'one two' ],
413 quux => [ qw(QUUX1 QUUX2 QUUX3) ],
415 'foo.baz' => [ 'FOOBAZ' ],
416 'foo.bar' => [ qw(FOOBAR1 FOOBAR2) ],
419 foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') {
420 my $foo = $dp->parse($lose);
423 [ $foo->({ QUERY_STRING => '' }) ],
425 "${lose} fails with no query"
429 [ $foo->({ QUERY_STRING => 'bar=baz' }) ],
431 "${lose} fails with query missing foo key"
436 [ '?foo=' => 'FOO' ],
437 [ '?:foo=' => { foo => 'FOO' } ],
438 [ '?spoo~' => undef ],
441 [ '?:@spoo~' => { spoo => [] } ],
442 [ '?bar=' => 'BAR2' ],
443 [ '?:bar=' => { bar => 'BAR2' } ],
444 [ '?@bar=' => [ qw(BAR1 BAR2) ] ],
445 [ '?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
446 [ '?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
447 [ '?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
448 [ '?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
449 [ '?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
450 [ '?*' => \%all_single ],
451 [ '?@*' => \%all_multi ],
452 [ '?foo=&@*' => 'FOO', \%all_multi ],
453 [ '?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
454 [ '?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
455 [ '?foo.baz=' => 'FOOBAZ' ],
456 [ '?:foo.baz=' => { 'foo.baz' => 'FOOBAZ' } ],
457 [ '?foo.bar=' => 'FOOBAR2' ],
458 [ '?:foo.bar=' => { 'foo.bar' => 'FOOBAR2' } ],
459 [ '?@foo.bar=' => [ qw(FOOBAR1 FOOBAR2) ] ],
460 [ '?:@foo.bar=' => { 'foo.bar' => [ qw(FOOBAR1 FOOBAR2) ] } ],
462 my ($spec, @res) = @$win;
463 my $match = $dp->parse($spec);
464 #use Data::Dump::Streamer; warn Dump($match);
466 [ $match->({ QUERY_STRING => $q }) ],
468 "${spec} matches correctly"
473 # /path/info/ + query string
476 foreach my $lose2 ('/foo/bar/+?foo=','/foo/bar/+?:foo=','/foo/bar/+?@foo=','/foo/bar/+?:@foo=') {
477 my $foo = $dp->parse($lose2);
480 [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => '' }) ],
482 "${lose2} fails with no query"
486 [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => 'bar=baz' }) ],
488 "${lose2} fails with query missing foo key"
493 [ '/foo/bar/+?foo=' => 'FOO' ],
494 [ '/foo/bar/+?:foo=' => { foo => 'FOO' } ],
495 [ '/foo/bar/+?spoo~' => undef ],
496 [ '/foo/bar/+?:spoo~' => {} ],
497 [ '/foo/bar/+?@spoo~' => [] ],
498 [ '/foo/bar/+?:@spoo~' => { spoo => [] } ],
499 [ '/foo/bar/+?bar=' => 'BAR2' ],
500 [ '/foo/bar/+?:bar=' => { bar => 'BAR2' } ],
501 [ '/foo/bar/+?@bar=' => [ qw(BAR1 BAR2) ] ],
502 [ '/foo/bar/+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
503 [ '/foo/bar/+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
504 [ '/foo/bar/+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
505 [ '/foo/bar/+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
506 [ '/foo/bar/+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
507 [ '/foo/bar/+?*' => \%all_single ],
508 [ '/foo/bar/+?@*' => \%all_multi ],
509 [ '/foo/bar/+?foo=&@*' => 'FOO', \%all_multi ],
510 [ '/foo/bar/+?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
511 [ '/foo/bar/+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
512 [ '/foo/bar/+?foo.baz=' => 'FOOBAZ' ],
513 [ '/foo/bar/+?:foo.baz=' => { 'foo.baz' => 'FOOBAZ' } ],
514 [ '/foo/bar/+?foo.bar=' => 'FOOBAR2' ],
515 [ '/foo/bar/+?:foo.bar=' => { 'foo.bar' => 'FOOBAR2' } ],
516 [ '/foo/bar/+?@foo.bar=' => [ qw(FOOBAR1 FOOBAR2) ] ],
517 [ '/foo/bar/+?:@foo.bar=' => { 'foo.bar' => [ qw(FOOBAR1 FOOBAR2) ] } ],
519 my ($spec, @res) = @$win2;
520 my $match = $dp->parse($spec);
521 # use Data::Dump::Streamer; warn Dump($match);
523 [ $match->({ PATH_INFO => '/foo/bar/', QUERY_STRING => $q }) ],
525 "${spec} matches correctly"
530 # /path/info + query string
533 foreach my $lose3 ('/foo/bar+?foo=','/foo/bar+?:foo=','/foo/bar+?@foo=','/foo/bar+?:@foo=') {
534 my $foo = $dp->parse($lose3);
537 [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => '' }) ],
539 "${lose3} fails with no query"
543 [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => 'bar=baz' }) ],
545 "${lose3} fails with query missing foo key"
550 [ '/foo/bar+?foo=' => 'FOO' ],
551 [ '/foo/bar+?:foo=' => { foo => 'FOO' } ],
552 [ '/foo/bar+?spoo~' => undef ],
553 [ '/foo/bar+?:spoo~' => {} ],
554 [ '/foo/bar+?@spoo~' => [] ],
555 [ '/foo/bar+?:@spoo~' => { spoo => [] } ],
556 [ '/foo/bar+?bar=' => 'BAR2' ],
557 [ '/foo/bar+?:bar=' => { bar => 'BAR2' } ],
558 [ '/foo/bar+?@bar=' => [ qw(BAR1 BAR2) ] ],
559 [ '/foo/bar+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
560 [ '/foo/bar+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
561 [ '/foo/bar+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
562 [ '/foo/bar+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
563 [ '/foo/bar+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
564 [ '/foo/bar+?*' => \%all_single ],
565 [ '/foo/bar+?@*' => \%all_multi ],
566 [ '/foo/bar+?foo=&@*' => 'FOO', \%all_multi ],
567 [ '/foo/bar+?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
568 [ '/foo/bar+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
569 [ '/foo/bar+?foo.baz=' => 'FOOBAZ' ],
570 [ '/foo/bar+?:foo.baz=' => { 'foo.baz' => 'FOOBAZ' } ],
571 [ '/foo/bar+?foo.bar=' => 'FOOBAR2' ],
572 [ '/foo/bar+?:foo.bar=' => { 'foo.bar' => 'FOOBAR2' } ],
573 [ '/foo/bar+?@foo.bar=' => [ qw(FOOBAR1 FOOBAR2) ] ],
574 [ '/foo/bar+?:@foo.bar=' => { 'foo.bar' => [ qw(FOOBAR1 FOOBAR2) ] } ],
576 my ($spec, @res) = @$win3;
577 my $match = $dp->parse($spec);
578 # use Data::Dump::Streamer; warn Dump($match);
580 [ $match->({ PATH_INFO => '/foo/bar', QUERY_STRING => $q }) ],
582 "${spec} matches correctly"