| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More tests => 10; |
| 4 |
use Test::Fatal; |
| 5 |
use Plack::Test; |
| 6 |
use HTTP::Request::Common; |
| 7 |
use Plack::Builder; |
| 8 |
use URI::Escape; |
| 9 |
|
| 10 |
BEGIN { use_ok('Dancer2::Core::Request') } |
| 11 |
|
| 12 |
sub psgi_ok { [ 200, [], ['OK'] ] } |
| 13 |
|
| 14 |
sub test_get_params { |
| 15 |
my %exp_params = ( |
| 16 |
'name' => 'Alexis Sukrieh', |
| 17 |
'IRC Nickname' => 'sukria', |
| 18 |
'Project' => 'Perl Dancer2', |
| 19 |
'hash' => [ 2, 4 ], |
| 20 |
int1 => 1, |
| 21 |
int2 => 0, |
| 22 |
); |
| 23 |
|
| 24 |
my $test = Plack::Test->create( sub { |
| 25 |
my $env = shift; |
| 26 |
my $request = Dancer2::Core::Request->new( env => $env ); |
| 27 |
|
| 28 |
is( $request->path, '/', 'path is set' ); |
| 29 |
is( $request->method, 'GET', 'method is set' ); |
| 30 |
ok( $request->is_get, 'method is GET' ); |
| 31 |
|
| 32 |
is_deeply( |
| 33 |
scalar( $request->params ), |
| 34 |
\%exp_params, |
| 35 |
'params are OK', |
| 36 |
); |
| 37 |
|
| 38 |
is( |
| 39 |
$request->params->{'name'}, |
| 40 |
'Alexis Sukrieh', |
| 41 |
'params accessor works', |
| 42 |
); |
| 43 |
|
| 44 |
my %params = $request->params; |
| 45 |
is_deeply( |
| 46 |
scalar( $request->params ), |
| 47 |
\%params, |
| 48 |
'params wantarray works', |
| 49 |
); |
| 50 |
|
| 51 |
return psgi_ok; |
| 52 |
} ); |
| 53 |
|
| 54 |
my $request_url = '/?' . |
| 55 |
join '&', map {; |
| 56 |
my $param = $_; |
| 57 |
ref $exp_params{$param} |
| 58 |
? map +( uri_escape($param).'='.uri_escape($_) ), @{ $exp_params{$_} } |
| 59 |
: uri_escape($_).'='.uri_escape( $exp_params{$param} ); |
| 60 |
} keys %exp_params; |
| 61 |
|
| 62 |
ok( |
| 63 |
$test->request( GET $request_url )->is_success, |
| 64 |
'Request successful', |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
sub test_post_params { |
| 69 |
my %exp_params = ( |
| 70 |
foo => 'bar', |
| 71 |
name => 'john', |
| 72 |
hash => [ 2, 4, 6 ], |
| 73 |
); |
| 74 |
|
| 75 |
my $test = Plack::Test->create( sub { |
| 76 |
my $env = shift; |
| 77 |
my $request = Dancer2::Core::Request->new( env => $env ); |
| 78 |
|
| 79 |
is( $request->path, '/', 'path is set' ); |
| 80 |
is( $request->method, 'POST', 'method is set' ); |
| 81 |
ok( $request->is_post, 'method is POST' ); |
| 82 |
|
| 83 |
like( |
| 84 |
$request->to_string, |
| 85 |
qr{^\[\#\d+\] POST /}, |
| 86 |
'Request presented well as string', |
| 87 |
); |
| 88 |
|
| 89 |
is_deeply( |
| 90 |
scalar( $request->params ), |
| 91 |
\%exp_params, |
| 92 |
'params are OK', |
| 93 |
); |
| 94 |
|
| 95 |
my %params = $request->params; |
| 96 |
is_deeply( |
| 97 |
scalar( $request->params ), |
| 98 |
\%params, |
| 99 |
'params wantarray works', |
| 100 |
); |
| 101 |
|
| 102 |
is_deeply( |
| 103 |
scalar( $request->params ), |
| 104 |
\%params, |
| 105 |
'params wantarray works', |
| 106 |
); |
| 107 |
|
| 108 |
return psgi_ok; |
| 109 |
} ); |
| 110 |
|
| 111 |
my $req = POST '/', \%exp_params; |
| 112 |
|
| 113 |
ok( |
| 114 |
$test->request($req)->is_success, |
| 115 |
'Request successful', |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
sub test_mixed_params { |
| 120 |
my $test = Plack::Test->create( sub { |
| 121 |
my $env = shift; |
| 122 |
my $request = Dancer2::Core::Request->new( env => $env ); |
| 123 |
|
| 124 |
my %exp_params = ( |
| 125 |
mixed => { |
| 126 |
x => 1, y => 2, meth => 'post', |
| 127 |
}, |
| 128 |
|
| 129 |
get => { |
| 130 |
y => 2, meth => 'get', |
| 131 |
}, |
| 132 |
|
| 133 |
post => { |
| 134 |
x => 1, meth => 'post', |
| 135 |
}, |
| 136 |
); |
| 137 |
|
| 138 |
is( $request->path, '/', 'path is set' ); |
| 139 |
is( $request->method, 'POST', 'method is set' ); |
| 140 |
|
| 141 |
is_deeply( |
| 142 |
scalar( $request->params ), |
| 143 |
$exp_params{'mixed'}, |
| 144 |
'params are OK', |
| 145 |
); |
| 146 |
|
| 147 |
is_deeply( |
| 148 |
scalar( $request->params('body') ), |
| 149 |
$exp_params{'post'}, |
| 150 |
'body params are OK', |
| 151 |
); |
| 152 |
|
| 153 |
is_deeply( |
| 154 |
scalar( $request->params('query') ), |
| 155 |
$exp_params{'get'}, |
| 156 |
'query params are OK', |
| 157 |
); |
| 158 |
|
| 159 |
return psgi_ok; |
| 160 |
} ); |
| 161 |
|
| 162 |
my $req = POST '/?y=2&meth=get', |
| 163 |
{ x => 1, meth => 'post' }; |
| 164 |
|
| 165 |
ok( |
| 166 |
$test->request($req)->is_success, |
| 167 |
'Request successful', |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
sub test_all_params { |
| 172 |
test_get_params; |
| 173 |
test_post_params; |
| 174 |
test_mixed_params; |
| 175 |
} |
| 176 |
|
| 177 |
subtest 'Defaults' => sub { |
| 178 |
my $test = Plack::Test->create( sub { |
| 179 |
my $env = shift; |
| 180 |
my $request = Dancer2::Core::Request->new( env => $env ); |
| 181 |
isa_ok( $request, 'Dancer2::Core::Request' ); |
| 182 |
|
| 183 |
can_ok( $request, 'env' ); |
| 184 |
isa_ok( $request->env, 'HASH' ); |
| 185 |
|
| 186 |
# http env keys |
| 187 |
my @http_env_keys = qw< |
| 188 |
accept accept_charset accept_encoding accept_language |
| 189 |
connection keep_alive referer user_agent x_requested_with |
| 190 |
>; |
| 191 |
|
| 192 |
can_ok( $request, @http_env_keys ); |
| 193 |
|
| 194 |
is( |
| 195 |
$request->$_, |
| 196 |
$request->env->{"HTTP_$_"}, |
| 197 |
"HTTP ENV key $_", |
| 198 |
) for @http_env_keys; |
| 199 |
|
| 200 |
is( |
| 201 |
$request->agent, |
| 202 |
$request->user_agent, |
| 203 |
'agent as alias to user_agent', |
| 204 |
); |
| 205 |
|
| 206 |
is( |
| 207 |
$request->remote_address, |
| 208 |
$request->address, |
| 209 |
'remote_address as alias to address', |
| 210 |
); |
| 211 |
|
| 212 |
# variables |
| 213 |
$request->var( foo => 'bar' ); |
| 214 |
is_deeply( |
| 215 |
$request->vars, |
| 216 |
{ foo => 'bar' }, |
| 217 |
'Setting variables using DSL', |
| 218 |
); |
| 219 |
|
| 220 |
is( $request->var('foo'), 'bar', 'Read single variable' ); |
| 221 |
|
| 222 |
$request->var( foo => 'baz' ); |
| 223 |
|
| 224 |
is_deeply( |
| 225 |
$request->vars, |
| 226 |
{ foo => 'baz' }, |
| 227 |
'Overwriting variables using vars() method', |
| 228 |
); |
| 229 |
|
| 230 |
is( $request->var('foo'), 'baz', 'Read variable' ); |
| 231 |
|
| 232 |
is( $request->path, '/defaults', 'Default path' ); |
| 233 |
is( $request->path_info, '/defaults', 'Default path_info' ); |
| 234 |
is( $request->method, 'GET', 'Default method' ); |
| 235 |
|
| 236 |
is( $request->id, 1, 'Correct request ID' ); |
| 237 |
|
| 238 |
my %aliases = ( |
| 239 |
address => 'REMOTE_ADDR', |
| 240 |
remote_host => 'REMOTE_HOST', |
| 241 |
protocol => 'SERVER_PROTOCOL', |
| 242 |
port => 'SERVER_PORT', |
| 243 |
request_uri => 'REQUEST_URI', |
| 244 |
user => 'REMOTE_USER', |
| 245 |
script_name => 'SCRIPT_NAME', |
| 246 |
); |
| 247 |
|
| 248 |
is( |
| 249 |
$request->$_, |
| 250 |
$request->env->{ $aliases{$_} }, |
| 251 |
"$_ derived from $aliases{$_}", |
| 252 |
) for keys %aliases; |
| 253 |
|
| 254 |
is( |
| 255 |
$request->to_string, |
| 256 |
'[#1] GET /defaults', |
| 257 |
'Correct to_string', |
| 258 |
); |
| 259 |
|
| 260 |
return psgi_ok; |
| 261 |
} ); |
| 262 |
|
| 263 |
ok( |
| 264 |
$test->request( GET '/defaults' )->is_success, |
| 265 |
'Request successful', |
| 266 |
); |
| 267 |
}; |
| 268 |
|
| 269 |
subtest 'Create with single env' => sub { |
| 270 |
isa_ok( |
| 271 |
Dancer2::Core::Request->new( env => {} ), |
| 272 |
'Dancer2::Core::Request', |
| 273 |
'Create with env hash', |
| 274 |
); |
| 275 |
|
| 276 |
my $request; |
| 277 |
isa_ok( |
| 278 |
$request = Dancer2::Core::Request->new( |
| 279 |
env => { REQUEST_METHOD => 'X' } |
| 280 |
), |
| 281 |
'Dancer2::Core::Request', |
| 282 |
'Create with single argument for env', |
| 283 |
); |
| 284 |
|
| 285 |
is( $request->method, 'X', 'env() attribute populated successfully' ); |
| 286 |
}; |
| 287 |
|
| 288 |
subtest 'Serializer' => sub { |
| 289 |
{ |
| 290 |
my $request = Dancer2::Core::Request->new( env => {} ); |
| 291 |
can_ok( $request, qw<serializer> ); |
| 292 |
ok( ! $request->serializer, 'No serializer set' ); |
| 293 |
} |
| 294 |
|
| 295 |
{ |
| 296 |
{ package Nothing; use Moo; } |
| 297 |
|
| 298 |
# The type check fails - BUILD is not called, no increment of _count. |
| 299 |
ok( |
| 300 |
exception { |
| 301 |
Dancer2::Core::Request->new( |
| 302 |
env => {}, |
| 303 |
serializer => Nothing->new, |
| 304 |
) |
| 305 |
}, |
| 306 |
'Cannot send random object to request as serializer', |
| 307 |
); |
| 308 |
|
| 309 |
{ |
| 310 |
package Serializer; |
| 311 |
use Moo; |
| 312 |
with 'Dancer2::Core::Role::Serializer'; |
| 313 |
sub serialize {1} |
| 314 |
sub deserialize {1} |
| 315 |
has '+content_type' => ( default => sub {1} ); |
| 316 |
} |
| 317 |
|
| 318 |
my $request; |
| 319 |
is( |
| 320 |
exception { |
| 321 |
$request = Dancer2::Core::Request->new( |
| 322 |
env => { REQUEST_METHOD => 'GET' }, |
| 323 |
serializer => Serializer->new, |
| 324 |
) |
| 325 |
}, |
| 326 |
undef, |
| 327 |
'Can create request with serializer', |
| 328 |
); |
| 329 |
|
| 330 |
ok( $request->serializer, 'Serializer set' ); |
| 331 |
isa_ok( $request->serializer, 'Serializer' ); |
| 332 |
} |
| 333 |
}; |
| 334 |
|
| 335 |
subtest 'Path when mounting' => sub { |
| 336 |
my $app = builder { mount '/mount' => sub { |
| 337 |
my $env = shift; |
| 338 |
my $request = Dancer2::Core::Request->new( env => $env ); |
| 339 |
|
| 340 |
is( |
| 341 |
$request->script_name, |
| 342 |
'/mount', |
| 343 |
'Script name when mounted (script_name)', |
| 344 |
); |
| 345 |
|
| 346 |
is( |
| 347 |
$request->request_uri, |
| 348 |
'/mount/mounted_path', |
| 349 |
'Correct request_uri', |
| 350 |
); |
| 351 |
|
| 352 |
is( |
| 353 |
$request->path, |
| 354 |
'/mounted_path', |
| 355 |
'Full path when mounted (path)', |
| 356 |
); |
| 357 |
|
| 358 |
is( |
| 359 |
$request->path_info, |
| 360 |
'/mounted_path', |
| 361 |
'Mounted path when mounted (path_info)', |
| 362 |
); |
| 363 |
|
| 364 |
return psgi_ok; |
| 365 |
} }; |
| 366 |
|
| 367 |
my $test = Plack::Test->create($app); |
| 368 |
|
| 369 |
ok( |
| 370 |
$test->request( GET '/mount/mounted_path' )->is_success, |
| 371 |
'Request successful', |
| 372 |
); |
| 373 |
}; |
| 374 |
|
| 375 |
subtest 'Different method' => sub { |
| 376 |
my $test = Plack::Test->create( sub { |
| 377 |
my $env = shift; |
| 378 |
my $request = Dancer2::Core::Request->new( env => $env ); |
| 379 |
|
| 380 |
is( $request->method, 'PUT', 'Correct method' ); |
| 381 |
|
| 382 |
is( |
| 383 |
$request->env->{'REQUEST_METHOD'}, |
| 384 |
$request->method, |
| 385 |
'REQUEST_METHOD derived from env', |
| 386 |
); |
| 387 |
|
| 388 |
return psgi_ok; |
| 389 |
} ); |
| 390 |
|
| 391 |
ok( |
| 392 |
$test->request( PUT '/' )->is_success, |
| 393 |
'Request successful', |
| 394 |
); |
| 395 |
}; |
| 396 |
|
| 397 |
# the calling order to this method matters because it checks |
| 398 |
# how many requests were run so far |
| 399 |
subtest 'Checking request ID' => sub { |
| 400 |
my $test = Plack::Test->create( sub { |
| 401 |
my $env = shift; |
| 402 |
my $request = Dancer2::Core::Request->new( env => $env ); |
| 403 |
is( $request->id, 8, 'Correct request id' ); |
| 404 |
|
| 405 |
return psgi_ok; |
| 406 |
} ); |
| 407 |
|
| 408 |
ok( |
| 409 |
$test->request( GET '/' )->is_success, |
| 410 |
'Request successful', |
| 411 |
); |
| 412 |
}; |
| 413 |
|
| 414 |
subtest 'is_$method (head/post/get/put/delete/patch' => sub { |
| 415 |
foreach my $http_method ( qw<head post get put delete patch> ) { |
| 416 |
my $test = Plack::Test->create( sub { |
| 417 |
my $env = shift; |
| 418 |
my $request = Dancer2::Core::Request->new( env => $env ); |
| 419 |
my $method = "is_$http_method"; |
| 420 |
ok( $request->$method, $method ); |
| 421 |
return psgi_ok; |
| 422 |
} ); |
| 423 |
|
| 424 |
ok( |
| 425 |
$test->request( |
| 426 |
HTTP::Request->new( ( uc $http_method ) => '/' ) |
| 427 |
)->is_success, |
| 428 |
'Request successful', |
| 429 |
); |
| 430 |
} |
| 431 |
}; |
| 432 |
|
| 433 |
subtest 'Parameters (body/query/route)' => sub { |
| 434 |
note $Dancer2::Core::Request::XS_URL_DECODE ? |
| 435 |
'Running test with XS_URL_DECODE' : |
| 436 |
'Running test without XS_URL_DECODE'; |
| 437 |
|
| 438 |
note $Dancer2::Core::Request::XS_PARSE_QUERY_STRING ? |
| 439 |
'Running test with XS_PARSE_QUERY_STRING' : |
| 440 |
'Running test without XS_PARSE_QUERY_STRING'; |
| 441 |
|
| 442 |
test_all_params; |
| 443 |
|
| 444 |
if ( $Dancer2::Core::Request::XS_PARSE_QUERY_STRING ) { |
| 445 |
note 'Running test without XS_PARSE_QUERY_STRING'; |
| 446 |
$Dancer2::Core::Request::XS_PARSE_QUERY_STRING = 0; |
| 447 |
test_all_params; |
| 448 |
} |
| 449 |
|
| 450 |
if ( $Dancer2::Core::Request::XS_URL_DECODE ) { |
| 451 |
note 'Running test without XS_URL_DECODE'; |
| 452 |
$Dancer2::Core::Request::XS_URL_DECODE = 0; |
| 453 |
test_all_params; |
| 454 |
} |
| 455 |
}; |
| 456 |
|
| 457 |
# multiple parsing of request body (such as from creating two request objects) |
| 458 |
# previously caused an infinite loop when HTTP::Body was used. |
| 459 |
subtest 'Multiple request object creation doesnt reparse request body' => sub { |
| 460 |
my $test = Plack::Test->create( sub { |
| 461 |
my $env = shift; |
| 462 |
my $request = Dancer2::Core::Request->new( env => $env ); |
| 463 |
# Second institation shouldn't reparse request body. |
| 464 |
my $request2 = Dancer2::Core::Request->new( env => $env ); |
| 465 |
|
| 466 |
my %exp_params = ( |
| 467 |
post => { |
| 468 |
x => 1, meth => 'post', |
| 469 |
}, |
| 470 |
); |
| 471 |
|
| 472 |
is( $request->path, '/', 'path is set' ); |
| 473 |
is( $request->method, 'POST', 'method is set' ); |
| 474 |
|
| 475 |
is_deeply( |
| 476 |
scalar( $request->params('body') ), |
| 477 |
$exp_params{'post'}, |
| 478 |
'body params are OK', |
| 479 |
); |
| 480 |
|
| 481 |
return psgi_ok; |
| 482 |
} ); |
| 483 |
|
| 484 |
my $req = POST '/', { x => 1, meth => 'post' }; |
| 485 |
|
| 486 |
ok( |
| 487 |
$test->request($req)->is_success, |
| 488 |
'Request successful', |
| 489 |
); |
| 490 |
}; |
| 491 |
|
| 492 |
# more stuff to test |
| 493 |
|
| 494 |
# special methods: |
| 495 |
# forwarded_for_address |
| 496 |
# forwarded_protocol |
| 497 |
# forwarded_host |
| 498 |
# host |
| 499 |
|
| 500 |
#subtest 'Behind proxy (host/is_behind_proxy)' => sub { |
| 501 |
# my $test = Plack::Test->create( sub { psgi_ok } ); |
| 502 |
# |
| 503 |
# ok( |
| 504 |
# $test->request( GET '/dev/null' )->is_success, |
| 505 |
# 'Different method request successful', |
| 506 |
# ); |
| 507 |
#}; |
| 508 |
|
| 509 |
#subtest 'Path resolution methods' => sub { |
| 510 |
# my $test = Plack::Test->create( sub { |
| 511 |
# my $env = shift; |
| 512 |
# my $request = Dancer2::Core::Request->new( env => $env ); |
| 513 |
# |
| 514 |
# return psgi_ok; |
| 515 |
# } ); |
| 516 |
#}; |
| 517 |
|
| 518 |
#subtest 'Upload' => sub {1}; |
| 519 |
#subtest 'Scheme' => sub {1}; |
| 520 |
#subtest 'Cookies' => sub {1}; |
| 521 |
#subtest 'Headers' => sub {1}; |