implement ()
[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 my $get = $dp->parse_dispatch_specification('GET');
11
12 is_deeply(
13   [ $get->({ REQUEST_METHOD => 'GET' }) ],
14   [ {} ],
15   'GET matches'
16 );
17
18 is_deeply(
19   [ $get->({ REQUEST_METHOD => 'POST' }) ],
20   [],
21   'POST does not match'
22 );
23
24 ok(
25   !eval { $dp->parse_dispatch_specification('GET POST'); 1; },
26   "Don't yet allow two methods"
27 );
28
29 my $html = $dp->parse_dispatch_specification('.html');
30
31 is_deeply(
32   [ $html->({ PATH_INFO => '/foo/bar.html' }) ],
33   [ { PATH_INFO => '/foo/bar' } ],
34   '.html matches'
35 );
36
37 is_deeply(
38   [ $html->({ PATH_INFO => '/foo/bar.xml' }) ],
39   [],
40   '.xml does not match .html'
41 );
42
43 my $any_ext = $dp->parse_dispatch_specification('.*');
44
45 is_deeply(
46   [ $any_ext->({ PATH_INFO => '/foo/bar.html' }) ],
47   [ { PATH_INFO => '/foo/bar' }, 'html' ],
48   '.html matches .* and extension returned'
49 );
50
51 is_deeply(
52   [ $any_ext->({ PATH_INFO => '/foo/bar' }) ],
53   [],
54   'no extension does not match .*'
55 );
56
57
58 my $slash = $dp->parse_dispatch_specification('/');
59
60 is_deeply(
61   [ $slash->({ PATH_INFO => '/' }) ],
62   [ {} ],
63   '/ matches /'
64 );
65
66 is_deeply(
67   [ $slash->({ PATH_INFO => '/foo' }) ],
68   [ ],
69   '/foo does not match /'
70 );
71
72 my $post = $dp->parse_dispatch_specification('/post/*');
73
74 is_deeply(
75   [ $post->({ PATH_INFO => '/post/one' }) ],
76   [ {}, 'one' ],
77   '/post/one parses out one'
78 );
79
80 is_deeply(
81   [ $post->({ PATH_INFO => '/post/one/' }) ],
82   [],
83   '/post/one/ does not match'
84 );
85
86 my $combi = $dp->parse_dispatch_specification('GET+/post/*');
87
88 is_deeply(
89   [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'GET' }) ],
90   [ {}, 'one' ],
91   '/post/one parses out one'
92 );
93
94 is_deeply(
95   [ $combi->({ PATH_INFO => '/post/one/', REQUEST_METHOD => 'GET' }) ],
96   [],
97   '/post/one/ does not match'
98 );
99
100 is_deeply(
101   [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'POST' }) ],
102   [],
103   'POST /post/one does not match'
104 );
105
106 my $or = $dp->parse_dispatch_specification('GET|POST');
107
108 foreach my $meth (qw(GET POST)) {
109
110   is_deeply(
111     [ $or->({ REQUEST_METHOD => $meth }) ],
112     [ {} ],
113     'GET|POST matches method '.$meth
114   );
115 }
116
117 is_deeply(
118   [ $or->({ REQUEST_METHOD => 'PUT' }) ],
119   [],
120   'GET|POST does not match PUT'
121 );
122
123 $or = $dp->parse_dispatch_specification('GET|POST|DELETE');
124
125 foreach my $meth (qw(GET POST DELETE)) {
126
127   is_deeply(
128     [ $or->({ REQUEST_METHOD => $meth }) ],
129     [ {} ],
130     'GET|POST|DELETE matches method '.$meth
131   );
132 }
133
134 is_deeply(
135   [ $or->({ REQUEST_METHOD => 'PUT' }) ],
136   [],
137   'GET|POST|DELETE does not match PUT'
138 );
139
140 my $nest = $dp->parse_dispatch_specification('(GET+/foo)|POST');
141
142 is_deeply(
143   [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'GET' }) ],
144   [ {} ],
145   '(GET+/foo)|POST matches GET /foo'
146 );
147
148 is_deeply(
149   [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'GET' }) ],
150   [],
151   '(GET+/foo)|POST does not match GET /bar'
152 );
153
154 is_deeply(
155   [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => 'POST' }) ],
156   [ {} ],
157   '(GET+/foo)|POST matches POST /bar'
158 );
159
160 is_deeply(
161   [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'PUT' }) ],
162   [],
163   '(GET+/foo)|POST does not match PUT /foo'
164 );