Test that (GET+/foo)|(POST+/foo) works
[catagits/Web-Simple.git] / t / dispatch_parser.t
CommitLineData
920d6222 1use strict;
2use warnings FATAL => 'all';
3
4use Test::More qw(no_plan);
5
d63bcdae 6use Web::Dispatch::Parser;
920d6222 7
d63bcdae 8my $dp = Web::Dispatch::Parser->new;
920d6222 9
59ccc1e8 10{
6cf1d73a 11 my $all = $dp->parse('');
12
13 is_deeply(
14 [ $all->({ REQUEST_METHOD => 'GET' }) ],
15 [ {} ],
16 'GET matches'
17 );
18
19 is_deeply(
20 [ $all->({ REQUEST_METHOD => 'POST' }) ],
21 [ {} ],
22 'POST matches'
23 );
aaa598e6 24};
25
051b7ee2 26{
6cf1d73a 27 my $get = $dp->parse('GET');
28
29 is_deeply(
30 [ $get->({ REQUEST_METHOD => 'GET' }) ],
31 [ {} ],
32 'GET matches'
33 );
34
35 is_deeply(
36 [ $get->({ REQUEST_METHOD => 'POST' }) ],
37 [],
38 'POST does not match'
39 );
051b7ee2 40}
920d6222 41
051b7ee2 42{
6cf1d73a 43 my $html = $dp->parse('.html');
44
45 is_deeply(
46 [ $html->({ PATH_INFO => '/foo/bar.html' }) ],
47 [ { } ],
48 '.html matches'
49 );
50
51 is_deeply(
52 [ $html->({ PATH_INFO => '/foo/bar.xml' }) ],
53 [],
54 '.xml does not match .html'
55 );
c6ea9542 56}
57
051b7ee2 58{
6cf1d73a 59 my $any_ext = $dp->parse('.*');
60
61 is_deeply(
62 [ $any_ext->({ PATH_INFO => '/foo/bar.html' }) ],
63 [ { }, 'html' ],
64 '.html matches .* and extension returned'
65 );
66
67 is_deeply(
68 [ $any_ext->({ PATH_INFO => '/foo/bar' }) ],
69 [],
70 'no extension does not match .*'
71 );
051b7ee2 72}
da9b9236 73
051b7ee2 74{
6cf1d73a 75 my $slash = $dp->parse('/');
76
77 is_deeply(
78 [ $slash->({ PATH_INFO => '/' }) ],
79 [ {} ],
80 '/ matches /'
81 );
82
83 is_deeply(
84 [ $slash->({ PATH_INFO => '/foo' }) ],
85 [ ],
86 '/foo does not match /'
87 );
051b7ee2 88}
da9b9236 89
051b7ee2 90{
6cf1d73a 91 my $post = $dp->parse('/post/*');
92
93 is_deeply(
94 [ $post->({ PATH_INFO => '/post/one' }) ],
95 [ {}, 'one' ],
96 '/post/one parses out one'
97 );
98
99 is_deeply(
100 [ $post->({ PATH_INFO => '/post/one/' }) ],
101 [],
102 '/post/one/ does not match'
103 );
104
105 is_deeply(
106 [ $post->({ PATH_INFO => '/post/one.html' }) ],
107 [ {}, 'one' ],
108 '/post/one.html still parses out one'
109 );
da9b9236 110}
111
051b7ee2 112{
6cf1d73a 113 my $post = $dp->parse('/foo-bar/*');
114
115 is_deeply(
116 [ $post->({ PATH_INFO => '/foo-bar/one' }) ],
117 [ {}, 'one' ],
118 '/foo-bar/one parses out one'
119 );
120
121 is_deeply(
122 [ $post->({ PATH_INFO => '/foo-bar/one/' }) ],
123 [],
124 '/foo-bar/one/ does not match'
125 );
1c4f4b78 126}
127
128{
6cf1d73a 129 my $combi = $dp->parse('GET+/post/*');
130
131 is_deeply(
132 [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'GET' }) ],
133 [ {}, 'one' ],
134 '/post/one parses out one'
135 );
136
137 is_deeply(
138 [ $combi->({ PATH_INFO => '/post/one/', REQUEST_METHOD => 'GET' }) ],
139 [],
140 '/post/one/ does not match'
141 );
142
143 is_deeply(
144 [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'POST' }) ],
145 [],
146 'POST /post/one does not match'
147 );
051b7ee2 148}
b0420ad6 149
051b7ee2 150{
6cf1d73a 151 my $or = $dp->parse('GET|POST');
152
153 foreach my $meth (qw(GET POST)) {
154
155 is_deeply(
156 [ $or->({ REQUEST_METHOD => $meth }) ],
157 [ {} ],
158 'GET|POST matches method '.$meth
159 );
160 }
161
162 is_deeply(
163 [ $or->({ REQUEST_METHOD => 'PUT' }) ],
164 [],
165 'GET|POST does not match PUT'
166 );
051b7ee2 167}
b0420ad6 168
051b7ee2 169{
6cf1d73a 170 my $or = $dp->parse('GET|POST|DELETE');
171
172 foreach my $meth (qw(GET POST DELETE)) {
173
174 is_deeply(
175 [ $or->({ REQUEST_METHOD => $meth }) ],
176 [ {} ],
177 'GET|POST|DELETE matches method '.$meth
178 );
179 }
180
181 is_deeply(
182 [ $or->({ REQUEST_METHOD => 'PUT' }) ],
183 [],
184 'GET|POST|DELETE does not match PUT'
185 );
051b7ee2 186}
b0420ad6 187
051b7ee2 188{
6cf1d73a 189 my $nest = $dp->parse('(GET+/foo)|POST');
190
191 is_deeply(
192 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'GET' }) ],
193 [ {} ],
194 '(GET+/foo)|POST matches GET /foo'
195 );
196
197 is_deeply(
198 [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'GET' }) ],
199 [],
200 '(GET+/foo)|POST does not match GET /bar'
201 );
202
203 is_deeply(
204 [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'POST' }) ],
205 [ {} ],
206 '(GET+/foo)|POST matches POST /bar'
207 );
208
209 is_deeply(
210 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'PUT' }) ],
211 [],
212 '(GET+/foo)|POST does not match PUT /foo'
213 );
051b7ee2 214}
a4ec359d 215
216{
965732b4 217 my $spec = '(GET+/foo)|(POST+/foo)';
218 my $nest = $dp->parse($spec);
219
220 for my $method (qw( GET POST )) {
221 is_deeply(
222 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => $method }) ],
223 [ {} ],
224 "$spec matches $method /foo"
225 );
226 is_deeply(
227 [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => $method }) ],
228 [],
229 "$spec does not match $method /bar"
230 );
231 }
232
233 is_deeply(
234 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'PUT' }) ],
235 [],
236 "$spec does not match PUT /foo"
237 );
238}
239
240{
a4ec359d 241 local $@;
242 ok(
d63bcdae 243 !eval { $dp->parse('/foo+(GET'); 1 },
a4ec359d 244 'Death with missing closing )'
245 );
246 my $err = q{
247 /foo+(GET
248 ^
249 };
250 (s/^\n//s,s/\n $//s,s/^ //mg) for $err;
251 like(
252 $@,
253 qr{\Q$err\E},
254 "Error $@ matches\n${err}\n"
255 );
256}
2ee4ab06 257
051b7ee2 258{
6cf1d73a 259 my $not = $dp->parse('!.html+.*');
260
261 is_deeply(
262 [ $not->({ PATH_INFO => '/foo.xml' }) ],
263 [ {}, 'xml' ],
264 '!.html+.* matches /foo.xml'
265 );
266
267 is_deeply(
268 [ $not->({ PATH_INFO => '/foo.html' }) ],
269 [],
270 '!.html+.* does not match /foo.html'
271 );
272
273 is_deeply(
274 [ $not->({ PATH_INFO => '/foo' }) ],
275 [],
276 '!.html+.* does not match /foo'
277 );
c2c68a87 278}
279
280{
281 my $ext = $dp->parse('/foo.bar');
282
283 is_deeply(
284 [ $ext->({ PATH_INFO => '/foo.bar' }) ],
285 [ {} ],
286 '/foo.bar matches /foo.bar'
287 );
288
289 is_deeply(
290 [ $ext->({ PATH_INFO => '/foo.bar.ext' }) ],
291 [ {} ],
292 '/foo.bar matches /foo.bar.ext'
293 );
294
295 is_deeply(
296 [ $ext->({ PATH_INFO => '/foo.notbar' }) ],
297 [],
298 '/foo.bar does not match /foo.notbar'
299 );
051b7ee2 300}
da8429c9 301
051b7ee2 302{
6cf1d73a 303 my $sub = $dp->parse('/foo/*/...');
304
305 is_deeply(
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'
309 );
310
311 is_deeply(
312 [ $sub->({ PATH_INFO => '/foo/1/' }) ],
313 [ { PATH_INFO => '/', SCRIPT_NAME => '/foo/1' }, 1 ],
314 '/foo/*/... matches /foo/1/bar and strips to /'
315 );
316
317 is_deeply(
318 [ $sub->({ PATH_INFO => '/foo/1' }) ],
319 [],
320 '/foo/*/... does not match /foo/1 (no trailing /)'
321 );
051b7ee2 322}
a5917caa 323
15e679c1 324{
4fe7d49f 325 my $sub = $dp->parse('/foo/**/belief');
326 my $match = 'barred/beyond';
327 is_deeply(
328 [ $sub->({ PATH_INFO => "/foo/${match}/belief" }) ],
329 [ {}, $match ],
330 "/foo/**/belief matches /foo/${match}/belief"
331 );
332}
333
334{
a96dd5cb 335 my $match = '~';
336 my $sub = $dp->parse($match);
337
338 is_deeply(
339 [ $sub->({ PATH_INFO => '/foo' }) ],
340 [],
341 "$match does not match /foo"
342 );
343
344 is_deeply(
345 [ $sub->({ PATH_INFO => '' }) ],
346 [ {} ],
347 "$match matches empty path with empty env"
348 );
349}
350
351{
8c51c01a 352 my $match = '/foo...';
353 my $sub = $dp->parse($match);
354
355 is_deeply(
bb0dbe7c 356 [ $sub->({ PATH_INFO => '/foobar' }) ],
357 [],
358 "$match does not match /foobar"
359 );
360
361 is_deeply(
8c51c01a 362 [ $sub->({ PATH_INFO => '/foo/bar' }) ],
363 [ { PATH_INFO => '/bar', SCRIPT_NAME => '/foo' } ],
364 "$match matches /foo/bar and strips to /bar"
365 );
366
367 is_deeply(
368 [ $sub->({ PATH_INFO => '/foo/' }) ],
369 [ { PATH_INFO => '/', SCRIPT_NAME => '/foo' } ],
370 "$match matches /foo/ and strips to /"
371 );
372
373 is_deeply(
374 [ $sub->({ PATH_INFO => '/foo' }) ],
375 [ { PATH_INFO => '', SCRIPT_NAME => '/foo' } ],
376 "$match matches /foo and strips to empty path"
377 );
378}
379
380{
15e679c1 381 my @dot_pairs = (
382 [ '/one/*' => 'two' ],
383 [ '/one/*.*' => 'two.three' ],
384 [ '/**' => 'one/two' ],
385 [ '/**.*' => 'one/two.three' ],
386 );
387
388 foreach my $p (@dot_pairs) {
389 is_deeply(
390 [ $dp->parse($p->[0])->({ PATH_INFO => '/one/two.three' }) ],
391 [ {}, $p->[1] ],
392 "${\$p->[0]} matches /one/two.three and returns ${\$p->[1]}"
393 );
394 }
395}
396
b83ac307 397{
398 my @named = (
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' } ],
405 );
406 foreach my $n (@named) {
407 is_deeply(
408 [ $dp->parse($n->[0])->({ PATH_INFO => $n->[1] }) ],
409 [ {}, $n->[2] ],
410 "${\$n->[0]} matches ${\$n->[1]} with correct captures"
411 );
412 }
413}
414
6c0f599a 415#
416# query string
417#
418
a5917caa 419my $q = 'foo=FOO&bar=BAR1&baz=one+two&quux=QUUX1&quux=QUUX2'
0cddee76 420 .'&foo.bar=FOOBAR1&foo.bar=FOOBAR2&foo.baz=FOOBAZ'
6cf1d73a 421 .'&bar=BAR2&quux=QUUX3&evil=%2F';
a5917caa 422
423my %all_single = (
6cf1d73a 424 foo => 'FOO',
425 bar => 'BAR2',
426 baz => 'one two',
427 quux => 'QUUX3',
428 evil => '/',
0cddee76 429 'foo.baz' => 'FOOBAZ',
430 'foo.bar' => 'FOOBAR2',
a5917caa 431);
432
433my %all_multi = (
6cf1d73a 434 foo => [ 'FOO' ],
435 bar => [ qw(BAR1 BAR2) ],
436 baz => [ 'one two' ],
437 quux => [ qw(QUUX1 QUUX2 QUUX3) ],
438 evil => [ '/' ],
0cddee76 439 'foo.baz' => [ 'FOOBAZ' ],
440 'foo.bar' => [ qw(FOOBAR1 FOOBAR2) ],
a5917caa 441);
442
eb9e0e25 443foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') {
6cf1d73a 444 my $foo = $dp->parse($lose);
6c0f599a 445
6cf1d73a 446 is_deeply(
447 [ $foo->({ QUERY_STRING => '' }) ],
448 [],
449 "${lose} fails with no query"
450 );
6c0f599a 451
6cf1d73a 452 is_deeply(
453 [ $foo->({ QUERY_STRING => 'bar=baz' }) ],
454 [],
455 "${lose} fails with query missing foo key"
456 );
6c0f599a 457}
a5917caa 458
6c0f599a 459foreach my $win (
6cf1d73a 460 [ '?foo=' => 'FOO' ],
461 [ '?:foo=' => { foo => 'FOO' } ],
462 [ '?spoo~' => undef ],
463 [ '?:spoo~' => {} ],
464 [ '?@spoo~' => [] ],
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) ] } ],
0cddee76 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) ] } ],
6c0f599a 485) {
6cf1d73a 486 my ($spec, @res) = @$win;
487 my $match = $dp->parse($spec);
488 #use Data::Dump::Streamer; warn Dump($match);
489 is_deeply(
490 [ $match->({ QUERY_STRING => $q }) ],
491 [ {}, @res ],
492 "${spec} matches correctly"
493 );
6c0f599a 494}
eb9e0e25 495
6c0f599a 496#
497# /path/info/ + query string
498#
499
500foreach my $lose2 ('/foo/bar/+?foo=','/foo/bar/+?:foo=','/foo/bar/+?@foo=','/foo/bar/+?:@foo=') {
6cf1d73a 501 my $foo = $dp->parse($lose2);
6c0f599a 502
6cf1d73a 503 is_deeply(
504 [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => '' }) ],
505 [ ],
506 "${lose2} fails with no query"
507 );
6c0f599a 508
6cf1d73a 509 is_deeply(
510 [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => 'bar=baz' }) ],
511 [ ],
512 "${lose2} fails with query missing foo key"
513 );
eb9e0e25 514}
a5917caa 515
6c0f599a 516foreach my $win2 (
6cf1d73a 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) ] } ],
0cddee76 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) ] } ],
a5917caa 542) {
6cf1d73a 543 my ($spec, @res) = @$win2;
544 my $match = $dp->parse($spec);
545 # use Data::Dump::Streamer; warn Dump($match);
546 is_deeply(
547 [ $match->({ PATH_INFO => '/foo/bar/', QUERY_STRING => $q }) ],
548 [ {}, @res ],
549 "${spec} matches correctly"
550 );
6c0f599a 551}
552
553#
554# /path/info + query string
555#
556
557foreach my $lose3 ('/foo/bar+?foo=','/foo/bar+?:foo=','/foo/bar+?@foo=','/foo/bar+?:@foo=') {
6cf1d73a 558 my $foo = $dp->parse($lose3);
6c0f599a 559
6cf1d73a 560 is_deeply(
561 [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => '' }) ],
562 [ ],
563 "${lose3} fails with no query"
564 );
6c0f599a 565
6cf1d73a 566 is_deeply(
567 [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => 'bar=baz' }) ],
568 [ ],
569 "${lose3} fails with query missing foo key"
570 );
6c0f599a 571}
572
573foreach my $win3 (
6cf1d73a 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) ] } ],
0cddee76 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) ] } ],
6c0f599a 599) {
6cf1d73a 600 my ($spec, @res) = @$win3;
601 my $match = $dp->parse($spec);
602 # use Data::Dump::Streamer; warn Dump($match);
603 is_deeply(
604 [ $match->({ PATH_INFO => '/foo/bar', QUERY_STRING => $q }) ],
605 [ {}, @res ],
606 "${spec} matches correctly"
607 );
a5917caa 608}