2 use warnings FATAL => 'all';
4 use Test::More qw(no_plan);
6 use Web::Simple::DispatchParser;
8 my $dp = Web::Simple::DispatchParser->new;
11 my $get = $dp->parse_dispatch_specification('GET');
14 [ $get->({ REQUEST_METHOD => 'GET' }) ],
20 [ $get->({ REQUEST_METHOD => 'POST' }) ],
27 !eval { $dp->parse_dispatch_specification('GET POST'); 1; },
28 "Don't yet allow two methods"
32 my $html = $dp->parse_dispatch_specification('.html');
35 [ $html->({ PATH_INFO => '/foo/bar.html' }) ],
36 [ { PATH_INFO => '/foo/bar' } ],
41 [ $html->({ PATH_INFO => '/foo/bar.xml' }) ],
43 '.xml does not match .html'
48 my $any_ext = $dp->parse_dispatch_specification('.*');
51 [ $any_ext->({ PATH_INFO => '/foo/bar.html' }) ],
52 [ { PATH_INFO => '/foo/bar' }, 'html' ],
53 '.html matches .* and extension returned'
57 [ $any_ext->({ PATH_INFO => '/foo/bar' }) ],
59 'no extension does not match .*'
64 my $slash = $dp->parse_dispatch_specification('/');
67 [ $slash->({ PATH_INFO => '/' }) ],
73 [ $slash->({ PATH_INFO => '/foo' }) ],
75 '/foo does not match /'
80 my $post = $dp->parse_dispatch_specification('/post/*');
83 [ $post->({ PATH_INFO => '/post/one' }) ],
85 '/post/one parses out one'
89 [ $post->({ PATH_INFO => '/post/one/' }) ],
91 '/post/one/ does not match'
96 my $combi = $dp->parse_dispatch_specification('GET+/post/*');
99 [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'GET' }) ],
101 '/post/one parses out one'
105 [ $combi->({ PATH_INFO => '/post/one/', REQUEST_METHOD => 'GET' }) ],
107 '/post/one/ does not match'
111 [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'POST' }) ],
113 'POST /post/one does not match'
118 my $combi = $dp->parse_dispatch_specification('?foo=');
121 [ $combi->({ PATH_INFO => '/?foo=' }) ],
123 '/post/one parses out one'
128 my $or = $dp->parse_dispatch_specification('GET|POST');
130 foreach my $meth (qw(GET POST)) {
133 [ $or->({ REQUEST_METHOD => $meth }) ],
135 'GET|POST matches method '.$meth
140 [ $or->({ REQUEST_METHOD => 'PUT' }) ],
142 'GET|POST does not match PUT'
147 my $or = $dp->parse_dispatch_specification('GET|POST|DELETE');
149 foreach my $meth (qw(GET POST DELETE)) {
152 [ $or->({ REQUEST_METHOD => $meth }) ],
154 'GET|POST|DELETE matches method '.$meth
159 [ $or->({ REQUEST_METHOD => 'PUT' }) ],
161 'GET|POST|DELETE does not match PUT'
166 my $nest = $dp->parse_dispatch_specification('(GET+/foo)|POST');
169 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'GET' }) ],
171 '(GET+/foo)|POST matches GET /foo'
175 [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'GET' }) ],
177 '(GET+/foo)|POST does not match GET /bar'
181 [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'POST' }) ],
183 '(GET+/foo)|POST matches POST /bar'
187 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'PUT' }) ],
189 '(GET+/foo)|POST does not match PUT /foo'
196 !eval { $dp->parse_dispatch_specification('/foo+(GET'); 1 },
197 'Death with missing closing )'
203 (s/^\n//s,s/\n $//s,s/^ //mg) for $err;
207 "Error $@ matches\n${err}\n"
212 my $not = $dp->parse_dispatch_specification('!.html+.*');
215 [ $not->({ PATH_INFO => '/foo.xml' }) ],
216 [ { PATH_INFO => '/foo' }, 'xml' ],
217 '!.html+.* matches /foo.xml'
221 [ $not->({ PATH_INFO => '/foo.html' }) ],
223 '!.html+.* does not match /foo.html'
227 [ $not->({ PATH_INFO => '/foo' }) ],
229 '!.html+.* does not match /foo'
234 my $sub = $dp->parse_dispatch_specification('/foo/*/...');
237 [ $sub->({ PATH_INFO => '/foo/1/bar' }) ],
238 [ { PATH_INFO => '/bar' }, 1 ],
239 '/foo/*/... matches /foo/1/bar and strips to /bar'
243 [ $sub->({ PATH_INFO => '/foo/1/' }) ],
244 [ { PATH_INFO => '/' }, 1 ],
245 '/foo/*/... matches /foo/1/bar and strips to /'
249 [ $sub->({ PATH_INFO => '/foo/1' }) ],
251 '/foo/*/... does not match /foo/1 (no trailing /)'
255 my $q = 'foo=FOO&bar=BAR1&baz=one+two&quux=QUUX1&quux=QUUX2'
256 .'&bar=BAR2&quux=QUUX3&evil=%2F';
268 bar => [ qw(BAR1 BAR2) ],
269 baz => [ 'one two' ],
270 quux => [ qw(QUUX1 QUUX2 QUUX3) ],
274 foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') {
275 my $foo = $dp->parse_dispatch_specification($lose);
278 [ $foo->({ QUERY_STRING => '' }) ],
280 "${lose} fails with no query"
284 [ $foo->({ QUERY_STRING => 'bar=baz' }) ],
286 "${lose} fails with query missing foo key"
291 [ '?foo=' => 'FOO' ],
292 [ '?:foo=' => { foo => 'FOO' } ],
293 [ '?spoo~' => undef ],
296 [ '?:@spoo~' => { spoo => [] } ],
297 [ '?bar=' => 'BAR2' ],
298 [ '?:bar=' => { bar => 'BAR2' } ],
299 [ '?@bar=' => [ qw(BAR1 BAR2) ] ],
300 [ '?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
301 [ '?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
302 [ '?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
303 [ '?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
304 [ '?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
305 [ '?*' => \%all_single ],
306 [ '?@*' => \%all_multi ],
307 [ '?foo=&@*' => 'FOO', do { my %h = %all_multi; delete $h{foo}; \%h } ],
308 [ '?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
309 [ '?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
311 my ($spec, @res) = @$win;
312 my $match = $dp->parse_dispatch_specification($spec);
313 #use Data::Dump::Streamer; warn Dump($match);
315 [ $match->({ QUERY_STRING => $q }) ],
317 "${spec} matches correctly"