| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
|
| 4 |
use Test::More; |
| 5 |
use Test::Fatal qw<exception>; |
| 6 |
use Encode (); |
| 7 |
|
| 8 |
use Dancer2::Core::Request; |
| 9 |
|
| 10 |
# Unit tests: request objects are built straight from a PSGI environment, with |
| 11 |
# no app and no dispatcher, so a failure here names the request layer rather |
| 12 |
# than something further up. |
| 13 |
# |
| 14 |
# Two accessor families coexist and do not agree, deliberately: |
| 15 |
# |
| 16 |
# params() - the legacy flat hash. Repeated values become an |
| 17 |
# arrayref, and it is where splat/captures live. |
| 18 |
# *_parameters() - Hash::MultiValue, per source, plus a merged view. |
| 19 |
# splat and captures are kept out of these. |
| 20 |
# |
| 21 |
# Both are asserted, because "fixing" either to look like the other would be a |
| 22 |
# silent behavior change for existing applications. |
| 23 |
|
| 24 |
# A minimal but complete PSGI env. CONTENT_LENGTH matters: without it |
| 25 |
# Plack::Request will not read the body at all. |
| 26 |
sub psgi_env { |
| 27 |
my %opt = @_; |
| 28 |
my $body = defined $opt{body} ? $opt{body} : ''; |
| 29 |
open my $input, '<', \$body or die "cannot open in-memory body: $!"; |
| 30 |
|
| 31 |
return { |
| 32 |
REQUEST_METHOD => $opt{method} || 'GET', |
| 33 |
PATH_INFO => defined $opt{path} ? $opt{path} : '/', |
| 34 |
QUERY_STRING => defined $opt{query} ? $opt{query} : '', |
| 35 |
SCRIPT_NAME => '', |
| 36 |
SERVER_NAME => 'localhost', |
| 37 |
SERVER_PORT => 80, |
| 38 |
CONTENT_LENGTH => length $body, |
| 39 |
'psgi.url_scheme' => 'http', |
| 40 |
'psgi.input' => $input, |
| 41 |
'psgi.errors' => \*STDERR, |
| 42 |
( $opt{ctype} ? ( CONTENT_TYPE => $opt{ctype} ) : () ), |
| 43 |
%{ $opt{extra} || {} }, |
| 44 |
}; |
| 45 |
} |
| 46 |
|
| 47 |
sub form_request { |
| 48 |
my %opt = @_; |
| 49 |
return Dancer2::Core::Request->new( |
| 50 |
env => psgi_env( %opt, ctype => 'application/x-www-form-urlencoded' ), |
| 51 |
( $opt{strict_utf8} ? ( strict_utf8 => 1 ) : () ), |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
subtest 'route parameters outrank body, body outranks query' => sub { |
| 56 |
my $req = form_request( |
| 57 |
method => 'POST', |
| 58 |
query => 'who=query&only_query=q', |
| 59 |
body => 'who=body&only_body=b', |
| 60 |
); |
| 61 |
|
| 62 |
# Before a route matches there are no route parameters, so body wins. |
| 63 |
is( $req->params->{who}, 'body', |
| 64 |
'a body parameter beats a query parameter of the same name' ); |
| 65 |
|
| 66 |
# This is what App::_dispatch does once a route matches. |
| 67 |
$req->_set_route_params( { who => 'route' } ); |
| 68 |
$req->_set_route_parameters( { who => 'route' } ); |
| 69 |
|
| 70 |
is( $req->params->{who}, 'route', |
| 71 |
'a route parameter beats both' ); |
| 72 |
|
| 73 |
# The losers are still reachable through their own source. |
| 74 |
is( $req->params('query')->{who}, 'query', 'the query value is still there' ); |
| 75 |
is( $req->params('body')->{who}, 'body', 'the body value is still there' ); |
| 76 |
is( $req->params('route')->{who}, 'route', 'and so is the route value' ); |
| 77 |
|
| 78 |
# Names that appear in only one place are unaffected by any of this. |
| 79 |
is( $req->params->{only_query}, 'q', 'a query-only name comes through' ); |
| 80 |
is( $req->params->{only_body}, 'b', 'a body-only name comes through' ); |
| 81 |
|
| 82 |
like( exception { $req->params('nonsense') }, |
| 83 |
qr/Unknown source params/, |
| 84 |
'an unknown source is refused rather than silently returning nothing' ); |
| 85 |
|
| 86 |
# The merged Hash::MultiValue keeps all three, in query-body-route order. |
| 87 |
is_deeply( |
| 88 |
[ $req->parameters->get_all('who') ], |
| 89 |
[ 'query', 'body', 'route' ], |
| 90 |
'the merged view keeps every value, query first and route last', |
| 91 |
); |
| 92 |
is( scalar $req->parameters->get('who'), 'route', |
| 93 |
'and its single-value read gives the last one, so route still wins' ); |
| 94 |
}; |
| 95 |
|
| 96 |
subtest 'a repeated query parameter keeps every value' => sub { |
| 97 |
my $req = Dancer2::Core::Request->new( |
| 98 |
env => psgi_env( query => 'a=1&a=2&a=3&b=solo' ) ); |
| 99 |
|
| 100 |
is_deeply( $req->params->{a}, [ '1', '2', '3' ], |
| 101 |
'params() collapses a repeated name to an arrayref of all values' ); |
| 102 |
is( $req->params->{b}, 'solo', |
| 103 |
'a single value stays a plain scalar, not a one-element array' ); |
| 104 |
|
| 105 |
is_deeply( [ $req->query_parameters->get_all('a') ], [ '1', '2', '3' ], |
| 106 |
'get_all on the query parameters returns all three' ); |
| 107 |
is( scalar $req->query_parameters->get('a'), '3', |
| 108 |
'and the single-value read gives the last, per Hash::MultiValue' ); |
| 109 |
}; |
| 110 |
|
| 111 |
subtest 'UTF-8 in the query string and the path is decoded to characters' => sub { |
| 112 |
# 'été' and '/café' as the bytes a client would actually send. |
| 113 |
my $req = Dancer2::Core::Request->new( env => psgi_env( |
| 114 |
query => 'name=%C3%A9t%C3%A9', |
| 115 |
path => "/caf\xc3\xa9", |
| 116 |
) ); |
| 117 |
|
| 118 |
my $name = $req->params->{name}; |
| 119 |
ok( utf8::is_utf8($name), 'the query value is decoded, not raw bytes' ); |
| 120 |
is( length $name, 3, 'and is three characters, not five bytes' ); |
| 121 |
is( Encode::encode_utf8($name), "\xc3\xa9t\xc3\xa9", |
| 122 |
'round-tripping it reproduces the original bytes' ); |
| 123 |
|
| 124 |
my $path = $req->path; |
| 125 |
ok( utf8::is_utf8($path), 'the path is decoded too' ); |
| 126 |
is( length $path, 5, 'and is five characters, not six bytes' ); |
| 127 |
|
| 128 |
my $via_hmv = scalar $req->query_parameters->get('name'); |
| 129 |
ok( utf8::is_utf8($via_hmv), |
| 130 |
'the Hash::MultiValue accessor decodes as well, not only params()' ); |
| 131 |
is( $via_hmv, $name, 'and agrees with params()' ); |
| 132 |
}; |
| 133 |
|
| 134 |
subtest 'invalid UTF-8 is passed through with a warning by default' => sub { |
| 135 |
my $req = Dancer2::Core::Request->new( env => psgi_env( query => 'x=%FF%FE' ) ); |
| 136 |
|
| 137 |
my @warnings; |
| 138 |
my $value = do { |
| 139 |
local $SIG{__WARN__} = sub { push @warnings, $_[0] }; |
| 140 |
$req->params->{x}; |
| 141 |
}; |
| 142 |
|
| 143 |
# Lenient mode: the request survives and the caller gets the bytes. |
| 144 |
ok( !utf8::is_utf8($value), 'the value is left as bytes, undecoded' ); |
| 145 |
is( $value, "\xFF\xFE", 'and the bytes are unchanged' ); |
| 146 |
|
| 147 |
is( scalar @warnings, 1, 'exactly one warning is emitted' ); |
| 148 |
like( $warnings[0], qr/Invalid UTF-8 in query parameters/, |
| 149 |
'naming what was invalid and where' ); |
| 150 |
like( $warnings[0], qr/leaving bytes unchanged/, |
| 151 |
'and saying what it did about it' ); |
| 152 |
}; |
| 153 |
|
| 154 |
subtest 'invalid UTF-8 goes to the PSGI logger when there is one' => sub { |
| 155 |
my @logged; |
| 156 |
my $req = Dancer2::Core::Request->new( env => psgi_env( |
| 157 |
query => 'x=%FF%FE', |
| 158 |
extra => { 'psgix.logger' => sub { push @logged, $_[0] } }, |
| 159 |
) ); |
| 160 |
|
| 161 |
my @warnings; |
| 162 |
my $value = do { |
| 163 |
local $SIG{__WARN__} = sub { push @warnings, $_[0] }; |
| 164 |
$req->params->{x}; |
| 165 |
}; |
| 166 |
|
| 167 |
is( scalar @logged, 1, 'the message goes to the PSGI logger' ); |
| 168 |
is( $logged[0]{level}, 'warning', 'at warning level' ); |
| 169 |
like( $logged[0]{message}, qr/Invalid UTF-8 in query parameters/, |
| 170 |
'with the same message' ); |
| 171 |
|
| 172 |
# This is the point of the branch: a server that provides a logger does |
| 173 |
# not also get warnings on STDERR. |
| 174 |
is( scalar @warnings, 0, 'and not also to STDERR' ); |
| 175 |
is( $value, "\xFF\xFE", 'the bytes still come through unchanged' ); |
| 176 |
}; |
| 177 |
|
| 178 |
subtest 'invalid UTF-8 is fatal when strict_utf8 is on' => sub { |
| 179 |
my $req = Dancer2::Core::Request->new( |
| 180 |
env => psgi_env( query => 'x=%FF%FE' ), |
| 181 |
strict_utf8 => 1, |
| 182 |
); |
| 183 |
|
| 184 |
my $err = exception { $req->params->{x} }; |
| 185 |
ok( defined $err, 'reading the parameter throws' ); |
| 186 |
like( $err, qr/Invalid UTF-8 in query parameters/, |
| 187 |
'naming what was invalid and where' ); |
| 188 |
unlike( $err, qr/leaving bytes unchanged/, |
| 189 |
'and not claiming to have carried on' ); |
| 190 |
|
| 191 |
# Valid UTF-8 is unaffected by strict mode. |
| 192 |
my $ok = Dancer2::Core::Request->new( |
| 193 |
env => psgi_env( query => 'x=%C3%A9' ), |
| 194 |
strict_utf8 => 1, |
| 195 |
); |
| 196 |
is( $ok->params->{x}, "\x{e9}", |
| 197 |
'valid UTF-8 still decodes normally under strict_utf8' ); |
| 198 |
}; |
| 199 |
|
| 200 |
subtest 'the XS and pure-Perl query parsers agree' => sub { |
| 201 |
# This distribution must stay usable with no XS modules installed |
| 202 |
# (CLAUDE.md: "core must stay pure-Perl-loadable"), so both parsers have |
| 203 |
# to produce the same result. Whichever one is active on this machine, |
| 204 |
# the other is still exercised here. |
| 205 |
# |
| 206 |
# CGI::Deurl::XS is optional. Forcing $XS_PARSE_QUERY_STRING on below |
| 207 |
# calls CGI::Deurl::XS::parse_query_string() directly, which dies with |
| 208 |
# "Undefined subroutine" if the module was never loaded - so skip |
| 209 |
# outright rather than let that take the rest of the file down with it. |
| 210 |
unless ( eval { require CGI::Deurl::XS; 1 } ) { |
| 211 |
plan skip_all => 'CGI::Deurl::XS is not installed'; |
| 212 |
} |
| 213 |
|
| 214 |
my $query = 'a=1&a=2&plus=a+b&utf=%C3%A9&empty=&solo=x'; |
| 215 |
|
| 216 |
my %result; |
| 217 |
for my $mode ( [ xs => 1 ], [ pp => 0 ] ) { |
| 218 |
my ( $label, $on ) = @$mode; |
| 219 |
|
| 220 |
no warnings 'once'; |
| 221 |
local $Dancer2::Core::Request::XS_PARSE_QUERY_STRING = $on; |
| 222 |
local $Dancer2::Core::Request::XS_URL_DECODE = $on; |
| 223 |
|
| 224 |
my $req = Dancer2::Core::Request->new( env => psgi_env( query => $query ) ); |
| 225 |
$result{$label} = $req->params; |
| 226 |
} |
| 227 |
|
| 228 |
is_deeply( $result{pp}, $result{xs}, |
| 229 |
'both parsers produce identical parameters for the same query string' ); |
| 230 |
|
| 231 |
# Spot-check the things a hand-rolled parser gets wrong, so this is not |
| 232 |
# just "two broken parsers agree". |
| 233 |
for my $label ( 'xs', 'pp' ) { |
| 234 |
my $p = $result{$label}; |
| 235 |
is_deeply( $p->{a}, [ '1', '2' ], "$label: repeated name becomes a list" ); |
| 236 |
is( $p->{plus}, 'a b', "$label: '+' decodes to a space" ); |
| 237 |
is( $p->{utf}, "\x{e9}", "$label: percent-encoded UTF-8 decodes" ); |
| 238 |
is( $p->{empty}, '', "$label: a name with no value is the empty string" ); |
| 239 |
is( $p->{solo}, 'x', "$label: an ordinary pair survives" ); |
| 240 |
} |
| 241 |
}; |
| 242 |
|
| 243 |
subtest 'splat and captures stay out of route_parameters' => sub { |
| 244 |
my $req = Dancer2::Core::Request->new( env => psgi_env( query => 'q=1' ) ); |
| 245 |
|
| 246 |
# App::_dispatch passes the *same* hashref to both setters, in this order, |
| 247 |
# and the second one deletes splat/captures from it. So the order is |
| 248 |
# load-bearing: reversed, splat() below would come back empty. |
| 249 |
my $match = { id => '42', splat => [ 'a', 'b' ], captures => { name => 'x' } }; |
| 250 |
$req->_set_route_params($match); |
| 251 |
$req->_set_route_parameters($match); |
| 252 |
|
| 253 |
# The legacy hash keeps them - that is how splat() and captures() work. |
| 254 |
is_deeply( [ $req->splat ], [ 'a', 'b' ], 'splat() returns the captures' ); |
| 255 |
is_deeply( $req->captures, { name => 'x' }, 'captures() returns the named ones' ); |
| 256 |
|
| 257 |
# The Hash::MultiValue accessors must not carry them, or an application |
| 258 |
# iterating its route parameters would see reserved names as user data. |
| 259 |
is_deeply( [ sort $req->route_parameters->keys ], ['id'], |
| 260 |
'route_parameters holds only the real named parameter' ); |
| 261 |
is_deeply( [ sort $req->parameters->keys ], [ 'id', 'q' ], |
| 262 |
'and neither does the merged view' ); |
| 263 |
|
| 264 |
is( $req->route_parameters->get('id'), '42', |
| 265 |
'the named route parameter is still readable' ); |
| 266 |
}; |
| 267 |
|
| 268 |
subtest 'a forwarded request keeps its decoded body parameters' => sub { |
| 269 |
my $req = form_request( |
| 270 |
method => 'POST', |
| 271 |
query => 'from=query', |
| 272 |
body => 'name=%C3%A9t%C3%A9&keep=yes', |
| 273 |
); |
| 274 |
|
| 275 |
my $before = $req->body_parameters->get('name'); |
| 276 |
ok( utf8::is_utf8($before), 'the original body parameter is decoded' ); |
| 277 |
|
| 278 |
# What App::make_forward_to does: clone with extra params and a new path. |
| 279 |
my $clone = $req->_shallow_clone( { added => 'new' }, { PATH_INFO => '/dst' } ); |
| 280 |
|
| 281 |
# The regression this guards (GH#1116, GH#1269): the clone used to lose |
| 282 |
# already-decoded body parameters, or decode them a second time. |
| 283 |
my $after = $clone->body_parameters->get('name'); |
| 284 |
ok( utf8::is_utf8($after), 'the clone still has it decoded' ); |
| 285 |
is( $after, $before, 'with the same value, not double-decoded' ); |
| 286 |
is_deeply( [ sort $clone->body_parameters->keys ], [ 'keep', 'name' ], |
| 287 |
'and every body parameter came across' ); |
| 288 |
|
| 289 |
# The legacy per-source view is carried by its own line in _shallow_clone, |
| 290 |
# separate from both the Hash::MultiValue clone above and the merged |
| 291 |
# params below. Dropping it leaves params('body') empty while those two |
| 292 |
# still look right, so it needs asserting in its own right. |
| 293 |
my $clone_body = $clone->params('body'); |
| 294 |
is_deeply( [ sort keys %$clone_body ], [ 'keep', 'name' ], |
| 295 |
"params('body') on the clone still lists every body parameter" ); |
| 296 |
is( $clone_body->{keep}, 'yes', "and their values" ); |
| 297 |
is( $clone_body->{name}, $before, |
| 298 |
"still decoded, matching the original request" ); |
| 299 |
|
| 300 |
is( $clone->params->{keep}, 'yes', 'body parameters are in the clone params' ); |
| 301 |
is( $clone->params->{from}, 'query', 'so are query parameters' ); |
| 302 |
is( $clone->params->{added}, 'new', 'and the parameters added at forward time' ); |
| 303 |
is( $clone->query_parameters->get('added'), 'new', |
| 304 |
'the added parameters also reach query_parameters' ); |
| 305 |
|
| 306 |
is( $clone->env->{PATH_INFO}, '/dst', 'the clone has the new path' ); |
| 307 |
is( $req->env->{PATH_INFO}, '/', |
| 308 |
'and the original request is not modified by the clone' ); |
| 309 |
}; |
| 310 |
|
| 311 |
done_testing(); |