503a57052ad4269290d3aa68e3cfac570a1fa4e4
[catagits/Web-Simple.git] / t / dispatch_parser.t
1 use strict;
2 use warnings FATAL => 'all';
3
4 use Test::More qw(no_plan);
5
6 use Web::Dispatch::Parser;
7
8 my $dp = Web::Dispatch::Parser->new;
9
10 {
11    my $get = $dp->parse('GET');
12
13    is_deeply(
14      [ $get->({ REQUEST_METHOD => 'GET' }) ],
15      [ {} ],
16      'GET matches'
17    );
18
19    is_deeply(
20      [ $get->({ REQUEST_METHOD => 'POST' }) ],
21      [],
22      'POST does not match'
23    );
24 }
25
26 {
27    my $html = $dp->parse('.html');
28
29    is_deeply(
30      [ $html->({ PATH_INFO => '/foo/bar.html' }) ],
31      [ { } ],
32      '.html matches'
33    );
34
35    is_deeply(
36      [ $html->({ PATH_INFO => '/foo/bar.xml' }) ],
37      [],
38      '.xml does not match .html'
39    );
40 }
41
42 {
43    my $any_ext = $dp->parse('.*');
44
45    is_deeply(
46      [ $any_ext->({ PATH_INFO => '/foo/bar.html' }) ],
47      [ { }, 'html' ],
48      '.html matches .* and extension returned'
49    );
50
51    is_deeply(
52      [ $any_ext->({ PATH_INFO => '/foo/bar' }) ],
53      [],
54      'no extension does not match .*'
55    );
56 }
57
58 {
59    my $slash = $dp->parse('/');
60
61    is_deeply(
62      [ $slash->({ PATH_INFO => '/' }) ],
63      [ {} ],
64      '/ matches /'
65    );
66
67    is_deeply(
68      [ $slash->({ PATH_INFO => '/foo' }) ],
69      [ ],
70      '/foo does not match /'
71    );
72 }
73
74 {
75    my $post = $dp->parse('/post/*');
76
77    is_deeply(
78      [ $post->({ PATH_INFO => '/post/one' }) ],
79      [ {}, 'one' ],
80      '/post/one parses out one'
81    );
82
83    is_deeply(
84      [ $post->({ PATH_INFO => '/post/one/' }) ],
85      [],
86      '/post/one/ does not match'
87    );
88
89    is_deeply(
90      [ $post->({ PATH_INFO => '/post/one.html' }) ],
91      [ {}, 'one' ],
92      '/post/one.html still parses out one'
93    );
94 }
95
96 {
97    my $post = $dp->parse('/foo-bar/*');
98
99    is_deeply(
100      [ $post->({ PATH_INFO => '/foo-bar/one' }) ],
101      [ {}, 'one' ],
102      '/foo-bar/one parses out one'
103    );
104
105    is_deeply(
106      [ $post->({ PATH_INFO => '/foo-bar/one/' }) ],
107      [],
108      '/foo-bar/one/ does not match'
109    );
110 }
111
112 {
113    my $combi = $dp->parse('GET+/post/*');
114
115    is_deeply(
116      [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'GET' }) ],
117      [ {}, 'one' ],
118      '/post/one parses out one'
119    );
120
121    is_deeply(
122      [ $combi->({ PATH_INFO => '/post/one/', REQUEST_METHOD => 'GET' }) ],
123      [],
124      '/post/one/ does not match'
125    );
126
127    is_deeply(
128      [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'POST' }) ],
129      [],
130      'POST /post/one does not match'
131    );
132 }
133
134 {
135    my $or = $dp->parse('GET|POST');
136
137    foreach my $meth (qw(GET POST)) {
138
139      is_deeply(
140        [ $or->({ REQUEST_METHOD => $meth }) ],
141        [ {} ],
142        'GET|POST matches method '.$meth
143      );
144    }
145
146    is_deeply(
147      [ $or->({ REQUEST_METHOD => 'PUT' }) ],
148      [],
149      'GET|POST does not match PUT'
150    );
151 }
152
153 {
154    my $or = $dp->parse('GET|POST|DELETE');
155
156    foreach my $meth (qw(GET POST DELETE)) {
157
158      is_deeply(
159        [ $or->({ REQUEST_METHOD => $meth }) ],
160        [ {} ],
161        'GET|POST|DELETE matches method '.$meth
162      );
163    }
164
165    is_deeply(
166      [ $or->({ REQUEST_METHOD => 'PUT' }) ],
167      [],
168      'GET|POST|DELETE does not match PUT'
169    );
170 }
171
172 {
173    my $nest = $dp->parse('(GET+/foo)|POST');
174
175    is_deeply(
176      [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'GET' }) ],
177      [ {} ],
178      '(GET+/foo)|POST matches GET /foo'
179    );
180
181    is_deeply(
182      [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'GET' }) ],
183      [],
184      '(GET+/foo)|POST does not match GET /bar'
185    );
186
187    is_deeply(
188      [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'POST' }) ],
189      [ {} ],
190      '(GET+/foo)|POST matches POST /bar'
191    );
192
193    is_deeply(
194      [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'PUT' }) ],
195      [],
196      '(GET+/foo)|POST does not match PUT /foo'
197    );
198 }
199
200 {
201   local $@;
202   ok(
203     !eval { $dp->parse('/foo+(GET'); 1 },
204     'Death with missing closing )'
205   );
206   my $err = q{
207     /foo+(GET
208          ^
209   };
210   (s/^\n//s,s/\n  $//s,s/^    //mg) for $err;
211   like(
212     $@,
213     qr{\Q$err\E},
214     "Error $@ matches\n${err}\n"
215   );
216 }
217
218 {
219    my $not = $dp->parse('!.html+.*');
220
221    is_deeply(
222      [ $not->({ PATH_INFO => '/foo.xml' }) ],
223      [ {}, 'xml' ],
224      '!.html+.* matches /foo.xml'
225    );
226
227    is_deeply(
228      [ $not->({ PATH_INFO => '/foo.html' }) ],
229      [],
230      '!.html+.* does not match /foo.html'
231    );
232
233    is_deeply(
234      [ $not->({ PATH_INFO => '/foo' }) ],
235      [],
236      '!.html+.* does not match /foo'
237    );
238 }
239
240 {
241    my $sub = $dp->parse('/foo/*/...');
242
243    is_deeply(
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'
247    );
248
249    is_deeply(
250      [ $sub->({ PATH_INFO => '/foo/1/' }) ],
251      [ { PATH_INFO => '/', SCRIPT_NAME => '/foo/1' }, 1 ],
252      '/foo/*/... matches /foo/1/bar and strips to /'
253    );
254
255    is_deeply(
256      [ $sub->({ PATH_INFO => '/foo/1' }) ],
257      [],
258      '/foo/*/... does not match /foo/1 (no trailing /)'
259    );
260 }
261
262 #
263 # query string
264 #
265
266 my $q = 'foo=FOO&bar=BAR1&baz=one+two&quux=QUUX1&quux=QUUX2'
267     .'&bar=BAR2&quux=QUUX3&evil=%2F';
268
269 my %all_single = (
270     foo => 'FOO',
271     bar => 'BAR2',
272     baz => 'one two',
273     quux => 'QUUX3',
274     evil => '/',
275 );
276
277 my %all_multi = (
278     foo => [ 'FOO' ],
279     bar => [ qw(BAR1 BAR2) ],
280     baz => [ 'one two' ],
281     quux => [ qw(QUUX1 QUUX2 QUUX3) ],
282     evil => [ '/' ],
283 );
284
285 foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') {
286     my $foo = $dp->parse($lose);
287
288     is_deeply(
289         [ $foo->({ QUERY_STRING => '' }) ],
290         [],
291         "${lose} fails with no query"
292     );
293
294     is_deeply(
295         [ $foo->({ QUERY_STRING => 'bar=baz' }) ],
296         [],
297         "${lose} fails with query missing foo key"
298     );
299 }
300
301 foreach my $win (
302     [ '?foo=' => 'FOO' ],
303     [ '?:foo=' => { foo => 'FOO' } ],
304     [ '?spoo~' => undef ],
305     [ '?:spoo~' => {} ],
306     [ '?@spoo~' => [] ],
307     [ '?:@spoo~' => { spoo => [] } ],
308     [ '?bar=' => 'BAR2' ],
309     [ '?:bar=' => { bar => 'BAR2' } ],
310     [ '?@bar=' => [ qw(BAR1 BAR2) ] ],
311     [ '?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
312     [ '?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
313     [ '?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
314     [ '?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
315     [ '?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
316     [ '?*' => \%all_single ],
317     [ '?@*' => \%all_multi ],
318     [ '?foo=&@*' => 'FOO', do { my %h = %all_multi; delete $h{foo}; \%h } ],
319     [ '?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
320     [ '?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
321 ) {
322     my ($spec, @res) = @$win;
323     my $match = $dp->parse($spec);
324     #use Data::Dump::Streamer; warn Dump($match);
325     is_deeply(
326         [ $match->({ QUERY_STRING => $q }) ],
327         [ {}, @res ],
328         "${spec} matches correctly"
329     );
330 }
331
332 #
333 # /path/info/ + query string
334 #
335
336 foreach my $lose2 ('/foo/bar/+?foo=','/foo/bar/+?:foo=','/foo/bar/+?@foo=','/foo/bar/+?:@foo=') {
337     my $foo = $dp->parse($lose2);
338
339     is_deeply(
340         [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => '' }) ],
341         [ ],
342         "${lose2} fails with no query"
343     );
344
345     is_deeply(
346         [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => 'bar=baz' }) ],
347         [ ],
348         "${lose2} fails with query missing foo key"
349     );
350 }
351
352 foreach my $win2 (
353     [ '/foo/bar/+?foo=' => 'FOO' ],
354     [ '/foo/bar/+?:foo=' => { foo => 'FOO' } ],
355     [ '/foo/bar/+?spoo~' => undef ],
356     [ '/foo/bar/+?:spoo~' => {} ],
357     [ '/foo/bar/+?@spoo~' => [] ],
358     [ '/foo/bar/+?:@spoo~' => { spoo => [] } ],
359     [ '/foo/bar/+?bar=' => 'BAR2' ],
360     [ '/foo/bar/+?:bar=' => { bar => 'BAR2' } ],
361     [ '/foo/bar/+?@bar=' => [ qw(BAR1 BAR2) ] ],
362     [ '/foo/bar/+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
363     [ '/foo/bar/+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
364     [ '/foo/bar/+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
365     [ '/foo/bar/+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
366     [ '/foo/bar/+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
367     [ '/foo/bar/+?*' => \%all_single ],
368     [ '/foo/bar/+?@*' => \%all_multi ],
369     [ '/foo/bar/+?foo=&@*' => 'FOO', do { my %h = %all_multi; delete $h{foo}; \%h } ],
370     [ '/foo/bar/+?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
371     [ '/foo/bar/+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
372 ) {
373     my ($spec, @res) = @$win2;
374     my $match = $dp->parse($spec);
375     # use Data::Dump::Streamer; warn Dump($match);
376     is_deeply(
377         [ $match->({ PATH_INFO => '/foo/bar/', QUERY_STRING => $q }) ],
378         [ {}, @res ],
379         "${spec} matches correctly"
380     );
381 }
382
383 #
384 # /path/info + query string
385 #
386
387 foreach my $lose3 ('/foo/bar+?foo=','/foo/bar+?:foo=','/foo/bar+?@foo=','/foo/bar+?:@foo=') {
388     my $foo = $dp->parse($lose3);
389
390     is_deeply(
391         [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => '' }) ],
392         [ ],
393         "${lose3} fails with no query"
394     );
395
396     is_deeply(
397         [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => 'bar=baz' }) ],
398         [ ],
399         "${lose3} fails with query missing foo key"
400     );
401 }
402
403 foreach my $win3 (
404     [ '/foo/bar+?foo=' => 'FOO' ],
405     [ '/foo/bar+?:foo=' => { foo => 'FOO' } ],
406     [ '/foo/bar+?spoo~' => undef ],
407     [ '/foo/bar+?:spoo~' => {} ],
408     [ '/foo/bar+?@spoo~' => [] ],
409     [ '/foo/bar+?:@spoo~' => { spoo => [] } ],
410     [ '/foo/bar+?bar=' => 'BAR2' ],
411     [ '/foo/bar+?:bar=' => { bar => 'BAR2' } ],
412     [ '/foo/bar+?@bar=' => [ qw(BAR1 BAR2) ] ],
413     [ '/foo/bar+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
414     [ '/foo/bar+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
415     [ '/foo/bar+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
416     [ '/foo/bar+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
417     [ '/foo/bar+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
418     [ '/foo/bar+?*' => \%all_single ],
419     [ '/foo/bar+?@*' => \%all_multi ],
420     [ '/foo/bar+?foo=&@*' => 'FOO', do { my %h = %all_multi; delete $h{foo}; \%h } ],
421     [ '/foo/bar+?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
422     [ '/foo/bar+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
423 ) {
424     my ($spec, @res) = @$win3;
425     my $match = $dp->parse($spec);
426     # use Data::Dump::Streamer; warn Dump($match);
427     is_deeply(
428         [ $match->({ PATH_INFO => '/foo/bar', QUERY_STRING => $q }) ],
429         [ {}, @res ],
430         "${spec} matches correctly"
431     );
432 }