334ceab8d81acb57b9a0a3c20dc2eec9ffa03b94
[catagits/Web-Simple.git] / t / dispatch_parser.t
1 use strict;
2 use warnings FATAL => 'all';
3
4 use Test::More qw(no_plan);
5
6 use Web::Simple::DispatchParser;
7
8 my $dp = Web::Simple::DispatchParser->new;
9
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 }
25
26 ok(
27   !eval { $dp->parse_dispatch_specification('GET POST'); 1; },
28   "Don't yet allow two methods"
29 );
30
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    );
45 }
46
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 }
62
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 }
78
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    );
93 }
94
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 }
116
117 {
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 }
135
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 }
154
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 }
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 }
200
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 }
222
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 }
244
245 my $q = 'foo=FOO&bar=BAR1&baz=one+two&quux=QUUX1&quux=QUUX2'
246         .'&bar=BAR2&quux=QUUX3&evil=%2F';
247
248 my %all_single = (
249   foo => 'FOO',
250   bar => 'BAR2',
251   baz => 'one two',
252   quux => 'QUUX3',
253   evil => '/',
254 );
255
256 my %all_multi = (
257   foo => [ 'FOO' ],
258   bar => [ qw(BAR1 BAR2) ],
259   baz => [ 'one two' ],
260   quux => [ qw(QUUX1 QUUX2 QUUX3) ],
261   evil => [ '/' ],
262 );
263
264 foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') {
265   my $foo = $dp->parse_dispatch_specification($lose);
266
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 }
279
280 foreach my $win (
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 => '/' } ],
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) ] } ],
300 ) {
301   my ($spec, @res) = @$win;
302   my $match = $dp->parse_dispatch_specification($spec);
303 #use Data::Dump::Streamer; warn Dump($match);
304   is_deeply(
305     [ $match->({ QUERY_STRING => $q }) ],
306     [ {}, @res ],
307     "${spec} matches correctly"
308   );
309 }