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