implement | dispatch combinator
[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
10my $get = $dp->parse_dispatch_specification('GET');
11
12is_deeply(
13 [ $get->({ REQUEST_METHOD => 'GET' }) ],
14 [ {} ],
15 'GET matches'
16);
17
18is_deeply(
19 [ $get->({ REQUEST_METHOD => 'POST' }) ],
20 [],
21 'POST does not match'
22);
23
24ok(
25 !eval { $dp->parse_dispatch_specification('GET POST'); 1; },
26 "Don't yet allow two methods"
27);
28
29my $html = $dp->parse_dispatch_specification('.html');
30
31is_deeply(
32 [ $html->({ PATH_INFO => '/foo/bar.html' }) ],
33 [ { PATH_INFO => '/foo/bar' } ],
34 '.html matches'
35);
36
37is_deeply(
38 [ $html->({ PATH_INFO => '/foo/bar.xml' }) ],
39 [],
40 '.xml does not match .html'
41);
42
c6ea9542 43my $any_ext = $dp->parse_dispatch_specification('.*');
44
45is_deeply(
46 [ $any_ext->({ PATH_INFO => '/foo/bar.html' }) ],
47 [ { PATH_INFO => '/foo/bar' }, 'html' ],
48 '.html matches .* and extension returned'
49);
50
51is_deeply(
52 [ $any_ext->({ PATH_INFO => '/foo/bar' }) ],
53 [],
54 'no extension does not match .*'
55);
56
57
920d6222 58my $slash = $dp->parse_dispatch_specification('/');
59
60is_deeply(
61 [ $slash->({ PATH_INFO => '/' }) ],
62 [ {} ],
63 '/ matches /'
64);
65
66is_deeply(
67 [ $slash->({ PATH_INFO => '/foo' }) ],
68 [ ],
69 '/foo does not match /'
70);
71
72my $post = $dp->parse_dispatch_specification('/post/*');
73
74is_deeply(
75 [ $post->({ PATH_INFO => '/post/one' }) ],
76 [ {}, 'one' ],
77 '/post/one parses out one'
78);
79
80is_deeply(
81 [ $post->({ PATH_INFO => '/post/one/' }) ],
82 [],
83 '/post/one/ does not match'
84);
85
9e4713ab 86my $combi = $dp->parse_dispatch_specification('GET+/post/*');
920d6222 87
88is_deeply(
89 [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'GET' }) ],
90 [ {}, 'one' ],
91 '/post/one parses out one'
92);
93
94is_deeply(
95 [ $combi->({ PATH_INFO => '/post/one/', REQUEST_METHOD => 'GET' }) ],
96 [],
97 '/post/one/ does not match'
98);
99
100is_deeply(
101 [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'POST' }) ],
102 [],
103 'POST /post/one does not match'
104);
c6ea9542 105
106my $or = $dp->parse_dispatch_specification('GET|POST');
107
108foreach my $meth (qw(GET POST)) {
109
110 is_deeply(
111 [ $or->({ REQUEST_METHOD => $meth }) ],
112 [ {} ],
113 'GET|POST matches method '.$meth
114 );
115}
116
117is_deeply(
118 [ $or->({ REQUEST_METHOD => 'PUT' }) ],
119 [],
120 'GET|POST does not match PUT'
121);