Debian Codegraph @

t/unit/core/route.t

raw
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Fatal;
6
7 use Dancer2::Core::Route;
8 use Dancer2::Core::Request;
9
10 # Build a request whose path is $path. Extra pairs are merged into the PSGI
11 # env, which is how we set headers (HTTP_USER_AGENT, CONTENT_TYPE, ...).
12 sub request_for {
13 my ( $path, %env ) = @_;
14 return Dancer2::Core::Request->new(
15 env => {
16 REQUEST_METHOD => 'GET',
17 PATH_INFO => $path,
18 'psgi.url_scheme' => 'http',
19 HTTP_HOST => 'localhost',
20 %env,
21 },
22 );
23 }
24
25 sub route_for {
26 return Dancer2::Core::Route->new( method => 'get', code => sub {1}, @_ );
27 }
28
29 # match() returns the captured data as a hashref on success (an empty hashref
30 # for a route with nothing to capture) and undef when the path doesn't match.
31 sub match_for {
32 my ( $route, $path, %env ) = @_;
33 return scalar $route->match( request_for( $path, %env ) );
34 }
35
36 subtest 'a static path matches itself and nothing else' => sub {
37 my $route = route_for( regexp => '/about' );
38
39 is_deeply( match_for( $route, '/about' ), {}, '/about matches, captures nothing' );
40 is( match_for( $route, '/about/us' ), undef, '/about does not match /about/us' );
41 is( match_for( $route, '/aboutus' ), undef, '/about does not match /aboutus' );
42 };
43
44 subtest 'a leading slash is added when the pattern omits one' => sub {
45 my $route = route_for( regexp => 'noslash' );
46
47 is_deeply( match_for( $route, '/noslash' ), {},
48 q{'noslash' is treated as '/noslash'} );
49 };
50
51 subtest 'a :token captures one path segment and stops at the slash' => sub {
52 my $route = route_for( regexp => '/user/:id' );
53
54 is_deeply( match_for( $route, '/user/42' ), { id => '42' },
55 'the segment is captured under the token name' );
56
57 # This is the assertion that fails if the token pattern ever becomes
58 # greedy enough to swallow a '/'.
59 is( match_for( $route, '/user/1/2' ), undef,
60 'a token does not capture across a slash' );
61
62 is( match_for( $route, '/user/' ), undef,
63 'a token requires at least one character' );
64 is( match_for( $route, '/user' ), undef, 'the segment is not optional' );
65 };
66
67 subtest 'a trailing ? makes a token optional' => sub {
68 my $route = route_for( regexp => '/o/:id?' );
69
70 is_deeply( match_for( $route, '/o/9' ), { id => '9' },
71 'the token still captures when present' );
72 is_deeply( match_for( $route, '/o/' ), { id => undef },
73 'the token is undef when absent, and the route still matches' );
74 };
75
76 subtest 'a dot in the pattern is a literal dot, not a regex wildcard' => sub {
77 my $route = route_for( regexp => '/file.txt' );
78
79 is_deeply( match_for( $route, '/file.txt' ), {}, 'the literal path matches' );
80
81 # This is the assertion that fails if '.' stops being escaped.
82 is( match_for( $route, '/fileXtxt' ), undef,
83 'the dot does not match an arbitrary character' );
84 };
85
86 subtest 'a * captures one segment into splat' => sub {
87 my $route = route_for( regexp => '/a/*/b/*' );
88
89 is_deeply(
90 match_for( $route, '/a/one/b/two' ),
91 { splat => [ 'one', 'two' ] },
92 'each wildcard contributes one value, in order',
93 );
94 is( match_for( $route, '/a/one/two/b/three' ), undef,
95 'a single wildcard does not span a slash' );
96 };
97
98 subtest 'a ** captures the rest of the path, split on slashes' => sub {
99 my $route = route_for( regexp => '/files/**' );
100
101 # The megasplat value is an arrayref *inside* splat: splat holds one entry
102 # per wildcard, and this wildcard's entry is the list of segments.
103 is_deeply(
104 match_for( $route, '/files/a/b/c' ),
105 { splat => [ [ 'a', 'b', 'c' ] ] },
106 'the remaining path is split into segments',
107 );
108 is_deeply(
109 match_for( $route, '/files/a' ),
110 { splat => [ ['a'] ] },
111 'a single segment still arrives as a one-element list',
112 );
113 };
114
115 subtest 'a typed token rejects a value of the wrong type' => sub {
116 my $route = route_for( regexp => '/n/:id[Int]' );
117
118 is_deeply( match_for( $route, '/n/42' ), { id => '42' },
119 'an integer is captured' );
120
121 # This is the assertion that fails if the type check is dropped: without
122 # it, '/n/abc' matches the same shape as '/n/42'.
123 is( match_for( $route, '/n/abc' ), undef,
124 'a non-integer does not match at all' );
125 };
126
127 subtest 'a prefix is prepended to the pattern' => sub {
128 my $route = route_for( regexp => '/list', prefix => '/api' );
129
130 is( $route->spec_route, '/api/list', 'spec_route records the prefixed path' );
131 is_deeply( match_for( $route, '/api/list' ), {}, 'the prefixed path matches' );
132 is( match_for( $route, '/list' ), undef, 'the unprefixed path does not' );
133 };
134
135 subtest 'a regexp route returns named captures under captures' => sub {
136 my $route = route_for( regexp => qr{^/x/(?<who>\w+)$} );
137
138 is_deeply(
139 match_for( $route, '/x/bob' ),
140 { captures => { who => 'bob' } },
141 'named captures are grouped under the captures key',
142 );
143 is( match_for( $route, '/y/bob' ), undef, 'a non-matching path returns undef' );
144 };
145
146 subtest 'the deprecated placeholder names are refused at build time' => sub {
147 like(
148 exception { route_for( regexp => '/a/:splat' ) },
149 qr/Named placeholder 'splat' is deprecated/,
150 q{:splat dies rather than being silently accepted},
151 );
152 like(
153 exception { route_for( regexp => '/a/:captures' ) },
154 qr/Named placeholder 'captures' is deprecated/,
155 q{:captures dies rather than being silently accepted},
156 );
157 };
158
159 subtest 'an unrecognised matching option is refused at build time' => sub {
160 like(
161 exception { route_for( regexp => '/a', options => { bogus => 1 } ) },
162 qr/Not a valid option for route matching: `bogus'/,
163 'a typo in an option name dies instead of being ignored',
164 );
165
166 is(
167 exception { route_for( regexp => '/a', options => { agent => 'x' } ) },
168 undef,
169 'a supported option name is accepted',
170 );
171 };
172
173 subtest 'route options gate the match on request properties' => sub {
174 my $route = route_for( regexp => '/only', options => { agent => 'Firefox' } );
175
176 # The option value is used as a pattern, not compared for equality.
177 is_deeply(
178 match_for( $route, '/only', HTTP_USER_AGENT => 'Mozilla Firefox 3' ),
179 {},
180 'the option matches anywhere within the header value',
181 );
182 is( match_for( $route, '/only', HTTP_USER_AGENT => 'Chrome' ), undef,
183 'a non-matching agent blocks the route' );
184 is( match_for( $route, '/only' ), undef,
185 'a missing agent header blocks the route' );
186
187 my $typed = route_for(
188 regexp => '/ct',
189 options => { content_type => 'application/json' },
190 );
191 is_deeply( match_for( $typed, '/ct', CONTENT_TYPE => 'application/json' ), {},
192 'a matching content type is allowed through' );
193 is( match_for( $typed, '/ct', CONTENT_TYPE => 'text/html' ), undef,
194 'a non-matching content type blocks the route' );
195 };
196
197 done_testing();