remove erroneous dispatch test
[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{
051b7ee2 118 my $or = $dp->parse_dispatch_specification('GET|POST');
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{
137 my $or = $dp->parse_dispatch_specification('GET|POST|DELETE');
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{
156 my $nest = $dp->parse_dispatch_specification('(GET+/foo)|POST');
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(
186 !eval { $dp->parse_dispatch_specification('/foo+(GET'); 1 },
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{
202 my $not = $dp->parse_dispatch_specification('!.html+.*');
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{
224 my $sub = $dp->parse_dispatch_specification('/foo/*/...');
225
226 is_deeply(
227 [ $sub->({ PATH_INFO => '/foo/1/bar' }) ],
228 [ { PATH_INFO => '/bar' }, 1 ],
229 '/foo/*/... matches /foo/1/bar and strips to /bar'
230 );
231
232 is_deeply(
233 [ $sub->({ PATH_INFO => '/foo/1/' }) ],
234 [ { PATH_INFO => '/' }, 1 ],
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
245my $q = 'foo=FOO&bar=BAR1&baz=one+two&quux=QUUX1&quux=QUUX2'
246 .'&bar=BAR2&quux=QUUX3&evil=%2F';
247
248my %all_single = (
249 foo => 'FOO',
250 bar => 'BAR2',
251 baz => 'one two',
252 quux => 'QUUX3',
253 evil => '/',
254);
255
256my %all_multi = (
257 foo => [ 'FOO' ],
258 bar => [ qw(BAR1 BAR2) ],
259 baz => [ 'one two' ],
260 quux => [ qw(QUUX1 QUUX2 QUUX3) ],
261 evil => [ '/' ],
262);
263
eb9e0e25 264foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') {
265 my $foo = $dp->parse_dispatch_specification($lose);
a5917caa 266
eb9e0e25 267 is_deeply(
268 [ $foo->({ QUERY_STRING => '' }) ],
269 [],
270 "${lose} fails with no query"
271 );
272
273 is_deeply(
274 [ $foo->({ QUERY_STRING => 'bar=baz' }) ],
275 [],
276 "${lose} fails with query missing foo key"
277 );
278}
a5917caa 279
280foreach my $win (
eb9e0e25 281 [ '?foo=' => 'FOO' ],
282 [ '?:foo=' => { foo => 'FOO' } ],
283 [ '?spoo~' => undef ],
284 [ '?:spoo~' => {} ],
285 [ '?@spoo~' => [] ],
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 => '/' } ],
a5917caa 295 [ '?*' => \%all_single ],
296 [ '?@*' => \%all_multi ],
eb9e0e25 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) ] } ],
a5917caa 300) {
eb9e0e25 301 my ($spec, @res) = @$win;
a5917caa 302 my $match = $dp->parse_dispatch_specification($spec);
303#use Data::Dump::Streamer; warn Dump($match);
304 is_deeply(
305 [ $match->({ QUERY_STRING => $q }) ],
eb9e0e25 306 [ {}, @res ],
a5917caa 307 "${spec} matches correctly"
308 );
309}