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 $get = $dp->parse('GET');
14 [ $get->({ REQUEST_METHOD => 'GET' }) ],
20 [ $get->({ REQUEST_METHOD => 'POST' }) ],
27 my $html = $dp->parse('.html');
30 [ $html->({ PATH_INFO => '/foo/bar.html' }) ],
36 [ $html->({ PATH_INFO => '/foo/bar.xml' }) ],
38 '.xml does not match .html'
43 my $any_ext = $dp->parse('.*');
46 [ $any_ext->({ PATH_INFO => '/foo/bar.html' }) ],
48 '.html matches .* and extension returned'
52 [ $any_ext->({ PATH_INFO => '/foo/bar' }) ],
54 'no extension does not match .*'
59 my $slash = $dp->parse('/');
62 [ $slash->({ PATH_INFO => '/' }) ],
68 [ $slash->({ PATH_INFO => '/foo' }) ],
70 '/foo does not match /'
75 my $post = $dp->parse('/post/*');
78 [ $post->({ PATH_INFO => '/post/one' }) ],
80 '/post/one parses out one'
84 [ $post->({ PATH_INFO => '/post/one/' }) ],
86 '/post/one/ does not match'
90 [ $post->({ PATH_INFO => '/post/one.html' }) ],
92 '/post/one.html still parses out one'
97 my $post = $dp->parse('/foo-bar/*');
100 [ $post->({ PATH_INFO => '/foo-bar/one' }) ],
102 '/foo-bar/one parses out one'
106 [ $post->({ PATH_INFO => '/foo-bar/one/' }) ],
108 '/foo-bar/one/ does not match'
113 my $combi = $dp->parse('GET+/post/*');
116 [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'GET' }) ],
118 '/post/one parses out one'
122 [ $combi->({ PATH_INFO => '/post/one/', REQUEST_METHOD => 'GET' }) ],
124 '/post/one/ does not match'
128 [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'POST' }) ],
130 'POST /post/one does not match'
135 my $or = $dp->parse('GET|POST');
137 foreach my $meth (qw(GET POST)) {
140 [ $or->({ REQUEST_METHOD => $meth }) ],
142 'GET|POST matches method '.$meth
147 [ $or->({ REQUEST_METHOD => 'PUT' }) ],
149 'GET|POST does not match PUT'
154 my $or = $dp->parse('GET|POST|DELETE');
156 foreach my $meth (qw(GET POST DELETE)) {
159 [ $or->({ REQUEST_METHOD => $meth }) ],
161 'GET|POST|DELETE matches method '.$meth
166 [ $or->({ REQUEST_METHOD => 'PUT' }) ],
168 'GET|POST|DELETE does not match PUT'
173 my $nest = $dp->parse('(GET+/foo)|POST');
176 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'GET' }) ],
178 '(GET+/foo)|POST matches GET /foo'
182 [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'GET' }) ],
184 '(GET+/foo)|POST does not match GET /bar'
188 [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'POST' }) ],
190 '(GET+/foo)|POST matches POST /bar'
194 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'PUT' }) ],
196 '(GET+/foo)|POST does not match PUT /foo'
203 !eval { $dp->parse('/foo+(GET'); 1 },
204 'Death with missing closing )'
210 (s/^\n//s,s/\n $//s,s/^ //mg) for $err;
214 "Error $@ matches\n${err}\n"
219 my $not = $dp->parse('!.html+.*');
222 [ $not->({ PATH_INFO => '/foo.xml' }) ],
224 '!.html+.* matches /foo.xml'
228 [ $not->({ PATH_INFO => '/foo.html' }) ],
230 '!.html+.* does not match /foo.html'
234 [ $not->({ PATH_INFO => '/foo' }) ],
236 '!.html+.* does not match /foo'
241 my $sub = $dp->parse('/foo/*/...');
244 [ $sub->({ PATH_INFO => '/foo/1/bar' }) ],
245 [ { PATH_INFO => '/bar', SCRIPT_NAME => '/foo/1' }, 1 ],
246 '/foo/*/... matches /foo/1/bar and strips to /bar'
250 [ $sub->({ PATH_INFO => '/foo/1/' }) ],
251 [ { PATH_INFO => '/', SCRIPT_NAME => '/foo/1' }, 1 ],
252 '/foo/*/... matches /foo/1/bar and strips to /'
256 [ $sub->({ PATH_INFO => '/foo/1' }) ],
258 '/foo/*/... does not match /foo/1 (no trailing /)'
264 [ '/one/*' => 'two' ],
265 [ '/one/*.*' => 'two.three' ],
266 [ '/**' => 'one/two' ],
267 [ '/**.*' => 'one/two.three' ],
270 foreach my $p (@dot_pairs) {
272 [ $dp->parse($p->[0])->({ PATH_INFO => '/one/two.three' }) ],
274 "${\$p->[0]} matches /one/two.three and returns ${\$p->[1]}"
283 my $q = 'foo=FOO&bar=BAR1&baz=one+two&quux=QUUX1&quux=QUUX2'
284 .'&bar=BAR2&quux=QUUX3&evil=%2F';
296 bar => [ qw(BAR1 BAR2) ],
297 baz => [ 'one two' ],
298 quux => [ qw(QUUX1 QUUX2 QUUX3) ],
302 foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') {
303 my $foo = $dp->parse($lose);
306 [ $foo->({ QUERY_STRING => '' }) ],
308 "${lose} fails with no query"
312 [ $foo->({ QUERY_STRING => 'bar=baz' }) ],
314 "${lose} fails with query missing foo key"
319 [ '?foo=' => 'FOO' ],
320 [ '?:foo=' => { foo => 'FOO' } ],
321 [ '?spoo~' => undef ],
324 [ '?:@spoo~' => { spoo => [] } ],
325 [ '?bar=' => 'BAR2' ],
326 [ '?:bar=' => { bar => 'BAR2' } ],
327 [ '?@bar=' => [ qw(BAR1 BAR2) ] ],
328 [ '?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
329 [ '?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
330 [ '?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
331 [ '?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
332 [ '?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
333 [ '?*' => \%all_single ],
334 [ '?@*' => \%all_multi ],
335 [ '?foo=&@*' => 'FOO', \%all_multi ],
336 [ '?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
337 [ '?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
339 my ($spec, @res) = @$win;
340 my $match = $dp->parse($spec);
341 #use Data::Dump::Streamer; warn Dump($match);
343 [ $match->({ QUERY_STRING => $q }) ],
345 "${spec} matches correctly"
350 # /path/info/ + query string
353 foreach my $lose2 ('/foo/bar/+?foo=','/foo/bar/+?:foo=','/foo/bar/+?@foo=','/foo/bar/+?:@foo=') {
354 my $foo = $dp->parse($lose2);
357 [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => '' }) ],
359 "${lose2} fails with no query"
363 [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => 'bar=baz' }) ],
365 "${lose2} fails with query missing foo key"
370 [ '/foo/bar/+?foo=' => 'FOO' ],
371 [ '/foo/bar/+?:foo=' => { foo => 'FOO' } ],
372 [ '/foo/bar/+?spoo~' => undef ],
373 [ '/foo/bar/+?:spoo~' => {} ],
374 [ '/foo/bar/+?@spoo~' => [] ],
375 [ '/foo/bar/+?:@spoo~' => { spoo => [] } ],
376 [ '/foo/bar/+?bar=' => 'BAR2' ],
377 [ '/foo/bar/+?:bar=' => { bar => 'BAR2' } ],
378 [ '/foo/bar/+?@bar=' => [ qw(BAR1 BAR2) ] ],
379 [ '/foo/bar/+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
380 [ '/foo/bar/+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
381 [ '/foo/bar/+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
382 [ '/foo/bar/+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
383 [ '/foo/bar/+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
384 [ '/foo/bar/+?*' => \%all_single ],
385 [ '/foo/bar/+?@*' => \%all_multi ],
386 [ '/foo/bar/+?foo=&@*' => 'FOO', \%all_multi ],
387 [ '/foo/bar/+?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
388 [ '/foo/bar/+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
390 my ($spec, @res) = @$win2;
391 my $match = $dp->parse($spec);
392 # use Data::Dump::Streamer; warn Dump($match);
394 [ $match->({ PATH_INFO => '/foo/bar/', QUERY_STRING => $q }) ],
396 "${spec} matches correctly"
401 # /path/info + query string
404 foreach my $lose3 ('/foo/bar+?foo=','/foo/bar+?:foo=','/foo/bar+?@foo=','/foo/bar+?:@foo=') {
405 my $foo = $dp->parse($lose3);
408 [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => '' }) ],
410 "${lose3} fails with no query"
414 [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => 'bar=baz' }) ],
416 "${lose3} fails with query missing foo key"
421 [ '/foo/bar+?foo=' => 'FOO' ],
422 [ '/foo/bar+?:foo=' => { foo => 'FOO' } ],
423 [ '/foo/bar+?spoo~' => undef ],
424 [ '/foo/bar+?:spoo~' => {} ],
425 [ '/foo/bar+?@spoo~' => [] ],
426 [ '/foo/bar+?:@spoo~' => { spoo => [] } ],
427 [ '/foo/bar+?bar=' => 'BAR2' ],
428 [ '/foo/bar+?:bar=' => { bar => 'BAR2' } ],
429 [ '/foo/bar+?@bar=' => [ qw(BAR1 BAR2) ] ],
430 [ '/foo/bar+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
431 [ '/foo/bar+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
432 [ '/foo/bar+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
433 [ '/foo/bar+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
434 [ '/foo/bar+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
435 [ '/foo/bar+?*' => \%all_single ],
436 [ '/foo/bar+?@*' => \%all_multi ],
437 [ '/foo/bar+?foo=&@*' => 'FOO', \%all_multi ],
438 [ '/foo/bar+?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
439 [ '/foo/bar+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
441 my ($spec, @res) = @$win3;
442 my $match = $dp->parse($spec);
443 # use Data::Dump::Streamer; warn Dump($match);
445 [ $match->({ PATH_INFO => '/foo/bar', QUERY_STRING => $q }) ],
447 "${spec} matches correctly"