adapted to take dashes
[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
051b7ee2 10{
d63bcdae 11 my $get = $dp->parse('GET');
051b7ee2 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}
920d6222 25
26ok(
d63bcdae 27 !eval { $dp->parse('GET POST'); 1; },
920d6222 28 "Don't yet allow two methods"
29);
30
051b7ee2 31{
d63bcdae 32 my $html = $dp->parse('.html');
051b7ee2 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 );
c6ea9542 45}
46
051b7ee2 47{
d63bcdae 48 my $any_ext = $dp->parse('.*');
051b7ee2 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}
da9b9236 62
051b7ee2 63{
d63bcdae 64 my $slash = $dp->parse('/');
051b7ee2 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}
da9b9236 78
051b7ee2 79{
d63bcdae 80 my $post = $dp->parse('/post/*');
051b7ee2 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 );
da9b9236 93}
94
051b7ee2 95{
1c4f4b78 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{
d63bcdae 112 my $combi = $dp->parse('GET+/post/*');
051b7ee2 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}
b0420ad6 132
051b7ee2 133{
d63bcdae 134 my $or = $dp->parse('GET|POST');
051b7ee2 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}
b0420ad6 151
051b7ee2 152{
d63bcdae 153 my $or = $dp->parse('GET|POST|DELETE');
051b7ee2 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}
b0420ad6 170
051b7ee2 171{
d63bcdae 172 my $nest = $dp->parse('(GET+/foo)|POST');
051b7ee2 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}
a4ec359d 198
199{
200 local $@;
201 ok(
d63bcdae 202 !eval { $dp->parse('/foo+(GET'); 1 },
a4ec359d 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}
2ee4ab06 216
051b7ee2 217{
d63bcdae 218 my $not = $dp->parse('!.html+.*');
051b7ee2 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}
da8429c9 238
051b7ee2 239{
d63bcdae 240 my $sub = $dp->parse('/foo/*/...');
051b7ee2 241
242 is_deeply(
243 [ $sub->({ PATH_INFO => '/foo/1/bar' }) ],
d63bcdae 244 [ { PATH_INFO => '/bar', SCRIPT_NAME => '/foo/1' }, 1 ],
051b7ee2 245 '/foo/*/... matches /foo/1/bar and strips to /bar'
246 );
247
248 is_deeply(
249 [ $sub->({ PATH_INFO => '/foo/1/' }) ],
d63bcdae 250 [ { PATH_INFO => '/', SCRIPT_NAME => '/foo/1' }, 1 ],
051b7ee2 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}
a5917caa 260
6c0f599a 261#
262# query string
263#
264
a5917caa 265my $q = 'foo=FOO&bar=BAR1&baz=one+two&quux=QUUX1&quux=QUUX2'
6c0f599a 266 .'&bar=BAR2&quux=QUUX3&evil=%2F';
a5917caa 267
268my %all_single = (
6c0f599a 269 foo => 'FOO',
270 bar => 'BAR2',
271 baz => 'one two',
272 quux => 'QUUX3',
273 evil => '/',
a5917caa 274);
275
276my %all_multi = (
6c0f599a 277 foo => [ 'FOO' ],
278 bar => [ qw(BAR1 BAR2) ],
279 baz => [ 'one two' ],
280 quux => [ qw(QUUX1 QUUX2 QUUX3) ],
281 evil => [ '/' ],
a5917caa 282);
283
eb9e0e25 284foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') {
d63bcdae 285 my $foo = $dp->parse($lose);
6c0f599a 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}
a5917caa 299
6c0f599a 300foreach 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;
d63bcdae 322 my $match = $dp->parse($spec);
6c0f599a 323 #use Data::Dump::Streamer; warn Dump($match);
324 is_deeply(
325 [ $match->({ QUERY_STRING => $q }) ],
326 [ {}, @res ],
327 "${spec} matches correctly"
328 );
329}
eb9e0e25 330
6c0f599a 331#
332# /path/info/ + query string
333#
334
335foreach my $lose2 ('/foo/bar/+?foo=','/foo/bar/+?:foo=','/foo/bar/+?@foo=','/foo/bar/+?:@foo=') {
d63bcdae 336 my $foo = $dp->parse($lose2);
6c0f599a 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 );
eb9e0e25 349}
a5917caa 350
6c0f599a 351foreach 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) ] } ],
a5917caa 371) {
6c0f599a 372 my ($spec, @res) = @$win2;
d63bcdae 373 my $match = $dp->parse($spec);
6c0f599a 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
386foreach my $lose3 ('/foo/bar+?foo=','/foo/bar+?:foo=','/foo/bar+?@foo=','/foo/bar+?:@foo=') {
d63bcdae 387 my $foo = $dp->parse($lose3);
6c0f599a 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
402foreach 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;
d63bcdae 424 my $match = $dp->parse($spec);
6c0f599a 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 );
a5917caa 431}