Commit | Line | Data |
920d6222 |
1 | use strict; |
2 | use warnings FATAL => 'all'; |
3 | |
4 | use Test::More qw(no_plan); |
5 | |
d63bcdae |
6 | use Web::Dispatch::Parser; |
920d6222 |
7 | |
d63bcdae |
8 | my $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 | |
051b7ee2 |
26 | { |
d63bcdae |
27 | my $html = $dp->parse('.html'); |
051b7ee2 |
28 | |
29 | is_deeply( |
30 | [ $html->({ PATH_INFO => '/foo/bar.html' }) ], |
5ba2eb68 |
31 | [ { } ], |
051b7ee2 |
32 | '.html matches' |
33 | ); |
34 | |
35 | is_deeply( |
36 | [ $html->({ PATH_INFO => '/foo/bar.xml' }) ], |
37 | [], |
38 | '.xml does not match .html' |
39 | ); |
c6ea9542 |
40 | } |
41 | |
051b7ee2 |
42 | { |
d63bcdae |
43 | my $any_ext = $dp->parse('.*'); |
051b7ee2 |
44 | |
45 | is_deeply( |
46 | [ $any_ext->({ PATH_INFO => '/foo/bar.html' }) ], |
5ba2eb68 |
47 | [ { }, 'html' ], |
051b7ee2 |
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 | } |
da9b9236 |
57 | |
051b7ee2 |
58 | { |
d63bcdae |
59 | my $slash = $dp->parse('/'); |
051b7ee2 |
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 | } |
da9b9236 |
73 | |
051b7ee2 |
74 | { |
d63bcdae |
75 | my $post = $dp->parse('/post/*'); |
051b7ee2 |
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 | ); |
5ba2eb68 |
88 | |
89 | is_deeply( |
90 | [ $post->({ PATH_INFO => '/post/one.html' }) ], |
91 | [ {}, 'one' ], |
92 | '/post/one.html still parses out one' |
93 | ); |
da9b9236 |
94 | } |
95 | |
051b7ee2 |
96 | { |
1c4f4b78 |
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 | { |
d63bcdae |
113 | my $combi = $dp->parse('GET+/post/*'); |
051b7ee2 |
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 | } |
b0420ad6 |
133 | |
051b7ee2 |
134 | { |
d63bcdae |
135 | my $or = $dp->parse('GET|POST'); |
051b7ee2 |
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 | } |
b0420ad6 |
152 | |
051b7ee2 |
153 | { |
d63bcdae |
154 | my $or = $dp->parse('GET|POST|DELETE'); |
051b7ee2 |
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 | } |
b0420ad6 |
171 | |
051b7ee2 |
172 | { |
d63bcdae |
173 | my $nest = $dp->parse('(GET+/foo)|POST'); |
051b7ee2 |
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 | } |
a4ec359d |
199 | |
4b8f2b1f |
200 | TODO: { |
201 | local $TODO = 'needs implementing'; |
202 | eval { |
203 | my $star_star = $dp->parse('GET+/**'); |
204 | |
205 | is_deeply( |
206 | [ $star_star->({ PATH_INFO => '/foo/bar', REQUEST_METHOD => 'GET' }) ], |
207 | [ {}, [qw(foo bar)] ], |
208 | '/foo/bar via /** parses out [qw(foo bar)]' |
209 | ); |
210 | }; |
211 | }; |
212 | |
213 | TODO: { |
214 | local $TODO = 'needs implementing'; |
215 | eval { |
216 | my $grp_star = $dp->parse('GET+/(**)'); |
217 | |
218 | is_deeply( |
219 | [ $grp_star->({ PATH_INFO => '/foo/bar', REQUEST_METHOD => 'GET' }) ], |
220 | [ {}, 'foo/bar' ], |
221 | '/foo/bar via /(**) parses out "foo/bar"' |
222 | ); |
223 | }; |
224 | }; |
225 | |
226 | TODO: { |
227 | local $TODO = 'needs implementing'; |
228 | eval { |
229 | my $dot_star = $dp->parse('GET+/*.*'); |
230 | |
231 | is_deeply( |
232 | [ $dot_star->({ PATH_INFO => '/foo.bar', REQUEST_METHOD => 'GET' }) ], |
233 | [ {}, qw(foo bar) ], |
234 | '/foo.bar via /*.* parses out qw(foo bar)' |
235 | ); |
236 | }; |
237 | }; |
238 | |
239 | TODO: { |
240 | local $TODO = 'needs implementing'; |
241 | |
242 | eval { |
243 | my $group_ds = $dp->parse('GET+/(*.*)'); |
244 | is_deeply( |
245 | [ $group_ds->({ PATH_INFO => '/foo.bar', REQUEST_METHOD => 'GET' }) ], |
246 | [ {}, 'foo.bar' ], |
247 | '/foo.bar via /*.* parses out "foo.bar"' |
248 | ); |
249 | }; |
250 | }; |
251 | |
a4ec359d |
252 | { |
253 | local $@; |
254 | ok( |
d63bcdae |
255 | !eval { $dp->parse('/foo+(GET'); 1 }, |
a4ec359d |
256 | 'Death with missing closing )' |
257 | ); |
258 | my $err = q{ |
259 | /foo+(GET |
260 | ^ |
261 | }; |
262 | (s/^\n//s,s/\n $//s,s/^ //mg) for $err; |
263 | like( |
264 | $@, |
265 | qr{\Q$err\E}, |
266 | "Error $@ matches\n${err}\n" |
267 | ); |
268 | } |
2ee4ab06 |
269 | |
051b7ee2 |
270 | { |
d63bcdae |
271 | my $not = $dp->parse('!.html+.*'); |
051b7ee2 |
272 | |
273 | is_deeply( |
274 | [ $not->({ PATH_INFO => '/foo.xml' }) ], |
5ba2eb68 |
275 | [ {}, 'xml' ], |
051b7ee2 |
276 | '!.html+.* matches /foo.xml' |
277 | ); |
278 | |
279 | is_deeply( |
280 | [ $not->({ PATH_INFO => '/foo.html' }) ], |
281 | [], |
282 | '!.html+.* does not match /foo.html' |
283 | ); |
284 | |
285 | is_deeply( |
286 | [ $not->({ PATH_INFO => '/foo' }) ], |
287 | [], |
288 | '!.html+.* does not match /foo' |
289 | ); |
290 | } |
da8429c9 |
291 | |
051b7ee2 |
292 | { |
d63bcdae |
293 | my $sub = $dp->parse('/foo/*/...'); |
051b7ee2 |
294 | |
295 | is_deeply( |
296 | [ $sub->({ PATH_INFO => '/foo/1/bar' }) ], |
d63bcdae |
297 | [ { PATH_INFO => '/bar', SCRIPT_NAME => '/foo/1' }, 1 ], |
051b7ee2 |
298 | '/foo/*/... matches /foo/1/bar and strips to /bar' |
299 | ); |
300 | |
301 | is_deeply( |
302 | [ $sub->({ PATH_INFO => '/foo/1/' }) ], |
d63bcdae |
303 | [ { PATH_INFO => '/', SCRIPT_NAME => '/foo/1' }, 1 ], |
051b7ee2 |
304 | '/foo/*/... matches /foo/1/bar and strips to /' |
305 | ); |
306 | |
307 | is_deeply( |
308 | [ $sub->({ PATH_INFO => '/foo/1' }) ], |
309 | [], |
310 | '/foo/*/... does not match /foo/1 (no trailing /)' |
311 | ); |
312 | } |
a5917caa |
313 | |
6c0f599a |
314 | # |
315 | # query string |
316 | # |
317 | |
a5917caa |
318 | my $q = 'foo=FOO&bar=BAR1&baz=one+two&quux=QUUX1&quux=QUUX2' |
6c0f599a |
319 | .'&bar=BAR2&quux=QUUX3&evil=%2F'; |
a5917caa |
320 | |
321 | my %all_single = ( |
6c0f599a |
322 | foo => 'FOO', |
323 | bar => 'BAR2', |
324 | baz => 'one two', |
325 | quux => 'QUUX3', |
326 | evil => '/', |
a5917caa |
327 | ); |
328 | |
329 | my %all_multi = ( |
6c0f599a |
330 | foo => [ 'FOO' ], |
331 | bar => [ qw(BAR1 BAR2) ], |
332 | baz => [ 'one two' ], |
333 | quux => [ qw(QUUX1 QUUX2 QUUX3) ], |
334 | evil => [ '/' ], |
a5917caa |
335 | ); |
336 | |
eb9e0e25 |
337 | foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') { |
d63bcdae |
338 | my $foo = $dp->parse($lose); |
6c0f599a |
339 | |
340 | is_deeply( |
341 | [ $foo->({ QUERY_STRING => '' }) ], |
342 | [], |
343 | "${lose} fails with no query" |
344 | ); |
345 | |
346 | is_deeply( |
347 | [ $foo->({ QUERY_STRING => 'bar=baz' }) ], |
348 | [], |
349 | "${lose} fails with query missing foo key" |
350 | ); |
351 | } |
a5917caa |
352 | |
6c0f599a |
353 | foreach my $win ( |
354 | [ '?foo=' => 'FOO' ], |
355 | [ '?:foo=' => { foo => 'FOO' } ], |
356 | [ '?spoo~' => undef ], |
357 | [ '?:spoo~' => {} ], |
358 | [ '?@spoo~' => [] ], |
359 | [ '?:@spoo~' => { spoo => [] } ], |
360 | [ '?bar=' => 'BAR2' ], |
361 | [ '?:bar=' => { bar => 'BAR2' } ], |
362 | [ '?@bar=' => [ qw(BAR1 BAR2) ] ], |
363 | [ '?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ], |
364 | [ '?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ], |
365 | [ '?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ], |
366 | [ '?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ], |
367 | [ '?:baz=&:evil=' => { baz => 'one two', evil => '/' } ], |
368 | [ '?*' => \%all_single ], |
369 | [ '?@*' => \%all_multi ], |
052bdd54 |
370 | [ '?foo=&@*' => 'FOO', \%all_multi ], |
6c0f599a |
371 | [ '?:foo=&@*' => { %all_multi, foo => 'FOO' } ], |
372 | [ '?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ], |
373 | ) { |
374 | my ($spec, @res) = @$win; |
d63bcdae |
375 | my $match = $dp->parse($spec); |
6c0f599a |
376 | #use Data::Dump::Streamer; warn Dump($match); |
377 | is_deeply( |
378 | [ $match->({ QUERY_STRING => $q }) ], |
379 | [ {}, @res ], |
380 | "${spec} matches correctly" |
381 | ); |
382 | } |
eb9e0e25 |
383 | |
6c0f599a |
384 | # |
385 | # /path/info/ + query string |
386 | # |
387 | |
388 | foreach my $lose2 ('/foo/bar/+?foo=','/foo/bar/+?:foo=','/foo/bar/+?@foo=','/foo/bar/+?:@foo=') { |
d63bcdae |
389 | my $foo = $dp->parse($lose2); |
6c0f599a |
390 | |
391 | is_deeply( |
392 | [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => '' }) ], |
393 | [ ], |
394 | "${lose2} fails with no query" |
395 | ); |
396 | |
397 | is_deeply( |
398 | [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => 'bar=baz' }) ], |
399 | [ ], |
400 | "${lose2} fails with query missing foo key" |
401 | ); |
eb9e0e25 |
402 | } |
a5917caa |
403 | |
6c0f599a |
404 | foreach my $win2 ( |
405 | [ '/foo/bar/+?foo=' => 'FOO' ], |
406 | [ '/foo/bar/+?:foo=' => { foo => 'FOO' } ], |
407 | [ '/foo/bar/+?spoo~' => undef ], |
408 | [ '/foo/bar/+?:spoo~' => {} ], |
409 | [ '/foo/bar/+?@spoo~' => [] ], |
410 | [ '/foo/bar/+?:@spoo~' => { spoo => [] } ], |
411 | [ '/foo/bar/+?bar=' => 'BAR2' ], |
412 | [ '/foo/bar/+?:bar=' => { bar => 'BAR2' } ], |
413 | [ '/foo/bar/+?@bar=' => [ qw(BAR1 BAR2) ] ], |
414 | [ '/foo/bar/+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ], |
415 | [ '/foo/bar/+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ], |
416 | [ '/foo/bar/+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ], |
417 | [ '/foo/bar/+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ], |
418 | [ '/foo/bar/+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ], |
419 | [ '/foo/bar/+?*' => \%all_single ], |
420 | [ '/foo/bar/+?@*' => \%all_multi ], |
052bdd54 |
421 | [ '/foo/bar/+?foo=&@*' => 'FOO', \%all_multi ], |
6c0f599a |
422 | [ '/foo/bar/+?:foo=&@*' => { %all_multi, foo => 'FOO' } ], |
423 | [ '/foo/bar/+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ], |
a5917caa |
424 | ) { |
6c0f599a |
425 | my ($spec, @res) = @$win2; |
d63bcdae |
426 | my $match = $dp->parse($spec); |
6c0f599a |
427 | # use Data::Dump::Streamer; warn Dump($match); |
428 | is_deeply( |
429 | [ $match->({ PATH_INFO => '/foo/bar/', QUERY_STRING => $q }) ], |
430 | [ {}, @res ], |
431 | "${spec} matches correctly" |
432 | ); |
433 | } |
434 | |
435 | # |
436 | # /path/info + query string |
437 | # |
438 | |
439 | foreach my $lose3 ('/foo/bar+?foo=','/foo/bar+?:foo=','/foo/bar+?@foo=','/foo/bar+?:@foo=') { |
d63bcdae |
440 | my $foo = $dp->parse($lose3); |
6c0f599a |
441 | |
442 | is_deeply( |
443 | [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => '' }) ], |
444 | [ ], |
445 | "${lose3} fails with no query" |
446 | ); |
447 | |
448 | is_deeply( |
449 | [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => 'bar=baz' }) ], |
450 | [ ], |
451 | "${lose3} fails with query missing foo key" |
452 | ); |
453 | } |
454 | |
455 | foreach my $win3 ( |
456 | [ '/foo/bar+?foo=' => 'FOO' ], |
457 | [ '/foo/bar+?:foo=' => { foo => 'FOO' } ], |
458 | [ '/foo/bar+?spoo~' => undef ], |
459 | [ '/foo/bar+?:spoo~' => {} ], |
460 | [ '/foo/bar+?@spoo~' => [] ], |
461 | [ '/foo/bar+?:@spoo~' => { spoo => [] } ], |
462 | [ '/foo/bar+?bar=' => 'BAR2' ], |
463 | [ '/foo/bar+?:bar=' => { bar => 'BAR2' } ], |
464 | [ '/foo/bar+?@bar=' => [ qw(BAR1 BAR2) ] ], |
465 | [ '/foo/bar+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ], |
466 | [ '/foo/bar+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ], |
467 | [ '/foo/bar+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ], |
468 | [ '/foo/bar+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ], |
469 | [ '/foo/bar+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ], |
470 | [ '/foo/bar+?*' => \%all_single ], |
471 | [ '/foo/bar+?@*' => \%all_multi ], |
052bdd54 |
472 | [ '/foo/bar+?foo=&@*' => 'FOO', \%all_multi ], |
6c0f599a |
473 | [ '/foo/bar+?:foo=&@*' => { %all_multi, foo => 'FOO' } ], |
474 | [ '/foo/bar+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ], |
475 | ) { |
476 | my ($spec, @res) = @$win3; |
d63bcdae |
477 | my $match = $dp->parse($spec); |
6c0f599a |
478 | # use Data::Dump::Streamer; warn Dump($match); |
479 | is_deeply( |
480 | [ $match->({ PATH_INFO => '/foo/bar', QUERY_STRING => $q }) ], |
481 | [ {}, @res ], |
482 | "${spec} matches correctly" |
483 | ); |
a5917caa |
484 | } |