basic named path part matching
[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{
217 local $@;
218 ok(
d63bcdae 219 !eval { $dp->parse('/foo+(GET'); 1 },
a4ec359d 220 'Death with missing closing )'
221 );
222 my $err = q{
223 /foo+(GET
224 ^
225 };
226 (s/^\n//s,s/\n $//s,s/^ //mg) for $err;
227 like(
228 $@,
229 qr{\Q$err\E},
230 "Error $@ matches\n${err}\n"
231 );
232}
2ee4ab06 233
051b7ee2 234{
6cf1d73a 235 my $not = $dp->parse('!.html+.*');
236
237 is_deeply(
238 [ $not->({ PATH_INFO => '/foo.xml' }) ],
239 [ {}, 'xml' ],
240 '!.html+.* matches /foo.xml'
241 );
242
243 is_deeply(
244 [ $not->({ PATH_INFO => '/foo.html' }) ],
245 [],
246 '!.html+.* does not match /foo.html'
247 );
248
249 is_deeply(
250 [ $not->({ PATH_INFO => '/foo' }) ],
251 [],
252 '!.html+.* does not match /foo'
253 );
c2c68a87 254}
255
256{
257 my $ext = $dp->parse('/foo.bar');
258
259 is_deeply(
260 [ $ext->({ PATH_INFO => '/foo.bar' }) ],
261 [ {} ],
262 '/foo.bar matches /foo.bar'
263 );
264
265 is_deeply(
266 [ $ext->({ PATH_INFO => '/foo.bar.ext' }) ],
267 [ {} ],
268 '/foo.bar matches /foo.bar.ext'
269 );
270
271 is_deeply(
272 [ $ext->({ PATH_INFO => '/foo.notbar' }) ],
273 [],
274 '/foo.bar does not match /foo.notbar'
275 );
051b7ee2 276}
da8429c9 277
051b7ee2 278{
6cf1d73a 279 my $sub = $dp->parse('/foo/*/...');
280
281 is_deeply(
282 [ $sub->({ PATH_INFO => '/foo/1/bar' }) ],
283 [ { PATH_INFO => '/bar', SCRIPT_NAME => '/foo/1' }, 1 ],
284 '/foo/*/... matches /foo/1/bar and strips to /bar'
285 );
286
287 is_deeply(
288 [ $sub->({ PATH_INFO => '/foo/1/' }) ],
289 [ { PATH_INFO => '/', SCRIPT_NAME => '/foo/1' }, 1 ],
290 '/foo/*/... matches /foo/1/bar and strips to /'
291 );
292
293 is_deeply(
294 [ $sub->({ PATH_INFO => '/foo/1' }) ],
295 [],
296 '/foo/*/... does not match /foo/1 (no trailing /)'
297 );
051b7ee2 298}
a5917caa 299
15e679c1 300{
4fe7d49f 301 my $sub = $dp->parse('/foo/**/belief');
302 my $match = 'barred/beyond';
303 is_deeply(
304 [ $sub->({ PATH_INFO => "/foo/${match}/belief" }) ],
305 [ {}, $match ],
306 "/foo/**/belief matches /foo/${match}/belief"
307 );
308}
309
310{
a96dd5cb 311 my $match = '~';
312 my $sub = $dp->parse($match);
313
314 is_deeply(
315 [ $sub->({ PATH_INFO => '/foo' }) ],
316 [],
317 "$match does not match /foo"
318 );
319
320 is_deeply(
321 [ $sub->({ PATH_INFO => '' }) ],
322 [ {} ],
323 "$match matches empty path with empty env"
324 );
325}
326
327{
8c51c01a 328 my $match = '/foo...';
329 my $sub = $dp->parse($match);
330
331 is_deeply(
bb0dbe7c 332 [ $sub->({ PATH_INFO => '/foobar' }) ],
333 [],
334 "$match does not match /foobar"
335 );
336
337 is_deeply(
8c51c01a 338 [ $sub->({ PATH_INFO => '/foo/bar' }) ],
339 [ { PATH_INFO => '/bar', SCRIPT_NAME => '/foo' } ],
340 "$match matches /foo/bar and strips to /bar"
341 );
342
343 is_deeply(
344 [ $sub->({ PATH_INFO => '/foo/' }) ],
345 [ { PATH_INFO => '/', SCRIPT_NAME => '/foo' } ],
346 "$match matches /foo/ and strips to /"
347 );
348
349 is_deeply(
350 [ $sub->({ PATH_INFO => '/foo' }) ],
351 [ { PATH_INFO => '', SCRIPT_NAME => '/foo' } ],
352 "$match matches /foo and strips to empty path"
353 );
354}
355
356{
15e679c1 357 my @dot_pairs = (
358 [ '/one/*' => 'two' ],
359 [ '/one/*.*' => 'two.three' ],
360 [ '/**' => 'one/two' ],
361 [ '/**.*' => 'one/two.three' ],
362 );
363
364 foreach my $p (@dot_pairs) {
365 is_deeply(
366 [ $dp->parse($p->[0])->({ PATH_INFO => '/one/two.three' }) ],
367 [ {}, $p->[1] ],
368 "${\$p->[0]} matches /one/two.three and returns ${\$p->[1]}"
369 );
370 }
371}
372
b83ac307 373{
374 my @named = (
375 [ '/foo/*:foo_id' => '/foo/1' => { foo_id => 1 } ],
376 [ '/foo/:foo_id' => '/foo/1' => { foo_id => 1 } ],
377 [ '/foo/:id/**:rest' => '/foo/id/rest/of/the/path.ext'
378 => { id => 'id', rest => 'rest/of/the/path' } ],
379 [ '/foo/:id/**.*:rest' => '/foo/id/rest/of/the/path.ext'
380 => { id => 'id', rest => 'rest/of/the/path.ext' } ],
381 );
382 foreach my $n (@named) {
383 is_deeply(
384 [ $dp->parse($n->[0])->({ PATH_INFO => $n->[1] }) ],
385 [ {}, $n->[2] ],
386 "${\$n->[0]} matches ${\$n->[1]} with correct captures"
387 );
388 }
389}
390
6c0f599a 391#
392# query string
393#
394
a5917caa 395my $q = 'foo=FOO&bar=BAR1&baz=one+two&quux=QUUX1&quux=QUUX2'
6cf1d73a 396 .'&bar=BAR2&quux=QUUX3&evil=%2F';
a5917caa 397
398my %all_single = (
6cf1d73a 399 foo => 'FOO',
400 bar => 'BAR2',
401 baz => 'one two',
402 quux => 'QUUX3',
403 evil => '/',
a5917caa 404);
405
406my %all_multi = (
6cf1d73a 407 foo => [ 'FOO' ],
408 bar => [ qw(BAR1 BAR2) ],
409 baz => [ 'one two' ],
410 quux => [ qw(QUUX1 QUUX2 QUUX3) ],
411 evil => [ '/' ],
a5917caa 412);
413
eb9e0e25 414foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') {
6cf1d73a 415 my $foo = $dp->parse($lose);
6c0f599a 416
6cf1d73a 417 is_deeply(
418 [ $foo->({ QUERY_STRING => '' }) ],
419 [],
420 "${lose} fails with no query"
421 );
6c0f599a 422
6cf1d73a 423 is_deeply(
424 [ $foo->({ QUERY_STRING => 'bar=baz' }) ],
425 [],
426 "${lose} fails with query missing foo key"
427 );
6c0f599a 428}
a5917caa 429
6c0f599a 430foreach my $win (
6cf1d73a 431 [ '?foo=' => 'FOO' ],
432 [ '?:foo=' => { foo => 'FOO' } ],
433 [ '?spoo~' => undef ],
434 [ '?:spoo~' => {} ],
435 [ '?@spoo~' => [] ],
436 [ '?:@spoo~' => { spoo => [] } ],
437 [ '?bar=' => 'BAR2' ],
438 [ '?:bar=' => { bar => 'BAR2' } ],
439 [ '?@bar=' => [ qw(BAR1 BAR2) ] ],
440 [ '?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
441 [ '?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
442 [ '?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
443 [ '?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
444 [ '?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
445 [ '?*' => \%all_single ],
446 [ '?@*' => \%all_multi ],
447 [ '?foo=&@*' => 'FOO', \%all_multi ],
448 [ '?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
449 [ '?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
6c0f599a 450) {
6cf1d73a 451 my ($spec, @res) = @$win;
452 my $match = $dp->parse($spec);
453 #use Data::Dump::Streamer; warn Dump($match);
454 is_deeply(
455 [ $match->({ QUERY_STRING => $q }) ],
456 [ {}, @res ],
457 "${spec} matches correctly"
458 );
6c0f599a 459}
eb9e0e25 460
6c0f599a 461#
462# /path/info/ + query string
463#
464
465foreach my $lose2 ('/foo/bar/+?foo=','/foo/bar/+?:foo=','/foo/bar/+?@foo=','/foo/bar/+?:@foo=') {
6cf1d73a 466 my $foo = $dp->parse($lose2);
6c0f599a 467
6cf1d73a 468 is_deeply(
469 [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => '' }) ],
470 [ ],
471 "${lose2} fails with no query"
472 );
6c0f599a 473
6cf1d73a 474 is_deeply(
475 [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => 'bar=baz' }) ],
476 [ ],
477 "${lose2} fails with query missing foo key"
478 );
eb9e0e25 479}
a5917caa 480
6c0f599a 481foreach my $win2 (
6cf1d73a 482 [ '/foo/bar/+?foo=' => 'FOO' ],
483 [ '/foo/bar/+?:foo=' => { foo => 'FOO' } ],
484 [ '/foo/bar/+?spoo~' => undef ],
485 [ '/foo/bar/+?:spoo~' => {} ],
486 [ '/foo/bar/+?@spoo~' => [] ],
487 [ '/foo/bar/+?:@spoo~' => { spoo => [] } ],
488 [ '/foo/bar/+?bar=' => 'BAR2' ],
489 [ '/foo/bar/+?:bar=' => { bar => 'BAR2' } ],
490 [ '/foo/bar/+?@bar=' => [ qw(BAR1 BAR2) ] ],
491 [ '/foo/bar/+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
492 [ '/foo/bar/+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
493 [ '/foo/bar/+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
494 [ '/foo/bar/+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
495 [ '/foo/bar/+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
496 [ '/foo/bar/+?*' => \%all_single ],
497 [ '/foo/bar/+?@*' => \%all_multi ],
498 [ '/foo/bar/+?foo=&@*' => 'FOO', \%all_multi ],
499 [ '/foo/bar/+?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
500 [ '/foo/bar/+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
a5917caa 501) {
6cf1d73a 502 my ($spec, @res) = @$win2;
503 my $match = $dp->parse($spec);
504 # use Data::Dump::Streamer; warn Dump($match);
505 is_deeply(
506 [ $match->({ PATH_INFO => '/foo/bar/', QUERY_STRING => $q }) ],
507 [ {}, @res ],
508 "${spec} matches correctly"
509 );
6c0f599a 510}
511
512#
513# /path/info + query string
514#
515
516foreach my $lose3 ('/foo/bar+?foo=','/foo/bar+?:foo=','/foo/bar+?@foo=','/foo/bar+?:@foo=') {
6cf1d73a 517 my $foo = $dp->parse($lose3);
6c0f599a 518
6cf1d73a 519 is_deeply(
520 [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => '' }) ],
521 [ ],
522 "${lose3} fails with no query"
523 );
6c0f599a 524
6cf1d73a 525 is_deeply(
526 [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => 'bar=baz' }) ],
527 [ ],
528 "${lose3} fails with query missing foo key"
529 );
6c0f599a 530}
531
532foreach my $win3 (
6cf1d73a 533 [ '/foo/bar+?foo=' => 'FOO' ],
534 [ '/foo/bar+?:foo=' => { foo => 'FOO' } ],
535 [ '/foo/bar+?spoo~' => undef ],
536 [ '/foo/bar+?:spoo~' => {} ],
537 [ '/foo/bar+?@spoo~' => [] ],
538 [ '/foo/bar+?:@spoo~' => { spoo => [] } ],
539 [ '/foo/bar+?bar=' => 'BAR2' ],
540 [ '/foo/bar+?:bar=' => { bar => 'BAR2' } ],
541 [ '/foo/bar+?@bar=' => [ qw(BAR1 BAR2) ] ],
542 [ '/foo/bar+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
543 [ '/foo/bar+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
544 [ '/foo/bar+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
545 [ '/foo/bar+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
546 [ '/foo/bar+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
547 [ '/foo/bar+?*' => \%all_single ],
548 [ '/foo/bar+?@*' => \%all_multi ],
549 [ '/foo/bar+?foo=&@*' => 'FOO', \%all_multi ],
550 [ '/foo/bar+?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
551 [ '/foo/bar+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
6c0f599a 552) {
6cf1d73a 553 my ($spec, @res) = @$win3;
554 my $match = $dp->parse($spec);
555 # use Data::Dump::Streamer; warn Dump($match);
556 is_deeply(
557 [ $match->({ PATH_INFO => '/foo/bar', QUERY_STRING => $q }) ],
558 [ {}, @res ],
559 "${spec} matches correctly"
560 );
a5917caa 561}