| 1 |
use strict; |
| 2 |
use warnings FATAL => 'all'; |
| 3 |
use Test::More; |
| 4 |
use Test::Fatal; |
| 5 |
|
| 6 |
use Dancer2::Core::Request; |
| 7 |
|
| 8 |
use Carp; |
| 9 |
use File::Temp 0.22; |
| 10 |
use Path::Tiny qw< path >; |
| 11 |
use Encode qw(encode_utf8); |
| 12 |
|
| 13 |
diag "If you want extra speed, install URL::Encode::XS" |
| 14 |
if !$Dancer2::Core::Request::XS_URL_DECODE; |
| 15 |
diag "If you want extra speed, install CGI::Deurl::XS" |
| 16 |
if !$Dancer2::Core::Request::XS_PARSE_QUERY_STRING; |
| 17 |
|
| 18 |
sub test_path { |
| 19 |
my ( $file, $dir ) = @_; |
| 20 |
is path($file)->parent, $dir, "dir of $file is $dir"; |
| 21 |
} |
| 22 |
|
| 23 |
sub run_test { |
| 24 |
my $filename = "some_\x{1A9}_file.txt"; |
| 25 |
|
| 26 |
my $content = qq{------BOUNDARY |
| 27 |
Content-Disposition: form-data; name="test_upload_file"; filename="$filename" |
| 28 |
Content-Type: text/plain |
| 29 |
|
| 30 |
SHOGUN |
| 31 |
------BOUNDARY |
| 32 |
Content-Disposition: form-data; name="test_upload_file"; filename="yappo2.txt" |
| 33 |
Content-Type: text/plain |
| 34 |
|
| 35 |
SHOGUN2 |
| 36 |
------BOUNDARY |
| 37 |
Content-Disposition: form-data; name="test_upload_file3"; filename="yappo3.txt" |
| 38 |
Content-Type: text/plain |
| 39 |
|
| 40 |
SHOGUN3 |
| 41 |
------BOUNDARY |
| 42 |
Content-Disposition: form-data; name="test_upload_file4"; filename="yappo4.txt" |
| 43 |
Content-Type: text/plain |
| 44 |
|
| 45 |
SHOGUN4 |
| 46 |
------BOUNDARY |
| 47 |
Content-Disposition: form-data; name="test_upload_file4"; filename="yappo5.txt" |
| 48 |
Content-Type: text/plain |
| 49 |
|
| 50 |
SHOGUN4 |
| 51 |
------BOUNDARY |
| 52 |
Content-Disposition: form-data; name="test_upload_file6"; filename="yappo6.txt" |
| 53 |
Content-Type: text/plain |
| 54 |
|
| 55 |
SHOGUN6 |
| 56 |
------BOUNDARY-- |
| 57 |
}; |
| 58 |
$content =~ s/\r\n/\n/g; |
| 59 |
$content =~ s/\n/\r\n/g; |
| 60 |
$content = encode_utf8($content); |
| 61 |
|
| 62 |
|
| 63 |
do { |
| 64 |
open my $in, '<', \$content; |
| 65 |
my $req = Dancer2::Core::Request->new( |
| 66 |
env => { |
| 67 |
'psgi.input' => $in, |
| 68 |
CONTENT_LENGTH => length($content), |
| 69 |
CONTENT_TYPE => 'multipart/form-data; boundary=----BOUNDARY', |
| 70 |
REQUEST_METHOD => 'POST', |
| 71 |
SCRIPT_NAME => '/', |
| 72 |
SERVER_PORT => 80, |
| 73 |
} |
| 74 |
); |
| 75 |
|
| 76 |
my @undef = $req->upload('undef'); |
| 77 |
is @undef, 0, 'non-existent upload as array is empty'; |
| 78 |
my $undef = $req->upload('undef'); |
| 79 |
is $undef, undef, '... and non-existent upload as scalar is undef'; |
| 80 |
|
| 81 |
my @uploads = $req->upload('test_upload_file'); |
| 82 |
like $uploads[0]->content, qr|^SHOGUN|, |
| 83 |
"content for first upload is ok, via 'upload'"; |
| 84 |
like $uploads[1]->content, qr|^SHOGUN|, |
| 85 |
"... content for second as well"; |
| 86 |
is $req->uploads->{'test_upload_file4'}[0]->content, 'SHOGUN4', |
| 87 |
"... content for other also good"; |
| 88 |
|
| 89 |
note "headers and decoded filename"; |
| 90 |
my $encoded_filename = encode_utf8($filename); |
| 91 |
is_deeply $uploads[0]->headers, |
| 92 |
{ 'Content-Disposition' => |
| 93 |
qq[form-data; name="test_upload_file"; filename="$encoded_filename"], |
| 94 |
'Content-Type' => 'text/plain', |
| 95 |
}; |
| 96 |
is $uploads[0]->filename, $filename; |
| 97 |
|
| 98 |
note "type"; |
| 99 |
is $uploads[0]->type, 'text/plain'; |
| 100 |
|
| 101 |
my $test_upload_file3 = $req->upload('test_upload_file3'); |
| 102 |
is $test_upload_file3->content, 'SHOGUN3', |
| 103 |
"content for upload #3 as a scalar is good, via req->upload"; |
| 104 |
|
| 105 |
my @test_upload_file6 = $req->upload('test_upload_file6'); |
| 106 |
is $test_upload_file6[0]->content, 'SHOGUN6', |
| 107 |
"content for upload #6 is good"; |
| 108 |
|
| 109 |
is $test_upload_file6[0]->content(':raw'), 'SHOGUN6'; |
| 110 |
|
| 111 |
my $upload = $req->upload('test_upload_file6'); |
| 112 |
isa_ok $upload, 'Dancer2::Core::Request::Upload'; |
| 113 |
is $upload->filename, 'yappo6.txt', 'filename is ok'; |
| 114 |
ok $upload->file_handle, 'file handle is defined'; |
| 115 |
is $req->params->{'test_upload_file6'}, 'yappo6.txt', |
| 116 |
"filename is accessible via params"; |
| 117 |
|
| 118 |
# copy_to, link_to |
| 119 |
my $dest_dir = File::Temp::tempdir( CLEANUP => 1, TMPDIR => 1 ); |
| 120 |
my $dest_file = path( $dest_dir, $upload->basename )->stringify; |
| 121 |
$upload->copy_to($dest_file); |
| 122 |
ok( ( -f $dest_file ), "file '$dest_file' has been copied" ); |
| 123 |
|
| 124 |
my $dest_file_link = path( $dest_dir, "hardlink" )->stringify; |
| 125 |
$upload->link_to($dest_file_link); |
| 126 |
ok( ( -f $dest_file_link ), |
| 127 |
"hardlink '$dest_file_link' has been created" |
| 128 |
); |
| 129 |
|
| 130 |
# make sure cleanup is performed when the HTTP::Body object is purged |
| 131 |
my $file = $upload->tempname; |
| 132 |
ok( ( -f $file ), 'temp file exists while request object lives' ); |
| 133 |
|
| 134 |
# On Windows, files cannot be unlinked while open. Close all cached |
| 135 |
# file handles before destroying $req so that the temp dir cleanup |
| 136 |
# (which runs in File::Temp::Dir's DESTROY, triggered by freeing |
| 137 |
# $req's PSGI env) can succeed. |
| 138 |
if ( $^O eq 'MSWin32' ) { |
| 139 |
for my $up_or_list ( values %{ $req->uploads } ) { |
| 140 |
my @ups = ref $up_or_list eq 'ARRAY' |
| 141 |
? @{$up_or_list} |
| 142 |
: $up_or_list; |
| 143 |
$_->{_fh} = undef for @ups; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
undef $req; |
| 148 |
SKIP: { |
| 149 |
skip |
| 150 |
"Win32 can't remove file/link while open due to deadlock", |
| 151 |
1 |
| 152 |
if ( $^O eq 'MSWin32' ); |
| 153 |
ok( ( !-f $file ), |
| 154 |
'temp file is removed when request object dies' |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
note "testing failing open for tempfile"; |
| 160 |
{ |
| 161 |
|
| 162 |
# mocking open_file to make it fail |
| 163 |
my $upload_file_coderef; |
| 164 |
{ |
| 165 |
no strict 'refs'; |
| 166 |
$upload_file_coderef = |
| 167 |
*{"Dancer2::Core::Request::Upload::file_handle"}{CODE}; |
| 168 |
no warnings 'redefine'; |
| 169 |
*{"Dancer2::Core::Request::Upload::file_handle"} = sub { |
| 170 |
croak "Can't open mocked-tempfile using mode '<'"; |
| 171 |
}; |
| 172 |
} |
| 173 |
$upload->{_fh} = undef; |
| 174 |
like( |
| 175 |
exception { $upload->file_handle }, |
| 176 |
qr{Can't open mocked-tempfile using mode '<'}, |
| 177 |
); |
| 178 |
|
| 179 |
# unmock open_file |
| 180 |
{ |
| 181 |
no strict 'refs'; |
| 182 |
no warnings 'redefine'; |
| 183 |
*{"Dancer2::Core::Request::Upload::file_handle"} = |
| 184 |
$upload_file_coderef; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
unlink($file) if ( $^O eq 'MSWin32' ); |
| 189 |
}; |
| 190 |
} |
| 191 |
|
| 192 |
note "Run test with XS_URL_DECODE" if $Dancer2::Core::Request::XS_URL_DECODE; |
| 193 |
note "Run test with XS_PARSE_QUERY_STRING" |
| 194 |
if $Dancer2::Core::Request::XS_PARSE_QUERY_STRING; |
| 195 |
run_test(); |
| 196 |
if ($Dancer2::Core::Request::XS_PARSE_QUERY_STRING) { |
| 197 |
note "Run test without XS_PARSE_QUERY_STRING"; |
| 198 |
$Dancer2::Core::Request::XS_PARSE_QUERY_STRING = 0; |
| 199 |
$Dancer2::Core::Request::_count = 0; |
| 200 |
run_test(); |
| 201 |
} |
| 202 |
if ($Dancer2::Core::Request::XS_URL_DECODE) { |
| 203 |
note "Run test without XS_URL_DECODE"; |
| 204 |
$Dancer2::Core::Request::XS_URL_DECODE = 0; |
| 205 |
$Dancer2::Core::Request::_count = 0; |
| 206 |
run_test(); |
| 207 |
} |
| 208 |
|
| 209 |
done_testing; |