| 1 |
# who test the tester? We do! |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use Ref::Util qw<is_arrayref>; |
| 6 |
use Path::Tiny (); |
| 7 |
|
| 8 |
BEGIN { |
| 9 |
# Disable route handlers so we can actually test route_exists |
| 10 |
# and route_doesnt_exist. Use config that disables default route handlers. |
| 11 |
$ENV{DANCER_CONFDIR} = Path::Tiny::path( __FILE__ )->parent->child('dancer-test')->stringify; |
| 12 |
} |
| 13 |
|
| 14 |
use Test::More tests => 46; |
| 15 |
|
| 16 |
use Dancer2; |
| 17 |
use Dancer2::Test; |
| 18 |
use Dancer2::Core::Request; |
| 19 |
use File::Temp; |
| 20 |
use Encode; |
| 21 |
use URI::Escape; |
| 22 |
|
| 23 |
$Dancer2::Test::NO_WARN = 1; |
| 24 |
|
| 25 |
my @routes = ( |
| 26 |
'/foo', |
| 27 |
[ GET => '/foo' ], |
| 28 |
Dancer2::Core::Request->new( |
| 29 |
env => { |
| 30 |
'psgi.url_scheme' => 'http', |
| 31 |
REQUEST_METHOD => 'GET', |
| 32 |
QUERY_STRING => '', |
| 33 |
SERVER_NAME => 'localhost', |
| 34 |
SERVER_PORT => 5000, |
| 35 |
SERVER_PROTOCOL => 'HTTP/1.1', |
| 36 |
SCRIPT_NAME => '', |
| 37 |
PATH_INFO => '/foo', |
| 38 |
REQUEST_URI => '/foo', |
| 39 |
} |
| 40 |
), |
| 41 |
); |
| 42 |
my $fighter = Dancer2::Core::Response->new( |
| 43 |
content => 'fighter', |
| 44 |
status => 404, |
| 45 |
); |
| 46 |
|
| 47 |
route_doesnt_exist $_ for (@routes, $fighter); |
| 48 |
|
| 49 |
|
| 50 |
get '/foo' => sub {'fighter'}; |
| 51 |
|
| 52 |
route_exists $_, "route $_ exists" for @routes; |
| 53 |
|
| 54 |
$fighter->status(200); |
| 55 |
push @routes, $fighter; |
| 56 |
|
| 57 |
for (@routes) { |
| 58 |
my $response = dancer_response $_; |
| 59 |
isa_ok $response => 'Dancer2::Core::Response'; |
| 60 |
is $response->content => 'fighter'; |
| 61 |
} |
| 62 |
|
| 63 |
response_content_is $_ => 'fighter', "response_content_is with $_" for @routes; |
| 64 |
response_content_isnt $_ => 'platypus', "response_content_isnt with $_" |
| 65 |
for @routes; |
| 66 |
response_content_like $_ => qr/igh/ for @routes; |
| 67 |
response_content_unlike $_ => qr/ought/ for @routes; |
| 68 |
|
| 69 |
response_status_is $_ => 200 for @routes; |
| 70 |
response_status_isnt $_ => 203 for @routes; |
| 71 |
|
| 72 |
## Check parameters get through ok |
| 73 |
get '/param' => sub { param('test') }; |
| 74 |
my $param_response = |
| 75 |
dancer_response( GET => '/param', { params => { test => 'hello' } } ); |
| 76 |
is $param_response->content, 'hello', 'PARAMS get echoed by route'; |
| 77 |
|
| 78 |
post '/upload' => sub { |
| 79 |
my $file = upload('test'); |
| 80 |
return $file->content; |
| 81 |
}; |
| 82 |
## Check we can upload files |
| 83 |
my $file_response = dancer_response( |
| 84 |
POST => '/upload', |
| 85 |
{ files => |
| 86 |
[ { filename => 'test.txt', name => 'test', data => 'testdata' } ] |
| 87 |
} |
| 88 |
); |
| 89 |
is $file_response->content, 'testdata', 'file uploaded with supplied data'; |
| 90 |
|
| 91 |
my $temp = File::Temp->new; |
| 92 |
print $temp 'testfile'; |
| 93 |
close($temp); |
| 94 |
|
| 95 |
$file_response = |
| 96 |
dancer_response( POST => '/upload', |
| 97 |
{ files => [ { filename => $temp->filename, name => 'test' } ] } ); |
| 98 |
is $file_response->content, 'testfile', 'file uploaded with supplied filename'; |
| 99 |
|
| 100 |
## Check multiselect/multi parameters get through ok |
| 101 |
get '/multi' => sub { |
| 102 |
my $t = param('test'); |
| 103 |
return 'bad' if !is_arrayref($t); |
| 104 |
my $p = join( '', @$t ); |
| 105 |
return $p; |
| 106 |
}; |
| 107 |
$param_response = |
| 108 |
dancer_response( GET => '/multi', |
| 109 |
{ params => { test => [ 'foo', 'bar' ] } } ); |
| 110 |
is $param_response->content, 'foobar', |
| 111 |
'multi values for same key get echoed back'; |
| 112 |
|
| 113 |
my $russian_test = |
| 114 |
decode( 'UTF-8', |
| 115 |
uri_unescape("%D0%B8%D1%81%D0%BF%D1%8B%D1%82%D0%B0%D0%BD%D0%B8%D0%B5") ); |
| 116 |
$param_response = |
| 117 |
dancer_response( GET => '/multi', |
| 118 |
{ params => { test => [ 'test/', $russian_test ] } } ); |
| 119 |
is decode( 'UTF-8', $param_response->content ), 'test/' . $russian_test, |
| 120 |
'multi utf8 value properly merge'; |
| 121 |
|
| 122 |
get '/headers' => sub { |
| 123 |
join " : ", request->header('X-Sent-By'), request->cookies->{foo}; |
| 124 |
}; |
| 125 |
note "extra headers in request"; |
| 126 |
sub extra_headers { |
| 127 |
my $sent_by = 'Dancer2::Test'; |
| 128 |
my $headers_test = dancer_response( GET => '/headers', |
| 129 |
{ |
| 130 |
headers => [ |
| 131 |
[ 'X-Sent-By' => $sent_by ], |
| 132 |
[ 'Cookie' => "foo=bar" ], |
| 133 |
], |
| 134 |
} |
| 135 |
); |
| 136 |
is $headers_test->content, "$sent_by : bar", |
| 137 |
"extra headers included in request"; |
| 138 |
} |
| 139 |
|
| 140 |
note "Run extra_headers test with XS_HTTP_COOKIES" |
| 141 |
if $Dancer2::Core::Request::XS_HTTP_COOKIES; |
| 142 |
extra_headers(); |
| 143 |
SKIP: { |
| 144 |
skip "HTTP::XSCookies not installed", 1 |
| 145 |
if !$Dancer2::Core::Request::XS_HTTP_COOKIES; |
| 146 |
note "Run extra_headers test without XS_HTTP_COOKIES"; |
| 147 |
$Dancer2::Core::Request::XS_HTTP_COOKIES = 0; |
| 148 |
extra_headers(); |
| 149 |
} |