dispatch parser
[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 $slash = $dp->parse_dispatch_specification('/');
44
45 is_deeply(
46   [ $slash->({ PATH_INFO => '/' }) ],
47   [ {} ],
48   '/ matches /'
49 );
50
51 is_deeply(
52   [ $slash->({ PATH_INFO => '/foo' }) ],
53   [ ],
54   '/foo does not match /'
55 );
56
57 my $post = $dp->parse_dispatch_specification('/post/*');
58
59 is_deeply(
60   [ $post->({ PATH_INFO => '/post/one' }) ],
61   [ {}, 'one' ],
62   '/post/one parses out one'
63 );
64
65 is_deeply(
66   [ $post->({ PATH_INFO => '/post/one/' }) ],
67   [],
68   '/post/one/ does not match'
69 );
70
71 my $combi = $dp->parse_dispatch_specification('GET /post/*');
72
73 is_deeply(
74   [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'GET' }) ],
75   [ {}, 'one' ],
76   '/post/one parses out one'
77 );
78
79 is_deeply(
80   [ $combi->({ PATH_INFO => '/post/one/', REQUEST_METHOD => 'GET' }) ],
81   [],
82   '/post/one/ does not match'
83 );
84
85 is_deeply(
86   [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'POST' }) ],
87   [],
88   'POST /post/one does not match'
89 );