2 use warnings FATAL => 'all';
4 use Test::More qw(no_plan);
6 use Web::Simple::DispatchParser;
8 my $dp = Web::Simple::DispatchParser->new;
11 my $get = $dp->parse_dispatch_specification('GET');
14 [ $get->({ REQUEST_METHOD => 'GET' }) ],
20 [ $get->({ REQUEST_METHOD => 'POST' }) ],
27 !eval { $dp->parse_dispatch_specification('GET POST'); 1; },
28 "Don't yet allow two methods"
32 my $html = $dp->parse_dispatch_specification('.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_dispatch_specification('.*');
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_dispatch_specification('/');
67 [ $slash->({ PATH_INFO => '/' }) ],
73 [ $slash->({ PATH_INFO => '/foo' }) ],
75 '/foo does not match /'
80 my $post = $dp->parse_dispatch_specification('/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 $combi = $dp->parse_dispatch_specification('GET+/post/*');
99 [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'GET' }) ],
101 '/post/one parses out one'
105 [ $combi->({ PATH_INFO => '/post/one/', REQUEST_METHOD => 'GET' }) ],
107 '/post/one/ does not match'
111 [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'POST' }) ],
113 'POST /post/one does not match'
118 my $or = $dp->parse_dispatch_specification('GET|POST');
120 foreach my $meth (qw(GET POST)) {
123 [ $or->({ REQUEST_METHOD => $meth }) ],
125 'GET|POST matches method '.$meth
130 [ $or->({ REQUEST_METHOD => 'PUT' }) ],
132 'GET|POST does not match PUT'
137 my $or = $dp->parse_dispatch_specification('GET|POST|DELETE');
139 foreach my $meth (qw(GET POST DELETE)) {
142 [ $or->({ REQUEST_METHOD => $meth }) ],
144 'GET|POST|DELETE matches method '.$meth
149 [ $or->({ REQUEST_METHOD => 'PUT' }) ],
151 'GET|POST|DELETE does not match PUT'
156 my $nest = $dp->parse_dispatch_specification('(GET+/foo)|POST');
159 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'GET' }) ],
161 '(GET+/foo)|POST matches GET /foo'
165 [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'GET' }) ],
167 '(GET+/foo)|POST does not match GET /bar'
171 [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'POST' }) ],
173 '(GET+/foo)|POST matches POST /bar'
177 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'PUT' }) ],
179 '(GET+/foo)|POST does not match PUT /foo'
186 !eval { $dp->parse_dispatch_specification('/foo+(GET'); 1 },
187 'Death with missing closing )'
193 (s/^\n//s,s/\n $//s,s/^ //mg) for $err;
197 "Error $@ matches\n${err}\n"
202 my $not = $dp->parse_dispatch_specification('!.html+.*');
205 [ $not->({ PATH_INFO => '/foo.xml' }) ],
206 [ { PATH_INFO => '/foo' }, 'xml' ],
207 '!.html+.* matches /foo.xml'
211 [ $not->({ PATH_INFO => '/foo.html' }) ],
213 '!.html+.* does not match /foo.html'
217 [ $not->({ PATH_INFO => '/foo' }) ],
219 '!.html+.* does not match /foo'
224 my $sub = $dp->parse_dispatch_specification('/foo/*/...');
227 [ $sub->({ PATH_INFO => '/foo/1/bar' }) ],
228 [ { PATH_INFO => '/bar' }, 1 ],
229 '/foo/*/... matches /foo/1/bar and strips to /bar'
233 [ $sub->({ PATH_INFO => '/foo/1/' }) ],
234 [ { PATH_INFO => '/' }, 1 ],
235 '/foo/*/... matches /foo/1/bar and strips to /'
239 [ $sub->({ PATH_INFO => '/foo/1' }) ],
241 '/foo/*/... does not match /foo/1 (no trailing /)'
249 my $q = 'foo=FOO&bar=BAR1&baz=one+two&quux=QUUX1&quux=QUUX2'
250 .'&bar=BAR2&quux=QUUX3&evil=%2F';
262 bar => [ qw(BAR1 BAR2) ],
263 baz => [ 'one two' ],
264 quux => [ qw(QUUX1 QUUX2 QUUX3) ],
268 foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') {
269 my $foo = $dp->parse_dispatch_specification($lose);
272 [ $foo->({ QUERY_STRING => '' }) ],
274 "${lose} fails with no query"
278 [ $foo->({ QUERY_STRING => 'bar=baz' }) ],
280 "${lose} fails with query missing foo key"
285 [ '?foo=' => 'FOO' ],
286 [ '?:foo=' => { foo => 'FOO' } ],
287 [ '?spoo~' => undef ],
290 [ '?:@spoo~' => { spoo => [] } ],
291 [ '?bar=' => 'BAR2' ],
292 [ '?:bar=' => { bar => 'BAR2' } ],
293 [ '?@bar=' => [ qw(BAR1 BAR2) ] ],
294 [ '?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
295 [ '?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
296 [ '?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
297 [ '?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
298 [ '?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
299 [ '?*' => \%all_single ],
300 [ '?@*' => \%all_multi ],
301 [ '?foo=&@*' => 'FOO', do { my %h = %all_multi; delete $h{foo}; \%h } ],
302 [ '?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
303 [ '?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
305 my ($spec, @res) = @$win;
306 my $match = $dp->parse_dispatch_specification($spec);
307 #use Data::Dump::Streamer; warn Dump($match);
309 [ $match->({ QUERY_STRING => $q }) ],
311 "${spec} matches correctly"
316 # /path/info/ + query string
319 foreach my $lose2 ('/foo/bar/+?foo=','/foo/bar/+?:foo=','/foo/bar/+?@foo=','/foo/bar/+?:@foo=') {
320 my $foo = $dp->parse_dispatch_specification($lose2);
323 [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => '' }) ],
325 "${lose2} fails with no query"
329 [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => 'bar=baz' }) ],
331 "${lose2} fails with query missing foo key"
336 [ '/foo/bar/+?foo=' => 'FOO' ],
337 [ '/foo/bar/+?:foo=' => { foo => 'FOO' } ],
338 [ '/foo/bar/+?spoo~' => undef ],
339 [ '/foo/bar/+?:spoo~' => {} ],
340 [ '/foo/bar/+?@spoo~' => [] ],
341 [ '/foo/bar/+?:@spoo~' => { spoo => [] } ],
342 [ '/foo/bar/+?bar=' => 'BAR2' ],
343 [ '/foo/bar/+?:bar=' => { bar => 'BAR2' } ],
344 [ '/foo/bar/+?@bar=' => [ qw(BAR1 BAR2) ] ],
345 [ '/foo/bar/+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
346 [ '/foo/bar/+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
347 [ '/foo/bar/+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
348 [ '/foo/bar/+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
349 [ '/foo/bar/+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
350 [ '/foo/bar/+?*' => \%all_single ],
351 [ '/foo/bar/+?@*' => \%all_multi ],
352 [ '/foo/bar/+?foo=&@*' => 'FOO', do { my %h = %all_multi; delete $h{foo}; \%h } ],
353 [ '/foo/bar/+?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
354 [ '/foo/bar/+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
356 my ($spec, @res) = @$win2;
357 my $match = $dp->parse_dispatch_specification($spec);
358 # use Data::Dump::Streamer; warn Dump($match);
360 [ $match->({ PATH_INFO => '/foo/bar/', QUERY_STRING => $q }) ],
362 "${spec} matches correctly"
367 # /path/info + query string
370 foreach my $lose3 ('/foo/bar+?foo=','/foo/bar+?:foo=','/foo/bar+?@foo=','/foo/bar+?:@foo=') {
371 my $foo = $dp->parse_dispatch_specification($lose3);
374 [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => '' }) ],
376 "${lose3} fails with no query"
380 [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => 'bar=baz' }) ],
382 "${lose3} fails with query missing foo key"
387 [ '/foo/bar+?foo=' => 'FOO' ],
388 [ '/foo/bar+?:foo=' => { foo => 'FOO' } ],
389 [ '/foo/bar+?spoo~' => undef ],
390 [ '/foo/bar+?:spoo~' => {} ],
391 [ '/foo/bar+?@spoo~' => [] ],
392 [ '/foo/bar+?:@spoo~' => { spoo => [] } ],
393 [ '/foo/bar+?bar=' => 'BAR2' ],
394 [ '/foo/bar+?:bar=' => { bar => 'BAR2' } ],
395 [ '/foo/bar+?@bar=' => [ qw(BAR1 BAR2) ] ],
396 [ '/foo/bar+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
397 [ '/foo/bar+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
398 [ '/foo/bar+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
399 [ '/foo/bar+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
400 [ '/foo/bar+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
401 [ '/foo/bar+?*' => \%all_single ],
402 [ '/foo/bar+?@*' => \%all_multi ],
403 [ '/foo/bar+?foo=&@*' => 'FOO', do { my %h = %all_multi; delete $h{foo}; \%h } ],
404 [ '/foo/bar+?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
405 [ '/foo/bar+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
407 my ($spec, @res) = @$win3;
408 my $match = $dp->parse_dispatch_specification($spec);
409 # use Data::Dump::Streamer; warn Dump($match);
411 [ $match->({ PATH_INFO => '/foo/bar', QUERY_STRING => $q }) ],
413 "${spec} matches correctly"