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 !eval { $dp->parse('GET POST'); 1; },
28 "Don't yet allow two methods"
32 my $html = $dp->parse('.html');
35 [ $html->({ PATH_INFO => '/foo/bar.html' }) ],
36 [ { PATH_INFO => '/foo/bar' } ],
41 [ $html->({ PATH_INFO => '/foo/bar.xml' }) ],
43 '.xml does not match .html'
48 my $any_ext = $dp->parse('.*');
51 [ $any_ext->({ PATH_INFO => '/foo/bar.html' }) ],
52 [ { PATH_INFO => '/foo/bar' }, 'html' ],
53 '.html matches .* and extension returned'
57 [ $any_ext->({ PATH_INFO => '/foo/bar' }) ],
59 'no extension does not match .*'
64 my $slash = $dp->parse('/');
67 [ $slash->({ PATH_INFO => '/' }) ],
73 [ $slash->({ PATH_INFO => '/foo' }) ],
75 '/foo does not match /'
80 my $post = $dp->parse('/post/*');
83 [ $post->({ PATH_INFO => '/post/one' }) ],
85 '/post/one parses out one'
89 [ $post->({ PATH_INFO => '/post/one/' }) ],
91 '/post/one/ does not match'
96 my $post = $dp->parse('/foo-bar/*');
99 [ $post->({ PATH_INFO => '/foo-bar/one' }) ],
101 '/foo-bar/one parses out one'
105 [ $post->({ PATH_INFO => '/foo-bar/one/' }) ],
107 '/foo-bar/one/ does not match'
112 my $combi = $dp->parse('GET+/post/*');
115 [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'GET' }) ],
117 '/post/one parses out one'
121 [ $combi->({ PATH_INFO => '/post/one/', REQUEST_METHOD => 'GET' }) ],
123 '/post/one/ does not match'
127 [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'POST' }) ],
129 'POST /post/one does not match'
134 my $or = $dp->parse('GET|POST');
136 foreach my $meth (qw(GET POST)) {
139 [ $or->({ REQUEST_METHOD => $meth }) ],
141 'GET|POST matches method '.$meth
146 [ $or->({ REQUEST_METHOD => 'PUT' }) ],
148 'GET|POST does not match PUT'
153 my $or = $dp->parse('GET|POST|DELETE');
155 foreach my $meth (qw(GET POST DELETE)) {
158 [ $or->({ REQUEST_METHOD => $meth }) ],
160 'GET|POST|DELETE matches method '.$meth
165 [ $or->({ REQUEST_METHOD => 'PUT' }) ],
167 'GET|POST|DELETE does not match PUT'
172 my $nest = $dp->parse('(GET+/foo)|POST');
175 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'GET' }) ],
177 '(GET+/foo)|POST matches GET /foo'
181 [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'GET' }) ],
183 '(GET+/foo)|POST does not match GET /bar'
187 [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'POST' }) ],
189 '(GET+/foo)|POST matches POST /bar'
193 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'PUT' }) ],
195 '(GET+/foo)|POST does not match PUT /foo'
202 !eval { $dp->parse('/foo+(GET'); 1 },
203 'Death with missing closing )'
209 (s/^\n//s,s/\n $//s,s/^ //mg) for $err;
213 "Error $@ matches\n${err}\n"
218 my $not = $dp->parse('!.html+.*');
221 [ $not->({ PATH_INFO => '/foo.xml' }) ],
222 [ { PATH_INFO => '/foo' }, 'xml' ],
223 '!.html+.* matches /foo.xml'
227 [ $not->({ PATH_INFO => '/foo.html' }) ],
229 '!.html+.* does not match /foo.html'
233 [ $not->({ PATH_INFO => '/foo' }) ],
235 '!.html+.* does not match /foo'
240 my $sub = $dp->parse('/foo/*/...');
243 [ $sub->({ PATH_INFO => '/foo/1/bar' }) ],
244 [ { PATH_INFO => '/bar', SCRIPT_NAME => '/foo/1' }, 1 ],
245 '/foo/*/... matches /foo/1/bar and strips to /bar'
249 [ $sub->({ PATH_INFO => '/foo/1/' }) ],
250 [ { PATH_INFO => '/', SCRIPT_NAME => '/foo/1' }, 1 ],
251 '/foo/*/... matches /foo/1/bar and strips to /'
255 [ $sub->({ PATH_INFO => '/foo/1' }) ],
257 '/foo/*/... does not match /foo/1 (no trailing /)'
265 my $q = 'foo=FOO&bar=BAR1&baz=one+two&quux=QUUX1&quux=QUUX2'
266 .'&bar=BAR2&quux=QUUX3&evil=%2F';
278 bar => [ qw(BAR1 BAR2) ],
279 baz => [ 'one two' ],
280 quux => [ qw(QUUX1 QUUX2 QUUX3) ],
284 foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') {
285 my $foo = $dp->parse($lose);
288 [ $foo->({ QUERY_STRING => '' }) ],
290 "${lose} fails with no query"
294 [ $foo->({ QUERY_STRING => 'bar=baz' }) ],
296 "${lose} fails with query missing foo key"
301 [ '?foo=' => 'FOO' ],
302 [ '?:foo=' => { foo => 'FOO' } ],
303 [ '?spoo~' => undef ],
306 [ '?:@spoo~' => { spoo => [] } ],
307 [ '?bar=' => 'BAR2' ],
308 [ '?:bar=' => { bar => 'BAR2' } ],
309 [ '?@bar=' => [ qw(BAR1 BAR2) ] ],
310 [ '?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
311 [ '?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
312 [ '?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
313 [ '?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
314 [ '?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
315 [ '?*' => \%all_single ],
316 [ '?@*' => \%all_multi ],
317 [ '?foo=&@*' => 'FOO', do { my %h = %all_multi; delete $h{foo}; \%h } ],
318 [ '?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
319 [ '?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
321 my ($spec, @res) = @$win;
322 my $match = $dp->parse($spec);
323 #use Data::Dump::Streamer; warn Dump($match);
325 [ $match->({ QUERY_STRING => $q }) ],
327 "${spec} matches correctly"
332 # /path/info/ + query string
335 foreach my $lose2 ('/foo/bar/+?foo=','/foo/bar/+?:foo=','/foo/bar/+?@foo=','/foo/bar/+?:@foo=') {
336 my $foo = $dp->parse($lose2);
339 [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => '' }) ],
341 "${lose2} fails with no query"
345 [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => 'bar=baz' }) ],
347 "${lose2} fails with query missing foo key"
352 [ '/foo/bar/+?foo=' => 'FOO' ],
353 [ '/foo/bar/+?:foo=' => { foo => 'FOO' } ],
354 [ '/foo/bar/+?spoo~' => undef ],
355 [ '/foo/bar/+?:spoo~' => {} ],
356 [ '/foo/bar/+?@spoo~' => [] ],
357 [ '/foo/bar/+?:@spoo~' => { spoo => [] } ],
358 [ '/foo/bar/+?bar=' => 'BAR2' ],
359 [ '/foo/bar/+?:bar=' => { bar => 'BAR2' } ],
360 [ '/foo/bar/+?@bar=' => [ qw(BAR1 BAR2) ] ],
361 [ '/foo/bar/+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
362 [ '/foo/bar/+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
363 [ '/foo/bar/+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
364 [ '/foo/bar/+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
365 [ '/foo/bar/+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
366 [ '/foo/bar/+?*' => \%all_single ],
367 [ '/foo/bar/+?@*' => \%all_multi ],
368 [ '/foo/bar/+?foo=&@*' => 'FOO', do { my %h = %all_multi; delete $h{foo}; \%h } ],
369 [ '/foo/bar/+?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
370 [ '/foo/bar/+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
372 my ($spec, @res) = @$win2;
373 my $match = $dp->parse($spec);
374 # use Data::Dump::Streamer; warn Dump($match);
376 [ $match->({ PATH_INFO => '/foo/bar/', QUERY_STRING => $q }) ],
378 "${spec} matches correctly"
383 # /path/info + query string
386 foreach my $lose3 ('/foo/bar+?foo=','/foo/bar+?:foo=','/foo/bar+?@foo=','/foo/bar+?:@foo=') {
387 my $foo = $dp->parse($lose3);
390 [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => '' }) ],
392 "${lose3} fails with no query"
396 [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => 'bar=baz' }) ],
398 "${lose3} fails with query missing foo key"
403 [ '/foo/bar+?foo=' => 'FOO' ],
404 [ '/foo/bar+?:foo=' => { foo => 'FOO' } ],
405 [ '/foo/bar+?spoo~' => undef ],
406 [ '/foo/bar+?:spoo~' => {} ],
407 [ '/foo/bar+?@spoo~' => [] ],
408 [ '/foo/bar+?:@spoo~' => { spoo => [] } ],
409 [ '/foo/bar+?bar=' => 'BAR2' ],
410 [ '/foo/bar+?:bar=' => { bar => 'BAR2' } ],
411 [ '/foo/bar+?@bar=' => [ qw(BAR1 BAR2) ] ],
412 [ '/foo/bar+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
413 [ '/foo/bar+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
414 [ '/foo/bar+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
415 [ '/foo/bar+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
416 [ '/foo/bar+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
417 [ '/foo/bar+?*' => \%all_single ],
418 [ '/foo/bar+?@*' => \%all_multi ],
419 [ '/foo/bar+?foo=&@*' => 'FOO', do { my %h = %all_multi; delete $h{foo}; \%h } ],
420 [ '/foo/bar+?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
421 [ '/foo/bar+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
423 my ($spec, @res) = @$win3;
424 my $match = $dp->parse($spec);
425 # use Data::Dump::Streamer; warn Dump($match);
427 [ $match->({ PATH_INFO => '/foo/bar', QUERY_STRING => $q }) ],
429 "${spec} matches correctly"