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 $or = $dp->parse_dispatch_specification('GET|POST');
120 foreach my $meth (qw(GET POST)) {
123 [ $or->({ REQUEST_METHOD => $meth }) ],
125 'GET|POST matches method '.$meth
130 [ $or->({ REQUEST_METHOD => 'PUT' }) ],
132 'GET|POST does not match PUT'
137 my $or = $dp->parse_dispatch_specification('GET|POST|DELETE');
139 foreach my $meth (qw(GET POST DELETE)) {
142 [ $or->({ REQUEST_METHOD => $meth }) ],
144 'GET|POST|DELETE matches method '.$meth
149 [ $or->({ REQUEST_METHOD => 'PUT' }) ],
151 'GET|POST|DELETE does not match PUT'
156 my $nest = $dp->parse_dispatch_specification('(GET+/foo)|POST');
159 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'GET' }) ],
161 '(GET+/foo)|POST matches GET /foo'
165 [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'GET' }) ],
167 '(GET+/foo)|POST does not match GET /bar'
171 [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'POST' }) ],
173 '(GET+/foo)|POST matches POST /bar'
177 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'PUT' }) ],
179 '(GET+/foo)|POST does not match PUT /foo'
186 !eval { $dp->parse_dispatch_specification('/foo+(GET'); 1 },
187 'Death with missing closing )'
193 (s/^\n//s,s/\n $//s,s/^ //mg) for $err;
197 "Error $@ matches\n${err}\n"
202 my $not = $dp->parse_dispatch_specification('!.html+.*');
205 [ $not->({ PATH_INFO => '/foo.xml' }) ],
206 [ { PATH_INFO => '/foo' }, 'xml' ],
207 '!.html+.* matches /foo.xml'
211 [ $not->({ PATH_INFO => '/foo.html' }) ],
213 '!.html+.* does not match /foo.html'
217 [ $not->({ PATH_INFO => '/foo' }) ],
219 '!.html+.* does not match /foo'
224 my $sub = $dp->parse_dispatch_specification('/foo/*/...');
227 [ $sub->({ PATH_INFO => '/foo/1/bar' }) ],
228 [ { PATH_INFO => '/bar' }, 1 ],
229 '/foo/*/... matches /foo/1/bar and strips to /bar'
233 [ $sub->({ PATH_INFO => '/foo/1/' }) ],
234 [ { PATH_INFO => '/' }, 1 ],
235 '/foo/*/... matches /foo/1/bar and strips to /'
239 [ $sub->({ PATH_INFO => '/foo/1' }) ],
241 '/foo/*/... does not match /foo/1 (no trailing /)'
245 my $q = 'foo=FOO&bar=BAR1&baz=one+two&quux=QUUX1&quux=QUUX2'
246 .'&bar=BAR2&quux=QUUX3&evil=%2F';
258 bar => [ qw(BAR1 BAR2) ],
259 baz => [ 'one two' ],
260 quux => [ qw(QUUX1 QUUX2 QUUX3) ],
264 foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') {
265 my $foo = $dp->parse_dispatch_specification($lose);
268 [ $foo->({ QUERY_STRING => '' }) ],
270 "${lose} fails with no query"
274 [ $foo->({ QUERY_STRING => 'bar=baz' }) ],
276 "${lose} fails with query missing foo key"
281 [ '?foo=' => 'FOO' ],
282 [ '?:foo=' => { foo => 'FOO' } ],
283 [ '?spoo~' => undef ],
286 [ '?:@spoo~' => { spoo => [] } ],
287 [ '?bar=' => 'BAR2' ],
288 [ '?:bar=' => { bar => 'BAR2' } ],
289 [ '?@bar=' => [ qw(BAR1 BAR2) ] ],
290 [ '?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ],
291 [ '?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ],
292 [ '?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ],
293 [ '?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ],
294 [ '?:baz=&:evil=' => { baz => 'one two', evil => '/' } ],
295 [ '?*' => \%all_single ],
296 [ '?@*' => \%all_multi ],
297 [ '?foo=&@*' => 'FOO', do { my %h = %all_multi; delete $h{foo}; \%h } ],
298 [ '?:foo=&@*' => { %all_multi, foo => 'FOO' } ],
299 [ '?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
301 my ($spec, @res) = @$win;
302 my $match = $dp->parse_dispatch_specification($spec);
303 #use Data::Dump::Streamer; warn Dump($match);
305 [ $match->({ QUERY_STRING => $q }) ],
307 "${spec} matches correctly"