factor dispatch parser out
[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{
d63bcdae 96 my $combi = $dp->parse('GET+/post/*');
051b7ee2 97
98 is_deeply(
99 [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'GET' }) ],
100 [ {}, 'one' ],
101 '/post/one parses out one'
102 );
103
104 is_deeply(
105 [ $combi->({ PATH_INFO => '/post/one/', REQUEST_METHOD => 'GET' }) ],
106 [],
107 '/post/one/ does not match'
108 );
109
110 is_deeply(
111 [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'POST' }) ],
112 [],
113 'POST /post/one does not match'
114 );
115}
b0420ad6 116
051b7ee2 117{
d63bcdae 118 my $or = $dp->parse('GET|POST');
051b7ee2 119
120 foreach my $meth (qw(GET POST)) {
121
122 is_deeply(
123 [ $or->({ REQUEST_METHOD => $meth }) ],
124 [ {} ],
125 'GET|POST matches method '.$meth
126 );
127 }
128
129 is_deeply(
130 [ $or->({ REQUEST_METHOD => 'PUT' }) ],
131 [],
132 'GET|POST does not match PUT'
133 );
134}
b0420ad6 135
051b7ee2 136{
d63bcdae 137 my $or = $dp->parse('GET|POST|DELETE');
051b7ee2 138
139 foreach my $meth (qw(GET POST DELETE)) {
140
141 is_deeply(
142 [ $or->({ REQUEST_METHOD => $meth }) ],
143 [ {} ],
144 'GET|POST|DELETE matches method '.$meth
145 );
146 }
147
148 is_deeply(
149 [ $or->({ REQUEST_METHOD => 'PUT' }) ],
150 [],
151 'GET|POST|DELETE does not match PUT'
152 );
153}
b0420ad6 154
051b7ee2 155{
d63bcdae 156 my $nest = $dp->parse('(GET+/foo)|POST');
051b7ee2 157
158 is_deeply(
159 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'GET' }) ],
160 [ {} ],
161 '(GET+/foo)|POST matches GET /foo'
162 );
163
164 is_deeply(
165 [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'GET' }) ],
166 [],
167 '(GET+/foo)|POST does not match GET /bar'
168 );
169
170 is_deeply(
171 [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'POST' }) ],
172 [ {} ],
173 '(GET+/foo)|POST matches POST /bar'
174 );
175
176 is_deeply(
177 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'PUT' }) ],
178 [],
179 '(GET+/foo)|POST does not match PUT /foo'
180 );
181}
a4ec359d 182
183{
184 local $@;
185 ok(
d63bcdae 186 !eval { $dp->parse('/foo+(GET'); 1 },
a4ec359d 187 'Death with missing closing )'
188 );
189 my $err = q{
190 /foo+(GET
191 ^
192 };
193 (s/^\n//s,s/\n $//s,s/^ //mg) for $err;
194 like(
195 $@,
196 qr{\Q$err\E},
197 "Error $@ matches\n${err}\n"
198 );
199}
2ee4ab06 200
051b7ee2 201{
d63bcdae 202 my $not = $dp->parse('!.html+.*');
051b7ee2 203
204 is_deeply(
205 [ $not->({ PATH_INFO => '/foo.xml' }) ],
206 [ { PATH_INFO => '/foo' }, 'xml' ],
207 '!.html+.* matches /foo.xml'
208 );
209
210 is_deeply(
211 [ $not->({ PATH_INFO => '/foo.html' }) ],
212 [],
213 '!.html+.* does not match /foo.html'
214 );
215
216 is_deeply(
217 [ $not->({ PATH_INFO => '/foo' }) ],
218 [],
219 '!.html+.* does not match /foo'
220 );
221}
da8429c9 222
051b7ee2 223{
d63bcdae 224 my $sub = $dp->parse('/foo/*/...');
051b7ee2 225
226 is_deeply(
227 [ $sub->({ PATH_INFO => '/foo/1/bar' }) ],
d63bcdae 228 [ { PATH_INFO => '/bar', SCRIPT_NAME => '/foo/1' }, 1 ],
051b7ee2 229 '/foo/*/... matches /foo/1/bar and strips to /bar'
230 );
231
232 is_deeply(
233 [ $sub->({ PATH_INFO => '/foo/1/' }) ],
d63bcdae 234 [ { PATH_INFO => '/', SCRIPT_NAME => '/foo/1' }, 1 ],
051b7ee2 235 '/foo/*/... matches /foo/1/bar and strips to /'
236 );
237
238 is_deeply(
239 [ $sub->({ PATH_INFO => '/foo/1' }) ],
240 [],
241 '/foo/*/... does not match /foo/1 (no trailing /)'
242 );
243}
a5917caa 244
6c0f599a 245#
246# query string
247#
248
a5917caa 249my $q = 'foo=FOO&bar=BAR1&baz=one+two&quux=QUUX1&quux=QUUX2'
6c0f599a 250 .'&bar=BAR2&quux=QUUX3&evil=%2F';
a5917caa 251
252my %all_single = (
6c0f599a 253 foo => 'FOO',
254 bar => 'BAR2',
255 baz => 'one two',
256 quux => 'QUUX3',
257 evil => '/',
a5917caa 258);
259
260my %all_multi = (
6c0f599a 261 foo => [ 'FOO' ],
262 bar => [ qw(BAR1 BAR2) ],
263 baz => [ 'one two' ],
264 quux => [ qw(QUUX1 QUUX2 QUUX3) ],
265 evil => [ '/' ],
a5917caa 266);
267
eb9e0e25 268foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') {
d63bcdae 269 my $foo = $dp->parse($lose);
6c0f599a 270
271 is_deeply(
272 [ $foo->({ QUERY_STRING => '' }) ],
273 [],
274 "${lose} fails with no query"
275 );
276
277 is_deeply(
278 [ $foo->({ QUERY_STRING => 'bar=baz' }) ],
279 [],
280 "${lose} fails with query missing foo key"
281 );
282}
a5917caa 283
6c0f599a 284foreach my $win (
285 [ '?foo=' => 'FOO' ],
286 [ '?:foo=' => { foo => 'FOO' } ],
287 [ '?spoo~' => undef ],
288 [ '?:spoo~' => {} ],
289 [ '?@spoo~' => [] ],
290 [ '?:@spoo~' => { spoo => [] } ],
291 [ '?bar=' => 'BAR2' ],
292 [ '?:bar=' => { bar => 'BAR2' } ],
293 [ '?@bar=' => [ qw(BAR1 BAR2) ] ],
294 [ '?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
295 [ '?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
296 [ '?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
297 [ '?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
298 [ '?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
299 [ '?*' => \%all_single ],
300 [ '?@*' => \%all_multi ],
301 [ '?foo=&@*' => 'FOO', do { my %h = %all_multi; delete $h{foo}; \%h } ],
302 [ '?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
303 [ '?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
304) {
305 my ($spec, @res) = @$win;
d63bcdae 306 my $match = $dp->parse($spec);
6c0f599a 307 #use Data::Dump::Streamer; warn Dump($match);
308 is_deeply(
309 [ $match->({ QUERY_STRING => $q }) ],
310 [ {}, @res ],
311 "${spec} matches correctly"
312 );
313}
eb9e0e25 314
6c0f599a 315#
316# /path/info/ + query string
317#
318
319foreach my $lose2 ('/foo/bar/+?foo=','/foo/bar/+?:foo=','/foo/bar/+?@foo=','/foo/bar/+?:@foo=') {
d63bcdae 320 my $foo = $dp->parse($lose2);
6c0f599a 321
322 is_deeply(
323 [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => '' }) ],
324 [ ],
325 "${lose2} fails with no query"
326 );
327
328 is_deeply(
329 [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => 'bar=baz' }) ],
330 [ ],
331 "${lose2} fails with query missing foo key"
332 );
eb9e0e25 333}
a5917caa 334
6c0f599a 335foreach my $win2 (
336 [ '/foo/bar/+?foo=' => 'FOO' ],
337 [ '/foo/bar/+?:foo=' => { foo => 'FOO' } ],
338 [ '/foo/bar/+?spoo~' => undef ],
339 [ '/foo/bar/+?:spoo~' => {} ],
340 [ '/foo/bar/+?@spoo~' => [] ],
341 [ '/foo/bar/+?:@spoo~' => { spoo => [] } ],
342 [ '/foo/bar/+?bar=' => 'BAR2' ],
343 [ '/foo/bar/+?:bar=' => { bar => 'BAR2' } ],
344 [ '/foo/bar/+?@bar=' => [ qw(BAR1 BAR2) ] ],
345 [ '/foo/bar/+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
346 [ '/foo/bar/+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
347 [ '/foo/bar/+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
348 [ '/foo/bar/+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
349 [ '/foo/bar/+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
350 [ '/foo/bar/+?*' => \%all_single ],
351 [ '/foo/bar/+?@*' => \%all_multi ],
352 [ '/foo/bar/+?foo=&@*' => 'FOO', do { my %h = %all_multi; delete $h{foo}; \%h } ],
353 [ '/foo/bar/+?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
354 [ '/foo/bar/+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
a5917caa 355) {
6c0f599a 356 my ($spec, @res) = @$win2;
d63bcdae 357 my $match = $dp->parse($spec);
6c0f599a 358 # use Data::Dump::Streamer; warn Dump($match);
359 is_deeply(
360 [ $match->({ PATH_INFO => '/foo/bar/', QUERY_STRING => $q }) ],
361 [ {}, @res ],
362 "${spec} matches correctly"
363 );
364}
365
366#
367# /path/info + query string
368#
369
370foreach my $lose3 ('/foo/bar+?foo=','/foo/bar+?:foo=','/foo/bar+?@foo=','/foo/bar+?:@foo=') {
d63bcdae 371 my $foo = $dp->parse($lose3);
6c0f599a 372
373 is_deeply(
374 [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => '' }) ],
375 [ ],
376 "${lose3} fails with no query"
377 );
378
379 is_deeply(
380 [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => 'bar=baz' }) ],
381 [ ],
382 "${lose3} fails with query missing foo key"
383 );
384}
385
386foreach my $win3 (
387 [ '/foo/bar+?foo=' => 'FOO' ],
388 [ '/foo/bar+?:foo=' => { foo => 'FOO' } ],
389 [ '/foo/bar+?spoo~' => undef ],
390 [ '/foo/bar+?:spoo~' => {} ],
391 [ '/foo/bar+?@spoo~' => [] ],
392 [ '/foo/bar+?:@spoo~' => { spoo => [] } ],
393 [ '/foo/bar+?bar=' => 'BAR2' ],
394 [ '/foo/bar+?:bar=' => { bar => 'BAR2' } ],
395 [ '/foo/bar+?@bar=' => [ qw(BAR1 BAR2) ] ],
396 [ '/foo/bar+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
397 [ '/foo/bar+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
398 [ '/foo/bar+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
399 [ '/foo/bar+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
400 [ '/foo/bar+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
401 [ '/foo/bar+?*' => \%all_single ],
402 [ '/foo/bar+?@*' => \%all_multi ],
403 [ '/foo/bar+?foo=&@*' => 'FOO', do { my %h = %all_multi; delete $h{foo}; \%h } ],
404 [ '/foo/bar+?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
405 [ '/foo/bar+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
406) {
407 my ($spec, @res) = @$win3;
d63bcdae 408 my $match = $dp->parse($spec);
6c0f599a 409 # use Data::Dump::Streamer; warn Dump($match);
410 is_deeply(
411 [ $match->({ PATH_INFO => '/foo/bar', QUERY_STRING => $q }) ],
412 [ {}, @res ],
413 "${spec} matches correctly"
414 );
a5917caa 415}