wrap tests in blocks and a failing tests for ?foo=
[catagits/Web-Simple.git] / t / dispatch_parser.t
CommitLineData
920d6222 1use strict;
2use warnings FATAL => 'all';
3
4use Test::More qw(no_plan);
5
6use Web::Simple::DispatchParser;
7
8my $dp = Web::Simple::DispatchParser->new;
9
051b7ee2 10{
11 my $get = $dp->parse_dispatch_specification('GET');
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(
27 !eval { $dp->parse_dispatch_specification('GET POST'); 1; },
28 "Don't yet allow two methods"
29);
30
051b7ee2 31{
32 my $html = $dp->parse_dispatch_specification('.html');
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{
48 my $any_ext = $dp->parse_dispatch_specification('.*');
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{
64 my $slash = $dp->parse_dispatch_specification('/');
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{
80 my $post = $dp->parse_dispatch_specification('/post/*');
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{
96 my $combi = $dp->parse_dispatch_specification('GET+/post/*');
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{
118 my $combi = $dp->parse_dispatch_specification('?foo=');
b0420ad6 119
051b7ee2 120 is_deeply(
121 [ $combi->({ PATH_INFO => '/?foo=' }) ],
122 [ {}, 'one' ],
123 '/post/one parses out one'
124 );
125}
b0420ad6 126
051b7ee2 127{
128 my $or = $dp->parse_dispatch_specification('GET|POST');
129
130 foreach my $meth (qw(GET POST)) {
131
132 is_deeply(
133 [ $or->({ REQUEST_METHOD => $meth }) ],
134 [ {} ],
135 'GET|POST matches method '.$meth
136 );
137 }
138
139 is_deeply(
140 [ $or->({ REQUEST_METHOD => 'PUT' }) ],
141 [],
142 'GET|POST does not match PUT'
143 );
144}
b0420ad6 145
051b7ee2 146{
147 my $or = $dp->parse_dispatch_specification('GET|POST|DELETE');
148
149 foreach my $meth (qw(GET POST DELETE)) {
150
151 is_deeply(
152 [ $or->({ REQUEST_METHOD => $meth }) ],
153 [ {} ],
154 'GET|POST|DELETE matches method '.$meth
155 );
156 }
157
158 is_deeply(
159 [ $or->({ REQUEST_METHOD => 'PUT' }) ],
160 [],
161 'GET|POST|DELETE does not match PUT'
162 );
163}
b0420ad6 164
051b7ee2 165{
166 my $nest = $dp->parse_dispatch_specification('(GET+/foo)|POST');
167
168 is_deeply(
169 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'GET' }) ],
170 [ {} ],
171 '(GET+/foo)|POST matches GET /foo'
172 );
173
174 is_deeply(
175 [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'GET' }) ],
176 [],
177 '(GET+/foo)|POST does not match GET /bar'
178 );
179
180 is_deeply(
181 [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'POST' }) ],
182 [ {} ],
183 '(GET+/foo)|POST matches POST /bar'
184 );
185
186 is_deeply(
187 [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'PUT' }) ],
188 [],
189 '(GET+/foo)|POST does not match PUT /foo'
190 );
191}
a4ec359d 192
193{
194 local $@;
195 ok(
196 !eval { $dp->parse_dispatch_specification('/foo+(GET'); 1 },
197 'Death with missing closing )'
198 );
199 my $err = q{
200 /foo+(GET
201 ^
202 };
203 (s/^\n//s,s/\n $//s,s/^ //mg) for $err;
204 like(
205 $@,
206 qr{\Q$err\E},
207 "Error $@ matches\n${err}\n"
208 );
209}
2ee4ab06 210
051b7ee2 211{
212 my $not = $dp->parse_dispatch_specification('!.html+.*');
213
214 is_deeply(
215 [ $not->({ PATH_INFO => '/foo.xml' }) ],
216 [ { PATH_INFO => '/foo' }, 'xml' ],
217 '!.html+.* matches /foo.xml'
218 );
219
220 is_deeply(
221 [ $not->({ PATH_INFO => '/foo.html' }) ],
222 [],
223 '!.html+.* does not match /foo.html'
224 );
225
226 is_deeply(
227 [ $not->({ PATH_INFO => '/foo' }) ],
228 [],
229 '!.html+.* does not match /foo'
230 );
231}
da8429c9 232
051b7ee2 233{
234 my $sub = $dp->parse_dispatch_specification('/foo/*/...');
235
236 is_deeply(
237 [ $sub->({ PATH_INFO => '/foo/1/bar' }) ],
238 [ { PATH_INFO => '/bar' }, 1 ],
239 '/foo/*/... matches /foo/1/bar and strips to /bar'
240 );
241
242 is_deeply(
243 [ $sub->({ PATH_INFO => '/foo/1/' }) ],
244 [ { PATH_INFO => '/' }, 1 ],
245 '/foo/*/... matches /foo/1/bar and strips to /'
246 );
247
248 is_deeply(
249 [ $sub->({ PATH_INFO => '/foo/1' }) ],
250 [],
251 '/foo/*/... does not match /foo/1 (no trailing /)'
252 );
253}
a5917caa 254
255my $q = 'foo=FOO&bar=BAR1&baz=one+two&quux=QUUX1&quux=QUUX2'
256 .'&bar=BAR2&quux=QUUX3&evil=%2F';
257
258my %all_single = (
259 foo => 'FOO',
260 bar => 'BAR2',
261 baz => 'one two',
262 quux => 'QUUX3',
263 evil => '/',
264);
265
266my %all_multi = (
267 foo => [ 'FOO' ],
268 bar => [ qw(BAR1 BAR2) ],
269 baz => [ 'one two' ],
270 quux => [ qw(QUUX1 QUUX2 QUUX3) ],
271 evil => [ '/' ],
272);
273
eb9e0e25 274foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') {
275 my $foo = $dp->parse_dispatch_specification($lose);
a5917caa 276
eb9e0e25 277 is_deeply(
278 [ $foo->({ QUERY_STRING => '' }) ],
279 [],
280 "${lose} fails with no query"
281 );
282
283 is_deeply(
284 [ $foo->({ QUERY_STRING => 'bar=baz' }) ],
285 [],
286 "${lose} fails with query missing foo key"
287 );
288}
a5917caa 289
290foreach my $win (
eb9e0e25 291 [ '?foo=' => 'FOO' ],
292 [ '?:foo=' => { foo => 'FOO' } ],
293 [ '?spoo~' => undef ],
294 [ '?:spoo~' => {} ],
295 [ '?@spoo~' => [] ],
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 => '/' } ],
a5917caa 305 [ '?*' => \%all_single ],
306 [ '?@*' => \%all_multi ],
eb9e0e25 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) ] } ],
a5917caa 310) {
eb9e0e25 311 my ($spec, @res) = @$win;
a5917caa 312 my $match = $dp->parse_dispatch_specification($spec);
313#use Data::Dump::Streamer; warn Dump($match);
314 is_deeply(
315 [ $match->({ QUERY_STRING => $q }) ],
eb9e0e25 316 [ {}, @res ],
a5917caa 317 "${spec} matches correctly"
318 );
319}