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