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'
217 my $spec = '(GET+/foo)|(POST+/foo)';
218 my $nest = $dp->parse($spec);
220 for my $method (qw( GET POST )) {
222 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => $method }) ],
224 "$spec matches $method /foo"
227 [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => $method }) ],
229 "$spec does not match $method /bar"
234 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'PUT' }) ],
236 "$spec does not match PUT /foo"
243 !eval { $dp->parse('/foo+(GET'); 1 },
244 'Death with missing closing )'
250 (s/^\n//s,s/\n $//s,s/^ //mg) for $err;
254 "Error $@ matches\n${err}\n"
259 my $not = $dp->parse('!.html+.*');
262 [ $not->({ PATH_INFO => '/foo.xml' }) ],
264 '!.html+.* matches /foo.xml'
268 [ $not->({ PATH_INFO => '/foo.html' }) ],
270 '!.html+.* does not match /foo.html'
274 [ $not->({ PATH_INFO => '/foo' }) ],
276 '!.html+.* does not match /foo'
281 my $ext = $dp->parse('/foo.bar');
284 [ $ext->({ PATH_INFO => '/foo.bar' }) ],
286 '/foo.bar matches /foo.bar'
290 [ $ext->({ PATH_INFO => '/foo.bar.ext' }) ],
292 '/foo.bar matches /foo.bar.ext'
296 [ $ext->({ PATH_INFO => '/foo.notbar' }) ],
298 '/foo.bar does not match /foo.notbar'
303 my $sub = $dp->parse('/foo/*/...');
306 [ $sub->({ PATH_INFO => '/foo/1/bar' }) ],
307 [ { PATH_INFO => '/bar', SCRIPT_NAME => '/foo/1' }, 1 ],
308 '/foo/*/... matches /foo/1/bar and strips to /bar'
312 [ $sub->({ PATH_INFO => '/foo/1/' }) ],
313 [ { PATH_INFO => '/', SCRIPT_NAME => '/foo/1' }, 1 ],
314 '/foo/*/... matches /foo/1/bar and strips to /'
318 [ $sub->({ PATH_INFO => '/foo/1' }) ],
320 '/foo/*/... does not match /foo/1 (no trailing /)'
325 my $sub = $dp->parse('/foo/**/belief');
326 my $match = 'barred/beyond';
328 [ $sub->({ PATH_INFO => "/foo/${match}/belief" }) ],
330 "/foo/**/belief matches /foo/${match}/belief"
336 my $sub = $dp->parse($match);
339 [ $sub->({ PATH_INFO => '/foo' }) ],
341 "$match does not match /foo"
345 [ $sub->({ PATH_INFO => '' }) ],
347 "$match matches empty path with empty env"
352 my $match = '/foo...';
353 my $sub = $dp->parse($match);
356 [ $sub->({ PATH_INFO => '/foobar' }) ],
358 "$match does not match /foobar"
362 [ $sub->({ PATH_INFO => '/foo/bar' }) ],
363 [ { PATH_INFO => '/bar', SCRIPT_NAME => '/foo' } ],
364 "$match matches /foo/bar and strips to /bar"
368 [ $sub->({ PATH_INFO => '/foo/' }) ],
369 [ { PATH_INFO => '/', SCRIPT_NAME => '/foo' } ],
370 "$match matches /foo/ and strips to /"
374 [ $sub->({ PATH_INFO => '/foo' }) ],
375 [ { PATH_INFO => '', SCRIPT_NAME => '/foo' } ],
376 "$match matches /foo and strips to empty path"
382 [ '/one/*' => 'two' ],
383 [ '/one/*.*' => 'two.three' ],
384 [ '/**' => 'one/two' ],
385 [ '/**.*' => 'one/two.three' ],
388 foreach my $p (@dot_pairs) {
390 [ $dp->parse($p->[0])->({ PATH_INFO => '/one/two.three' }) ],
392 "${\$p->[0]} matches /one/two.three and returns ${\$p->[1]}"
399 [ '/foo/*:foo_id' => '/foo/1' => { foo_id => 1 } ],
400 [ '/foo/:foo_id' => '/foo/1' => { foo_id => 1 } ],
401 [ '/foo/:id/**:rest' => '/foo/id/rest/of/the/path.ext'
402 => { id => 'id', rest => 'rest/of/the/path' } ],
403 [ '/foo/:id/**.*:rest' => '/foo/id/rest/of/the/path.ext'
404 => { id => 'id', rest => 'rest/of/the/path.ext' } ],
406 foreach my $n (@named) {
408 [ $dp->parse($n->[0])->({ PATH_INFO => $n->[1] }) ],
410 "${\$n->[0]} matches ${\$n->[1]} with correct captures"
419 my $q = 'foo=FOO&bar=BAR1&baz=one+two&quux=QUUX1&quux=QUUX2'
420 .'&foo.bar=FOOBAR1&foo.bar=FOOBAR2&foo.baz=FOOBAZ'
421 .'&bar=BAR2&quux=QUUX3&evil=%2F';
429 'foo.baz' => 'FOOBAZ',
430 'foo.bar' => 'FOOBAR2',
435 bar => [ qw(BAR1 BAR2) ],
436 baz => [ 'one two' ],
437 quux => [ qw(QUUX1 QUUX2 QUUX3) ],
439 'foo.baz' => [ 'FOOBAZ' ],
440 'foo.bar' => [ qw(FOOBAR1 FOOBAR2) ],
443 foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') {
444 my $foo = $dp->parse($lose);
447 [ $foo->({ QUERY_STRING => '' }) ],
449 "${lose} fails with no query"
453 [ $foo->({ QUERY_STRING => 'bar=baz' }) ],
455 "${lose} fails with query missing foo key"
460 [ '?foo=' => 'FOO' ],
461 [ '?:foo=' => { foo => 'FOO' } ],
462 [ '?spoo~' => undef ],
465 [ '?:@spoo~' => { spoo => [] } ],
466 [ '?bar=' => 'BAR2' ],
467 [ '?:bar=' => { bar => 'BAR2' } ],
468 [ '?@bar=' => [ qw(BAR1 BAR2) ] ],
469 [ '?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
470 [ '?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
471 [ '?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
472 [ '?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
473 [ '?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
474 [ '?*' => \%all_single ],
475 [ '?@*' => \%all_multi ],
476 [ '?foo=&@*' => 'FOO', \%all_multi ],
477 [ '?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
478 [ '?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
479 [ '?foo.baz=' => 'FOOBAZ' ],
480 [ '?:foo.baz=' => { 'foo.baz' => 'FOOBAZ' } ],
481 [ '?foo.bar=' => 'FOOBAR2' ],
482 [ '?:foo.bar=' => { 'foo.bar' => 'FOOBAR2' } ],
483 [ '?@foo.bar=' => [ qw(FOOBAR1 FOOBAR2) ] ],
484 [ '?:@foo.bar=' => { 'foo.bar' => [ qw(FOOBAR1 FOOBAR2) ] } ],
486 my ($spec, @res) = @$win;
487 my $match = $dp->parse($spec);
488 #use Data::Dump::Streamer; warn Dump($match);
490 [ $match->({ QUERY_STRING => $q }) ],
492 "${spec} matches correctly"
497 # /path/info/ + query string
500 foreach my $lose2 ('/foo/bar/+?foo=','/foo/bar/+?:foo=','/foo/bar/+?@foo=','/foo/bar/+?:@foo=') {
501 my $foo = $dp->parse($lose2);
504 [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => '' }) ],
506 "${lose2} fails with no query"
510 [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => 'bar=baz' }) ],
512 "${lose2} fails with query missing foo key"
517 [ '/foo/bar/+?foo=' => 'FOO' ],
518 [ '/foo/bar/+?:foo=' => { foo => 'FOO' } ],
519 [ '/foo/bar/+?spoo~' => undef ],
520 [ '/foo/bar/+?:spoo~' => {} ],
521 [ '/foo/bar/+?@spoo~' => [] ],
522 [ '/foo/bar/+?:@spoo~' => { spoo => [] } ],
523 [ '/foo/bar/+?bar=' => 'BAR2' ],
524 [ '/foo/bar/+?:bar=' => { bar => 'BAR2' } ],
525 [ '/foo/bar/+?@bar=' => [ qw(BAR1 BAR2) ] ],
526 [ '/foo/bar/+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
527 [ '/foo/bar/+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
528 [ '/foo/bar/+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
529 [ '/foo/bar/+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
530 [ '/foo/bar/+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
531 [ '/foo/bar/+?*' => \%all_single ],
532 [ '/foo/bar/+?@*' => \%all_multi ],
533 [ '/foo/bar/+?foo=&@*' => 'FOO', \%all_multi ],
534 [ '/foo/bar/+?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
535 [ '/foo/bar/+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
536 [ '/foo/bar/+?foo.baz=' => 'FOOBAZ' ],
537 [ '/foo/bar/+?:foo.baz=' => { 'foo.baz' => 'FOOBAZ' } ],
538 [ '/foo/bar/+?foo.bar=' => 'FOOBAR2' ],
539 [ '/foo/bar/+?:foo.bar=' => { 'foo.bar' => 'FOOBAR2' } ],
540 [ '/foo/bar/+?@foo.bar=' => [ qw(FOOBAR1 FOOBAR2) ] ],
541 [ '/foo/bar/+?:@foo.bar=' => { 'foo.bar' => [ qw(FOOBAR1 FOOBAR2) ] } ],
543 my ($spec, @res) = @$win2;
544 my $match = $dp->parse($spec);
545 # use Data::Dump::Streamer; warn Dump($match);
547 [ $match->({ PATH_INFO => '/foo/bar/', QUERY_STRING => $q }) ],
549 "${spec} matches correctly"
554 # /path/info + query string
557 foreach my $lose3 ('/foo/bar+?foo=','/foo/bar+?:foo=','/foo/bar+?@foo=','/foo/bar+?:@foo=') {
558 my $foo = $dp->parse($lose3);
561 [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => '' }) ],
563 "${lose3} fails with no query"
567 [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => 'bar=baz' }) ],
569 "${lose3} fails with query missing foo key"
574 [ '/foo/bar+?foo=' => 'FOO' ],
575 [ '/foo/bar+?:foo=' => { foo => 'FOO' } ],
576 [ '/foo/bar+?spoo~' => undef ],
577 [ '/foo/bar+?:spoo~' => {} ],
578 [ '/foo/bar+?@spoo~' => [] ],
579 [ '/foo/bar+?:@spoo~' => { spoo => [] } ],
580 [ '/foo/bar+?bar=' => 'BAR2' ],
581 [ '/foo/bar+?:bar=' => { bar => 'BAR2' } ],
582 [ '/foo/bar+?@bar=' => [ qw(BAR1 BAR2) ] ],
583 [ '/foo/bar+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
584 [ '/foo/bar+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
585 [ '/foo/bar+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
586 [ '/foo/bar+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
587 [ '/foo/bar+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
588 [ '/foo/bar+?*' => \%all_single ],
589 [ '/foo/bar+?@*' => \%all_multi ],
590 [ '/foo/bar+?foo=&@*' => 'FOO', \%all_multi ],
591 [ '/foo/bar+?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
592 [ '/foo/bar+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
593 [ '/foo/bar+?foo.baz=' => 'FOOBAZ' ],
594 [ '/foo/bar+?:foo.baz=' => { 'foo.baz' => 'FOOBAZ' } ],
595 [ '/foo/bar+?foo.bar=' => 'FOOBAR2' ],
596 [ '/foo/bar+?:foo.bar=' => { 'foo.bar' => 'FOOBAR2' } ],
597 [ '/foo/bar+?@foo.bar=' => [ qw(FOOBAR1 FOOBAR2) ] ],
598 [ '/foo/bar+?:@foo.bar=' => { 'foo.bar' => [ qw(FOOBAR1 FOOBAR2) ] } ],
600 my ($spec, @res) = @$win3;
601 my $match = $dp->parse($spec);
602 # use Data::Dump::Streamer; warn Dump($match);
604 [ $match->({ PATH_INFO => '/foo/bar', QUERY_STRING => $q }) ],
606 "${spec} matches correctly"