| 1 |
package Dancer2::Test; |
| 2 |
# ABSTRACT: Useful routines for testing Dancer2 apps |
| 3 |
$Dancer2::Test::VERSION = '2.2.2'; |
| 4 |
use strict; |
| 5 |
use warnings; |
| 6 |
|
| 7 |
use Carp qw<carp croak>; |
| 8 |
use Test::More; |
| 9 |
use Test::Builder; |
| 10 |
use URI::Escape; |
| 11 |
use Data::Dumper; |
| 12 |
use File::Temp; |
| 13 |
use Ref::Util qw<is_arrayref>; |
| 14 |
|
| 15 |
use parent 'Exporter'; |
| 16 |
our @EXPORT = qw( |
| 17 |
dancer_response |
| 18 |
|
| 19 |
response_content_is |
| 20 |
response_content_isnt |
| 21 |
response_content_is_deeply |
| 22 |
response_content_like |
| 23 |
response_content_unlike |
| 24 |
|
| 25 |
response_status_is |
| 26 |
response_status_isnt |
| 27 |
|
| 28 |
response_headers_include |
| 29 |
response_headers_are_deeply |
| 30 |
|
| 31 |
response_is_file |
| 32 |
|
| 33 |
route_exists |
| 34 |
route_doesnt_exist |
| 35 |
|
| 36 |
is_pod_covered |
| 37 |
route_pod_coverage |
| 38 |
|
| 39 |
); |
| 40 |
|
| 41 |
#dancer1 also has read_logs, response_redirect_location_is |
| 42 |
#cf. https://github.com/PerlDancer2/Dancer22/issues/25 |
| 43 |
|
| 44 |
use Dancer2::Core::MIME; |
| 45 |
use Dancer2::Core::Dispatcher; |
| 46 |
use Dancer2::Core::Request; |
| 47 |
|
| 48 |
# singleton to store all the apps |
| 49 |
my $_dispatcher = Dancer2::Core::Dispatcher->new; |
| 50 |
|
| 51 |
# prevent deprecation warnings |
| 52 |
our $NO_WARN = 0; |
| 53 |
|
| 54 |
# can be called with the ($method, $path, $option) triplet, |
| 55 |
# or can be fed a request object directly, or can be fed |
| 56 |
# a single string, assumed to be [ GET => $string ] |
| 57 |
# or can be fed a response (which is passed through without |
| 58 |
# any modification) |
| 59 |
sub dancer_response { |
| 60 |
croak 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead' |
| 61 |
unless $NO_WARN; |
| 62 |
|
| 63 |
_find_dancer_apps_for_dispatcher(); |
| 64 |
|
| 65 |
# useful for the high-level tests |
| 66 |
return $_[0] if ref $_[0] eq 'Dancer2::Core::Response'; |
| 67 |
|
| 68 |
my ( $request, $env ) = |
| 69 |
ref $_[0] eq 'Dancer2::Core::Request' |
| 70 |
? _build_env_from_request(@_) |
| 71 |
: _build_request_from_env(@_); |
| 72 |
|
| 73 |
# override the set_request so it actually sets our request instead |
| 74 |
{ |
| 75 |
## no critic qw(TestingAndDebugging::ProhibitNoWarnings) |
| 76 |
no warnings qw<redefine once>; |
| 77 |
*Dancer2::Core::App::set_request = sub { |
| 78 |
my $self = shift; |
| 79 |
$self->_set_request( $request ); |
| 80 |
$_->set_request( $request ) for @{ $self->defined_engines }; |
| 81 |
}; |
| 82 |
} |
| 83 |
|
| 84 |
# since the response is a PSGI response |
| 85 |
# we create a Response object which was originally expected |
| 86 |
my $psgi_response = $_dispatcher->dispatch($env); |
| 87 |
return Dancer2::Core::Response->new( |
| 88 |
status => $psgi_response->[0], |
| 89 |
headers => $psgi_response->[1], |
| 90 |
content => $psgi_response->[2][0], |
| 91 |
mime_type => Dancer2::Core::MIME->new(), |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
sub _build_request_from_env { |
| 98 |
|
| 99 |
# arguments can be passed as the triplet |
| 100 |
# or as a arrayref, or as a simple string |
| 101 |
my ( $method, $path, $options ) = |
| 102 |
@_ > 1 ? @_ |
| 103 |
: is_arrayref($_[0]) ? @{ $_[0] } |
| 104 |
: ( GET => $_[0], {} ); |
| 105 |
|
| 106 |
my $env = { |
| 107 |
%ENV, |
| 108 |
REQUEST_METHOD => uc($method), |
| 109 |
PATH_INFO => $path, |
| 110 |
QUERY_STRING => '', |
| 111 |
'psgi.url_scheme' => 'http', |
| 112 |
SERVER_PROTOCOL => 'HTTP/1.0', |
| 113 |
SERVER_NAME => 'localhost', |
| 114 |
SERVER_PORT => 3000, |
| 115 |
HTTP_HOST => 'localhost', |
| 116 |
HTTP_USER_AGENT => "Dancer2::Test simulator v " . Dancer2->VERSION, |
| 117 |
}; |
| 118 |
|
| 119 |
if ( defined $options->{params} ) { |
| 120 |
my @params; |
| 121 |
while ( my ( $p, $value ) = each %{ $options->{params} } ) { |
| 122 |
if ( is_arrayref($value) ) { |
| 123 |
for my $v (@$value) { |
| 124 |
push @params, |
| 125 |
uri_escape_utf8($p) . '=' . uri_escape_utf8($v); |
| 126 |
} |
| 127 |
} |
| 128 |
else { |
| 129 |
push @params, |
| 130 |
uri_escape_utf8($p) . '=' . uri_escape_utf8($value); |
| 131 |
} |
| 132 |
} |
| 133 |
$env->{QUERY_STRING} = join( '&', @params ); |
| 134 |
} |
| 135 |
|
| 136 |
my $request = Dancer2::Core::Request->new( env => $env ); |
| 137 |
|
| 138 |
# body |
| 139 |
$request->body( $options->{body} ) if exists $options->{body}; |
| 140 |
|
| 141 |
# headers |
| 142 |
if ( $options->{headers} ) { |
| 143 |
for my $header ( @{ $options->{headers} } ) { |
| 144 |
my ( $name, $value ) = @{$header}; |
| 145 |
$request->header( $name => $value ); |
| 146 |
if ( $name =~ /^cookie$/i ) { |
| 147 |
$env->{HTTP_COOKIE} = $value; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
# files |
| 153 |
if ( $options->{files} ) { |
| 154 |
for my $file ( @{ $options->{files} } ) { |
| 155 |
my $headers = $file->{headers}; |
| 156 |
$headers->{'Content-Type'} ||= 'text/plain'; |
| 157 |
|
| 158 |
my $temp = File::Temp->new(); |
| 159 |
if ( $file->{data} ) { |
| 160 |
print $temp $file->{data}; |
| 161 |
close($temp); |
| 162 |
} |
| 163 |
else { |
| 164 |
require File::Copy; |
| 165 |
File::Copy::copy( $file->{filename}, $temp ); |
| 166 |
} |
| 167 |
|
| 168 |
my $upload = Dancer2::Core::Request::Upload->new( |
| 169 |
filename => $file->{filename}, |
| 170 |
size => -s $temp->filename, |
| 171 |
tempname => $temp->filename, |
| 172 |
headers => $headers, |
| 173 |
); |
| 174 |
|
| 175 |
## keep temp_fh in scope so it doesn't get deleted too early |
| 176 |
## But will get deleted by the time the test is finished. |
| 177 |
$upload->{temp_fh} = $temp; |
| 178 |
|
| 179 |
$request->uploads->{ $file->{name} } = $upload; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
# content-type |
| 184 |
if ( $options->{content_type} ) { |
| 185 |
$request->content_type( $options->{content_type} ); |
| 186 |
} |
| 187 |
|
| 188 |
return ( $request, $env ); |
| 189 |
} |
| 190 |
|
| 191 |
sub _build_env_from_request { |
| 192 |
my ($request) = @_; |
| 193 |
|
| 194 |
my $env = { |
| 195 |
REQUEST_METHOD => $request->method, |
| 196 |
PATH_INFO => $request->path, |
| 197 |
QUERY_STRING => '', |
| 198 |
'psgi.url_scheme' => 'http', |
| 199 |
SERVER_PROTOCOL => 'HTTP/1.0', |
| 200 |
SERVER_NAME => 'localhost', |
| 201 |
SERVER_PORT => 3000, |
| 202 |
HTTP_HOST => 'localhost', |
| 203 |
HTTP_USER_AGENT => "Dancer2::Test simulator v" . Dancer2->VERSION, |
| 204 |
}; |
| 205 |
|
| 206 |
# TODO |
| 207 |
if ( my $params = $request->{_query_params} ) { |
| 208 |
my @params; |
| 209 |
while ( my ( $p, $value ) = each %{$params} ) { |
| 210 |
if ( is_arrayref($value) ) { |
| 211 |
for my $v (@$value) { |
| 212 |
push @params, |
| 213 |
uri_escape_utf8($p) . '=' . uri_escape_utf8($v); |
| 214 |
} |
| 215 |
} |
| 216 |
else { |
| 217 |
push @params, |
| 218 |
uri_escape_utf8($p) . '=' . uri_escape_utf8($value); |
| 219 |
} |
| 220 |
} |
| 221 |
$env->{QUERY_STRING} = join( '&', @params ); |
| 222 |
} |
| 223 |
|
| 224 |
# TODO files |
| 225 |
|
| 226 |
return ( $request, $env ); |
| 227 |
} |
| 228 |
|
| 229 |
sub response_status_is { |
| 230 |
my ( $req, $status, $test_name ) = @_; |
| 231 |
carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead' |
| 232 |
unless $NO_WARN; |
| 233 |
|
| 234 |
$test_name ||= "response status is $status for " . _req_label($req); |
| 235 |
|
| 236 |
my $response = dancer_response($req); |
| 237 |
|
| 238 |
my $tb = Test::Builder->new; |
| 239 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 240 |
$tb->is_eq( $response->[0], $status, $test_name ); |
| 241 |
} |
| 242 |
|
| 243 |
sub _find_route_match { |
| 244 |
my ( $request, $env ) = |
| 245 |
ref $_[0] eq 'Dancer2::Core::Request' |
| 246 |
? _build_env_from_request(@_) |
| 247 |
: _build_request_from_env(@_); |
| 248 |
|
| 249 |
for my $app (@{$_dispatcher->apps}) { |
| 250 |
for my $route (@{$app->routes->{lc($request->method)}}) { |
| 251 |
if ( $route->match($request) ) { |
| 252 |
return 1; |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
return 0; |
| 257 |
} |
| 258 |
|
| 259 |
sub route_exists { |
| 260 |
carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead' |
| 261 |
unless $NO_WARN; |
| 262 |
|
| 263 |
my $tb = Test::Builder->new; |
| 264 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 265 |
$tb->ok( _find_route_match($_[0]), $_[1]); |
| 266 |
} |
| 267 |
|
| 268 |
sub route_doesnt_exist { |
| 269 |
carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead' |
| 270 |
unless $NO_WARN; |
| 271 |
|
| 272 |
my $tb = Test::Builder->new; |
| 273 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 274 |
$tb->ok( !_find_route_match($_[0]), $_[1]); |
| 275 |
} |
| 276 |
|
| 277 |
sub response_status_isnt { |
| 278 |
my ( $req, $status, $test_name ) = @_; |
| 279 |
|
| 280 |
carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead' |
| 281 |
unless $NO_WARN; |
| 282 |
|
| 283 |
$test_name ||= "response status is not $status for " . _req_label($req); |
| 284 |
|
| 285 |
my $response = dancer_response($req); |
| 286 |
|
| 287 |
my $tb = Test::Builder->new; |
| 288 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 289 |
$tb->isnt_eq( $response->[0], $status, $test_name ); |
| 290 |
} |
| 291 |
|
| 292 |
{ |
| 293 |
# Map comparison operator names to human-friendly ones |
| 294 |
my %cmp_name = ( |
| 295 |
is_eq => "is", |
| 296 |
isnt_eq => "is not", |
| 297 |
like => "matches", |
| 298 |
unlike => "doesn't match", |
| 299 |
); |
| 300 |
|
| 301 |
sub _cmp_response_content { |
| 302 |
my ( $req, $want, $test_name, $cmp ) = @_; |
| 303 |
|
| 304 |
if ( @_ == 3 ) { |
| 305 |
$cmp = $test_name; |
| 306 |
$test_name = $cmp_name{$cmp}; |
| 307 |
$test_name = |
| 308 |
"response content $test_name $want for " . _req_label($req); |
| 309 |
} |
| 310 |
|
| 311 |
my $response = dancer_response($req); |
| 312 |
|
| 313 |
my $tb = Test::Builder->new; |
| 314 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 315 |
$tb->$cmp( $response->[2][0], $want, $test_name ); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
sub response_content_is { |
| 320 |
carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead' |
| 321 |
unless $NO_WARN; |
| 322 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 323 |
_cmp_response_content( @_, 'is_eq' ); |
| 324 |
} |
| 325 |
|
| 326 |
sub response_content_isnt { |
| 327 |
carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead' |
| 328 |
unless $NO_WARN; |
| 329 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 330 |
_cmp_response_content( @_, 'isnt_eq' ); |
| 331 |
} |
| 332 |
|
| 333 |
sub response_content_like { |
| 334 |
carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead' |
| 335 |
unless $NO_WARN; |
| 336 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 337 |
_cmp_response_content( @_, 'like' ); |
| 338 |
} |
| 339 |
|
| 340 |
sub response_content_unlike { |
| 341 |
carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead' |
| 342 |
unless $NO_WARN; |
| 343 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 344 |
_cmp_response_content( @_, 'unlike' ); |
| 345 |
} |
| 346 |
|
| 347 |
sub response_content_is_deeply { |
| 348 |
my ( $req, $matcher, $test_name ) = @_; |
| 349 |
carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead' |
| 350 |
unless $NO_WARN; |
| 351 |
$test_name ||= "response content looks good for " . _req_label($req); |
| 352 |
|
| 353 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 354 |
my $response = _req_to_response($req); |
| 355 |
is_deeply $response->[2][0], $matcher, $test_name; |
| 356 |
} |
| 357 |
|
| 358 |
sub response_is_file { |
| 359 |
my ( $req, $test_name ) = @_; |
| 360 |
carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead' |
| 361 |
unless $NO_WARN; |
| 362 |
$test_name ||= "a file is returned for " . _req_label($req); |
| 363 |
|
| 364 |
my $response = _get_file_response($req); |
| 365 |
my $tb = Test::Builder->new; |
| 366 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 367 |
return $tb->ok( defined($response), $test_name ); |
| 368 |
} |
| 369 |
|
| 370 |
sub response_headers_are_deeply { |
| 371 |
my ( $req, $expected, $test_name ) = @_; |
| 372 |
carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead' |
| 373 |
unless $NO_WARN; |
| 374 |
$test_name ||= "headers are as expected for " . _req_label($req); |
| 375 |
|
| 376 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 377 |
my $response = dancer_response( _expand_req($req) ); |
| 378 |
|
| 379 |
is_deeply( |
| 380 |
_sort_headers( $response->[1] ), |
| 381 |
_sort_headers($expected), $test_name |
| 382 |
); |
| 383 |
} |
| 384 |
|
| 385 |
sub response_headers_include { |
| 386 |
my ( $req, $expected, $test_name ) = @_; |
| 387 |
carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead' |
| 388 |
unless $NO_WARN; |
| 389 |
$test_name ||= "headers include expected data for " . _req_label($req); |
| 390 |
my $tb = Test::Builder->new; |
| 391 |
|
| 392 |
my $response = dancer_response($req); |
| 393 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 394 |
|
| 395 |
print STDERR "Headers are: " |
| 396 |
. Dumper( $response->[1] ) |
| 397 |
. "\n Expected to find header: " |
| 398 |
. Dumper($expected) |
| 399 |
if !$tb->ok( |
| 400 |
_include_in_headers( $response->[1], $expected ), |
| 401 |
$test_name |
| 402 |
); |
| 403 |
} |
| 404 |
|
| 405 |
sub route_pod_coverage { |
| 406 |
|
| 407 |
require Pod::Simple::Search; |
| 408 |
require Pod::Simple::SimpleTree; |
| 409 |
|
| 410 |
my $all_routes = {}; |
| 411 |
|
| 412 |
foreach my $app ( @{ $_dispatcher->apps } ) { |
| 413 |
my $routes = $app->routes; |
| 414 |
my $available_routes = []; |
| 415 |
foreach my $method ( sort { $b cmp $a } keys %$routes ) { |
| 416 |
foreach my $r ( @{ $routes->{$method} } ) { |
| 417 |
|
| 418 |
# we don't need pod coverage for head |
| 419 |
next if $method eq 'head'; |
| 420 |
push @$available_routes, $method . ' ' . $r->spec_route; |
| 421 |
} |
| 422 |
} |
| 423 |
## copy dereferenced array |
| 424 |
$all_routes->{ $app->name }{routes} = [@$available_routes] |
| 425 |
if @$available_routes; |
| 426 |
|
| 427 |
# Pod::Simple v3.30 excluded the current directory even when in @INC. |
| 428 |
# include the current directory as a search path; its backwards compatible |
| 429 |
# with previous version. |
| 430 |
my $undocumented_routes = []; |
| 431 |
my $file = Pod::Simple::Search->new->find( $app->name, '.' ); |
| 432 |
if ($file) { |
| 433 |
$all_routes->{ $app->name }{has_pod} = 1; |
| 434 |
my $parser = Pod::Simple::SimpleTree->new->parse_file($file); |
| 435 |
my $pod_dataref = $parser->root; |
| 436 |
my $found_routes = {}; |
| 437 |
for ( my $i = 0; $i < @$available_routes; $i++ ) { |
| 438 |
|
| 439 |
my $r = $available_routes->[$i]; |
| 440 |
my $app_string = lc $r; |
| 441 |
$app_string =~ s/\*/_REPLACED_STAR_/g; |
| 442 |
|
| 443 |
for ( my $idx = 0; $idx < @$pod_dataref; $idx++ ) { |
| 444 |
my $pod_part = $pod_dataref->[$idx]; |
| 445 |
|
| 446 |
next if !is_arrayref($pod_part); |
| 447 |
foreach my $ref_part (@$pod_part) { |
| 448 |
is_arrayref($ref_part) |
| 449 |
and push @$pod_dataref, $ref_part; |
| 450 |
} |
| 451 |
|
| 452 |
my $pod_string = lc $pod_part->[2]; |
| 453 |
$pod_string =~ s/['|"|\s]+/ /g; |
| 454 |
$pod_string =~ s/\s$//g; |
| 455 |
$pod_string =~ s/\*/_REPLACED_STAR_/g; |
| 456 |
if ( $pod_string =~ m/^$app_string$/ ) { |
| 457 |
$found_routes->{$app_string} = 1; |
| 458 |
next; |
| 459 |
} |
| 460 |
} |
| 461 |
if ( !$found_routes->{$app_string} ) { |
| 462 |
push @$undocumented_routes, $r; |
| 463 |
} |
| 464 |
} |
| 465 |
} |
| 466 |
else { ### no POD found |
| 467 |
$all_routes->{ $app->name }{has_pod} = 0; |
| 468 |
} |
| 469 |
if (@$undocumented_routes) { |
| 470 |
$all_routes->{ $app->name }{undocumented_routes} = |
| 471 |
$undocumented_routes; |
| 472 |
} |
| 473 |
elsif ( !$all_routes->{ $app->name }{has_pod} |
| 474 |
&& @{ $all_routes->{ $app->name }{routes} } ) |
| 475 |
{ |
| 476 |
## copy dereferenced array |
| 477 |
$all_routes->{ $app->name }{undocumented_routes} = |
| 478 |
[ @{ $all_routes->{ $app->name }{routes} } ]; |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
return $all_routes; |
| 483 |
} |
| 484 |
|
| 485 |
sub is_pod_covered { |
| 486 |
my ($test_name) = @_; |
| 487 |
|
| 488 |
$test_name ||= "is pod covered"; |
| 489 |
my $route_pod_coverage = route_pod_coverage(); |
| 490 |
|
| 491 |
my $tb = Test::Builder->new; |
| 492 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 493 |
|
| 494 |
foreach my $app ( @{ $_dispatcher->apps } ) { |
| 495 |
my %undocumented_route = |
| 496 |
( map { $_ => 1 } |
| 497 |
@{ $route_pod_coverage->{ $app->name }{undocumented_routes} } ); |
| 498 |
$tb->subtest( |
| 499 |
$app->name . $test_name, |
| 500 |
sub { |
| 501 |
foreach my $route ( |
| 502 |
@{ $route_pod_coverage->{ $app->name }{routes} } ) |
| 503 |
{ |
| 504 |
ok( !$undocumented_route{$route}, "$route is documented" ); |
| 505 |
} |
| 506 |
} |
| 507 |
); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
sub import { |
| 512 |
my ( $class, %options ) = @_; |
| 513 |
|
| 514 |
my @applications; |
| 515 |
if ( ref $options{apps} eq ref( [] ) ) { |
| 516 |
@applications = @{ $options{apps} }; |
| 517 |
} |
| 518 |
else { |
| 519 |
my ( $caller, $script ) = caller; |
| 520 |
|
| 521 |
# if no app is passed, assume the caller is one. |
| 522 |
@applications = ($caller) if $caller->can('dancer_app'); |
| 523 |
} |
| 524 |
|
| 525 |
# register the apps to the test dispatcher |
| 526 |
$_dispatcher->apps( [ map { |
| 527 |
$_->dancer_app->finish(); |
| 528 |
$_->dancer_app; |
| 529 |
} @applications ] ); |
| 530 |
|
| 531 |
$class->export_to_level( 1, $class, @EXPORT ); |
| 532 |
} |
| 533 |
|
| 534 |
# private |
| 535 |
|
| 536 |
sub _req_label { |
| 537 |
my $req = shift; |
| 538 |
|
| 539 |
return |
| 540 |
ref $req eq 'Dancer2::Core::Response' ? 'response object' |
| 541 |
: ref $req eq 'Dancer2::Core::Request' |
| 542 |
? join( ' ', map { $req->$_ } qw/ method path / ) |
| 543 |
: is_arrayref($req) ? join( ' ', @$req ) |
| 544 |
: "GET $req"; |
| 545 |
} |
| 546 |
|
| 547 |
sub _expand_req { |
| 548 |
my $req = shift; |
| 549 |
return is_arrayref($req) ? @$req : ( 'GET', $req ); |
| 550 |
} |
| 551 |
|
| 552 |
# Sort arrayref of headers (turn it into a list of arrayrefs, sort by the header |
| 553 |
# & value, then turn it back into an arrayref) |
| 554 |
sub _sort_headers { |
| 555 |
my @originalheaders = @{ shift() }; # take a copy we can modify |
| 556 |
my @headerpairs; |
| 557 |
while ( my ( $header, $value ) = splice @originalheaders, 0, 2 ) { |
| 558 |
push @headerpairs, [ $header, $value ]; |
| 559 |
} |
| 560 |
|
| 561 |
# We have an array of arrayrefs holding header => value pairs; sort them by |
| 562 |
# header then value, and return them flattened back into an arrayref |
| 563 |
return [ |
| 564 |
map {@$_} |
| 565 |
sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @headerpairs |
| 566 |
]; |
| 567 |
} |
| 568 |
|
| 569 |
# make sure the given header sublist is included in the full headers array |
| 570 |
sub _include_in_headers { |
| 571 |
my ( $full_headers, $expected_subset ) = @_; |
| 572 |
|
| 573 |
# walk through all the expected header pairs, make sure |
| 574 |
# they exist with the same value in the full_headers list |
| 575 |
# return false as soon as one is not. |
| 576 |
for ( my $i = 0; $i < scalar(@$expected_subset); $i += 2 ) { |
| 577 |
my ( $name, $value ) = |
| 578 |
( $expected_subset->[$i], $expected_subset->[ $i + 1 ] ); |
| 579 |
return 0 |
| 580 |
unless _check_header( $full_headers, $name, $value ); |
| 581 |
} |
| 582 |
|
| 583 |
# we've found all the expected pairs in the $full_headers list |
| 584 |
return 1; |
| 585 |
} |
| 586 |
|
| 587 |
sub _check_header { |
| 588 |
my ( $headers, $key, $value ) = @_; |
| 589 |
for ( my $i = 0; $i < scalar(@$headers); $i += 2 ) { |
| 590 |
my ( $name, $val ) = ( $headers->[$i], $headers->[ $i + 1 ] ); |
| 591 |
return 1 if $name eq $key && $value eq $val; |
| 592 |
} |
| 593 |
return 0; |
| 594 |
} |
| 595 |
|
| 596 |
sub _req_to_response { |
| 597 |
my $req = shift; |
| 598 |
|
| 599 |
# already a response object |
| 600 |
return $req if ref $req eq 'Dancer2::Core::Response'; |
| 601 |
|
| 602 |
return dancer_response( is_arrayref($req) ? @$req : ( 'GET', $req ) ); |
| 603 |
} |
| 604 |
|
| 605 |
# make sure we have at least one app in the dispatcher, and if not, |
| 606 |
# we must have at this point an app within the caller |
| 607 |
sub _find_dancer_apps_for_dispatcher { |
| 608 |
return if scalar( @{ $_dispatcher->apps } ); |
| 609 |
|
| 610 |
for ( my $deep = 0; $deep < 5; $deep++ ) { |
| 611 |
my $caller = caller($deep); |
| 612 |
next if !$caller || !$caller->can('dancer_app'); |
| 613 |
|
| 614 |
return $_dispatcher->apps( [ $caller->dancer_app ] ); |
| 615 |
} |
| 616 |
|
| 617 |
croak "Unable to find a Dancer2 app, did you use Dancer2 in your test?"; |
| 618 |
} |
| 619 |
|
| 620 |
1; |
| 621 |
|
| 622 |
__END__ |
| 623 |
|
| 624 |
=pod |
| 625 |
|
| 626 |
=encoding UTF-8 |
| 627 |
|
| 628 |
=head1 NAME |
| 629 |
|
| 630 |
Dancer2::Test - Useful routines for testing Dancer2 apps |
| 631 |
|
| 632 |
=head1 VERSION |
| 633 |
|
| 634 |
version 2.2.2 |
| 635 |
|
| 636 |
=head1 SYNOPSIS |
| 637 |
|
| 638 |
use Test::More; |
| 639 |
use Plack::Test; |
| 640 |
use HTTP::Request::Common; # install separately |
| 641 |
|
| 642 |
use YourDancerApp; |
| 643 |
|
| 644 |
my $app = YourDancerApp->to_app; |
| 645 |
my $test = Plack::Test->create($app); |
| 646 |
|
| 647 |
my $res = $test->request( GET '/' ); |
| 648 |
is( $res->code, 200, '[GET /] Request successful' ); |
| 649 |
like( $res->content, qr/hello, world/, '[GET /] Correct content' ); |
| 650 |
|
| 651 |
done_testing; |
| 652 |
|
| 653 |
=head1 DESCRIPTION |
| 654 |
|
| 655 |
B<DEPRECATED. This module and all the functions listed below are deprecated. Do |
| 656 |
not use this module.> The routines provided by this module for testing Dancer2 |
| 657 |
apps are buggy and unnecessary. Instead, use the L<Plack::Test> module as shown |
| 658 |
in the SYNOPSIS above and ignore the functions in this documentation. Consult |
| 659 |
the L<Plack::Test> documentation for further details. |
| 660 |
|
| 661 |
This module will be removed from the Dancer2 distribution in the near future. |
| 662 |
You should migrate all tests that use it over to the L<Plack::Test> module and |
| 663 |
remove this module from your system. This module will throw warnings to remind |
| 664 |
you. |
| 665 |
|
| 666 |
For now, you can silence the warnings by setting the C<NO_WARN> option: |
| 667 |
|
| 668 |
$Dancer::Test::NO_WARN = 1; |
| 669 |
|
| 670 |
In the functions below, $test_name is always optional. |
| 671 |
|
| 672 |
=head1 FUNCTIONS |
| 673 |
|
| 674 |
=head2 dancer_response ($method, $path, $params, $arg_env); |
| 675 |
|
| 676 |
Returns a Dancer2::Core::Response object for the given request. |
| 677 |
|
| 678 |
Only $method and $path are required. |
| 679 |
|
| 680 |
$params is a hashref with 'body' as a string; 'headers' can be an arrayref or |
| 681 |
a HTTP::Headers object, 'files' can be arrayref of hashref, containing some |
| 682 |
files to upload: |
| 683 |
|
| 684 |
dancer_response($method, $path, |
| 685 |
{ |
| 686 |
params => $params, |
| 687 |
body => $body, |
| 688 |
headers => $headers, |
| 689 |
files => [ { filename => '/path/to/file', name => 'my_file' } ], |
| 690 |
} |
| 691 |
); |
| 692 |
|
| 693 |
A good reason to use this function is for testing POST requests. Since POST |
| 694 |
requests may not be idempotent, it is necessary to capture the content and |
| 695 |
status in one shot. Calling the response_status_is and response_content_is |
| 696 |
functions in succession would make two requests, each of which could alter the |
| 697 |
state of the application and cause Schrodinger's cat to die. |
| 698 |
|
| 699 |
my $response = dancer_response POST => '/widgets'; |
| 700 |
is $response->status, 202, "response for POST /widgets is 202"; |
| 701 |
is $response->content, "Widget #1 has been scheduled for creation", |
| 702 |
"response content looks good for first POST /widgets"; |
| 703 |
|
| 704 |
$response = dancer_response POST => '/widgets'; |
| 705 |
is $response->status, 202, "response for POST /widgets is 202"; |
| 706 |
is $response->content, "Widget #2 has been scheduled for creation", |
| 707 |
"response content looks good for second POST /widgets"; |
| 708 |
|
| 709 |
It's possible to test file uploads: |
| 710 |
|
| 711 |
post '/upload' => sub { return upload('image')->content }; |
| 712 |
|
| 713 |
$response = dancer_response(POST => '/upload', {files => [{name => 'image', filename => '/path/to/image.jpg'}]}); |
| 714 |
|
| 715 |
In addition, you can supply the file contents as the C<data> key: |
| 716 |
|
| 717 |
my $data = 'A test string that will pretend to be file contents.'; |
| 718 |
$response = dancer_response(POST => '/upload', { |
| 719 |
files => [{name => 'test', filename => "filename.ext", data => $data}] |
| 720 |
}); |
| 721 |
|
| 722 |
You can also supply a hashref of headers: |
| 723 |
|
| 724 |
headers => { 'Content-Type' => 'text/plain' } |
| 725 |
|
| 726 |
=head2 response_status_is ($request, $expected, $test_name); |
| 727 |
|
| 728 |
Asserts that Dancer2's response for the given request has a status equal to the |
| 729 |
one given. |
| 730 |
|
| 731 |
response_status_is [GET => '/'], 200, "response for GET / is 200"; |
| 732 |
|
| 733 |
=head2 route_exists([$method, $path], $test_name) |
| 734 |
|
| 735 |
Asserts that the given request matches a route handler in Dancer2's |
| 736 |
registry. If the route would have returned a 404, the route still exists |
| 737 |
and this test will pass. |
| 738 |
|
| 739 |
Note that because Dancer2 uses the default route handler |
| 740 |
L<Dancer2::Handler::File> to match files in the public folder when |
| 741 |
no other route matches, this test will always pass. |
| 742 |
You can disable the default route handlers in the configs but you probably |
| 743 |
want L<Dancer2::Test/response_status_is> or L<Dancer2::Test/dancer_response> |
| 744 |
|
| 745 |
route_exists [GET => '/'], "GET / is handled"; |
| 746 |
|
| 747 |
=head2 route_doesnt_exist([$method, $path], $test_name) |
| 748 |
|
| 749 |
Asserts that the given request does not match any route handler |
| 750 |
in Dancer2's registry. |
| 751 |
|
| 752 |
Note that this test is likely to always fail as any route not matched will |
| 753 |
be handled by the default route handler in L<Dancer2::Handler::File>. |
| 754 |
This can be disabled in the configs. |
| 755 |
|
| 756 |
route_doesnt_exist [GET => '/bogus_path'], "GET /bogus_path is not handled"; |
| 757 |
|
| 758 |
=head2 response_status_isnt([$method, $path], $status, $test_name) |
| 759 |
|
| 760 |
Asserts that the status of Dancer2's response is not equal to the |
| 761 |
one given. |
| 762 |
|
| 763 |
response_status_isnt [GET => '/'], 404, "response for GET / is not a 404"; |
| 764 |
|
| 765 |
=head2 response_content_is([$method, $path], $expected, $test_name) |
| 766 |
|
| 767 |
Asserts that the response content is equal to the C<$expected> string. |
| 768 |
|
| 769 |
response_content_is [GET => '/'], "Hello, World", |
| 770 |
"got expected response content for GET /"; |
| 771 |
|
| 772 |
=head2 response_content_isnt([$method, $path], $not_expected, $test_name) |
| 773 |
|
| 774 |
Asserts that the response content is not equal to the C<$not_expected> string. |
| 775 |
|
| 776 |
response_content_isnt [GET => '/'], "Hello, World", |
| 777 |
"got expected response content for GET /"; |
| 778 |
|
| 779 |
=head2 response_content_like([$method, $path], $regexp, $test_name) |
| 780 |
|
| 781 |
Asserts that the response content for the given request matches the regexp |
| 782 |
given. |
| 783 |
|
| 784 |
response_content_like [GET => '/'], qr/Hello, World/, |
| 785 |
"response content looks good for GET /"; |
| 786 |
|
| 787 |
=head2 response_content_unlike([$method, $path], $regexp, $test_name) |
| 788 |
|
| 789 |
Asserts that the response content for the given request does not match the regexp |
| 790 |
given. |
| 791 |
|
| 792 |
response_content_unlike [GET => '/'], qr/Page not found/, |
| 793 |
"response content looks good for GET /"; |
| 794 |
|
| 795 |
=head2 response_content_is_deeply([$method, $path], $expected_struct, $test_name) |
| 796 |
|
| 797 |
Similar to response_content_is(), except that if response content and |
| 798 |
$expected_struct are references, it does a deep comparison walking each data |
| 799 |
structure to see if they are equivalent. |
| 800 |
|
| 801 |
If the two structures are different, it will display the place where they start |
| 802 |
differing. |
| 803 |
|
| 804 |
response_content_is_deeply [GET => '/complex_struct'], |
| 805 |
{ foo => 42, bar => 24}, |
| 806 |
"got expected response structure for GET /complex_struct"; |
| 807 |
|
| 808 |
=head2 response_is_file ($request, $test_name); |
| 809 |
|
| 810 |
=head2 response_headers_are_deeply([$method, $path], $expected, $test_name) |
| 811 |
|
| 812 |
Asserts that the response headers data structure equals the one given. |
| 813 |
|
| 814 |
response_headers_are_deeply [GET => '/'], [ 'X-Powered-By' => 'Dancer2 1.150' ]; |
| 815 |
|
| 816 |
=head2 response_headers_include([$method, $path], $expected, $test_name) |
| 817 |
|
| 818 |
Asserts that the response headers data structure includes some of the defined ones. |
| 819 |
|
| 820 |
response_headers_include [GET => '/'], [ 'Content-Type' => 'text/plain' ]; |
| 821 |
|
| 822 |
=head2 route_pod_coverage() |
| 823 |
|
| 824 |
Returns a structure describing pod coverage in your apps |
| 825 |
|
| 826 |
for one app like this: |
| 827 |
|
| 828 |
package t::lib::TestPod; |
| 829 |
use Dancer2; |
| 830 |
|
| 831 |
=head1 NAME |
| 832 |
|
| 833 |
TestPod |
| 834 |
|
| 835 |
=head2 ROUTES |
| 836 |
|
| 837 |
=over |
| 838 |
|
| 839 |
=cut |
| 840 |
|
| 841 |
=item get "/in_testpod" |
| 842 |
|
| 843 |
testpod |
| 844 |
|
| 845 |
=cut |
| 846 |
|
| 847 |
get '/in_testpod' => sub { |
| 848 |
return 'get in_testpod'; |
| 849 |
}; |
| 850 |
|
| 851 |
get '/hello' => sub { |
| 852 |
return "hello world"; |
| 853 |
}; |
| 854 |
|
| 855 |
=item post '/in_testpod/*' |
| 856 |
|
| 857 |
post in_testpod |
| 858 |
|
| 859 |
=cut |
| 860 |
|
| 861 |
post '/in_testpod/*' => sub { |
| 862 |
return 'post in_testpod'; |
| 863 |
}; |
| 864 |
|
| 865 |
=back |
| 866 |
|
| 867 |
=head2 SPECIALS |
| 868 |
|
| 869 |
=head3 PUBLIC |
| 870 |
|
| 871 |
=over |
| 872 |
|
| 873 |
=item get "/me:id" |
| 874 |
|
| 875 |
=cut |
| 876 |
|
| 877 |
get "/me:id" => sub { |
| 878 |
return "ME"; |
| 879 |
}; |
| 880 |
|
| 881 |
=back |
| 882 |
|
| 883 |
=head3 PRIVAT |
| 884 |
|
| 885 |
=over |
| 886 |
|
| 887 |
=item post "/me:id" |
| 888 |
|
| 889 |
post /me:id |
| 890 |
|
| 891 |
=cut |
| 892 |
|
| 893 |
post "/me:id" => sub { |
| 894 |
return "ME"; |
| 895 |
}; |
| 896 |
|
| 897 |
=back |
| 898 |
|
| 899 |
=cut |
| 900 |
|
| 901 |
1; |
| 902 |
|
| 903 |
route_pod_coverage; |
| 904 |
|
| 905 |
would return something like: |
| 906 |
|
| 907 |
{ |
| 908 |
't::lib::TestPod' => { |
| 909 |
'has_pod' => 1, |
| 910 |
'routes' => [ |
| 911 |
"post /in_testpod/*", |
| 912 |
"post /me:id", |
| 913 |
"get /in_testpod", |
| 914 |
"get /hello", |
| 915 |
"get /me:id" |
| 916 |
], |
| 917 |
'undocumented_routes' => [ |
| 918 |
"get /hello" |
| 919 |
] |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
=head2 is_pod_covered('is pod covered') |
| 924 |
|
| 925 |
Asserts that your apps have pods for all routes |
| 926 |
|
| 927 |
is_pod_covered 'is pod covered' |
| 928 |
|
| 929 |
to avoid test failures, you should document all your routes with one of the following: |
| 930 |
head1, head2,head3,head4, item. |
| 931 |
|
| 932 |
ex: |
| 933 |
|
| 934 |
=item get '/login' |
| 935 |
|
| 936 |
route to login |
| 937 |
|
| 938 |
=cut |
| 939 |
|
| 940 |
if you use: |
| 941 |
|
| 942 |
any '/myaction' => sub { |
| 943 |
# code |
| 944 |
} |
| 945 |
|
| 946 |
or |
| 947 |
|
| 948 |
any ['get', 'post'] => '/myaction' => sub { |
| 949 |
# code |
| 950 |
}; |
| 951 |
|
| 952 |
you need to create pods for each one of the routes created there. |
| 953 |
|
| 954 |
=head2 import |
| 955 |
|
| 956 |
When Dancer2::Test is imported, it should be passed all the |
| 957 |
applications that are supposed to be tested. |
| 958 |
|
| 959 |
If none passed, then the caller is supposed to be the sole application |
| 960 |
to test. |
| 961 |
|
| 962 |
# t/sometest.t |
| 963 |
|
| 964 |
use t::lib::Foo; |
| 965 |
use t::lib::Bar; |
| 966 |
|
| 967 |
use Dancer2::Test apps => ['t::lib::Foo', 't::lib::Bar']; |
| 968 |
|
| 969 |
=head1 AUTHOR |
| 970 |
|
| 971 |
Dancer Core Developers |
| 972 |
|
| 973 |
=head1 COPYRIGHT AND LICENSE |
| 974 |
|
| 975 |
This software is copyright (c) 2026 by Alexis Sukrieh. |
| 976 |
|
| 977 |
This is free software; you can redistribute it and/or modify it under |
| 978 |
the same terms as the Perl 5 programming language system itself. |
| 979 |
|
| 980 |
=cut |