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 $sub = $dp->parse('/foo/*/...');
260 [ $sub->({ PATH_INFO => '/foo/1/bar' }) ],
261 [ { PATH_INFO => '/bar', SCRIPT_NAME => '/foo/1' }, 1 ],
262 '/foo/*/... matches /foo/1/bar and strips to /bar'
266 [ $sub->({ PATH_INFO => '/foo/1/' }) ],
267 [ { PATH_INFO => '/', SCRIPT_NAME => '/foo/1' }, 1 ],
268 '/foo/*/... matches /foo/1/bar and strips to /'
272 [ $sub->({ PATH_INFO => '/foo/1' }) ],
274 '/foo/*/... does not match /foo/1 (no trailing /)'
280 [ '/one/*' => 'two' ],
281 [ '/one/*.*' => 'two.three' ],
282 [ '/**' => 'one/two' ],
283 [ '/**.*' => 'one/two.three' ],
286 foreach my $p (@dot_pairs) {
288 [ $dp->parse($p->[0])->({ PATH_INFO => '/one/two.three' }) ],
290 "${\$p->[0]} matches /one/two.three and returns ${\$p->[1]}"
299 my $q = 'foo=FOO&bar=BAR1&baz=one+two&quux=QUUX1&quux=QUUX2'
300 .'&bar=BAR2&quux=QUUX3&evil=%2F';
312 bar => [ qw(BAR1 BAR2) ],
313 baz => [ 'one two' ],
314 quux => [ qw(QUUX1 QUUX2 QUUX3) ],
318 foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') {
319 my $foo = $dp->parse($lose);
322 [ $foo->({ QUERY_STRING => '' }) ],
324 "${lose} fails with no query"
328 [ $foo->({ QUERY_STRING => 'bar=baz' }) ],
330 "${lose} fails with query missing foo key"
335 [ '?foo=' => 'FOO' ],
336 [ '?:foo=' => { foo => 'FOO' } ],
337 [ '?spoo~' => undef ],
340 [ '?:@spoo~' => { spoo => [] } ],
341 [ '?bar=' => 'BAR2' ],
342 [ '?:bar=' => { bar => 'BAR2' } ],
343 [ '?@bar=' => [ qw(BAR1 BAR2) ] ],
344 [ '?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
345 [ '?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
346 [ '?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
347 [ '?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
348 [ '?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
349 [ '?*' => \%all_single ],
350 [ '?@*' => \%all_multi ],
351 [ '?foo=&@*' => 'FOO', \%all_multi ],
352 [ '?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
353 [ '?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
355 my ($spec, @res) = @$win;
356 my $match = $dp->parse($spec);
357 #use Data::Dump::Streamer; warn Dump($match);
359 [ $match->({ QUERY_STRING => $q }) ],
361 "${spec} matches correctly"
366 # /path/info/ + query string
369 foreach my $lose2 ('/foo/bar/+?foo=','/foo/bar/+?:foo=','/foo/bar/+?@foo=','/foo/bar/+?:@foo=') {
370 my $foo = $dp->parse($lose2);
373 [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => '' }) ],
375 "${lose2} fails with no query"
379 [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => 'bar=baz' }) ],
381 "${lose2} fails with query missing foo key"
386 [ '/foo/bar/+?foo=' => 'FOO' ],
387 [ '/foo/bar/+?:foo=' => { foo => 'FOO' } ],
388 [ '/foo/bar/+?spoo~' => undef ],
389 [ '/foo/bar/+?:spoo~' => {} ],
390 [ '/foo/bar/+?@spoo~' => [] ],
391 [ '/foo/bar/+?:@spoo~' => { spoo => [] } ],
392 [ '/foo/bar/+?bar=' => 'BAR2' ],
393 [ '/foo/bar/+?:bar=' => { bar => 'BAR2' } ],
394 [ '/foo/bar/+?@bar=' => [ qw(BAR1 BAR2) ] ],
395 [ '/foo/bar/+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
396 [ '/foo/bar/+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
397 [ '/foo/bar/+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
398 [ '/foo/bar/+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
399 [ '/foo/bar/+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
400 [ '/foo/bar/+?*' => \%all_single ],
401 [ '/foo/bar/+?@*' => \%all_multi ],
402 [ '/foo/bar/+?foo=&@*' => 'FOO', \%all_multi ],
403 [ '/foo/bar/+?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
404 [ '/foo/bar/+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
406 my ($spec, @res) = @$win2;
407 my $match = $dp->parse($spec);
408 # use Data::Dump::Streamer; warn Dump($match);
410 [ $match->({ PATH_INFO => '/foo/bar/', QUERY_STRING => $q }) ],
412 "${spec} matches correctly"
417 # /path/info + query string
420 foreach my $lose3 ('/foo/bar+?foo=','/foo/bar+?:foo=','/foo/bar+?@foo=','/foo/bar+?:@foo=') {
421 my $foo = $dp->parse($lose3);
424 [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => '' }) ],
426 "${lose3} fails with no query"
430 [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => 'bar=baz' }) ],
432 "${lose3} fails with query missing foo key"
437 [ '/foo/bar+?foo=' => 'FOO' ],
438 [ '/foo/bar+?:foo=' => { foo => 'FOO' } ],
439 [ '/foo/bar+?spoo~' => undef ],
440 [ '/foo/bar+?:spoo~' => {} ],
441 [ '/foo/bar+?@spoo~' => [] ],
442 [ '/foo/bar+?:@spoo~' => { spoo => [] } ],
443 [ '/foo/bar+?bar=' => 'BAR2' ],
444 [ '/foo/bar+?:bar=' => { bar => 'BAR2' } ],
445 [ '/foo/bar+?@bar=' => [ qw(BAR1 BAR2) ] ],
446 [ '/foo/bar+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
447 [ '/foo/bar+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
448 [ '/foo/bar+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
449 [ '/foo/bar+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
450 [ '/foo/bar+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
451 [ '/foo/bar+?*' => \%all_single ],
452 [ '/foo/bar+?@*' => \%all_multi ],
453 [ '/foo/bar+?foo=&@*' => 'FOO', \%all_multi ],
454 [ '/foo/bar+?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
455 [ '/foo/bar+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
457 my ($spec, @res) = @$win3;
458 my $match = $dp->parse($spec);
459 # use Data::Dump::Streamer; warn Dump($match);
461 [ $match->({ PATH_INFO => '/foo/bar', QUERY_STRING => $q }) ],
463 "${spec} matches correctly"