| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use utf8; |
| 4 |
|
| 5 |
use Encode 'encode_utf8'; |
| 6 |
use Test::More; |
| 7 |
use Plack::Test; |
| 8 |
use HTTP::Request::Common; |
| 9 |
use Path::Tiny (); |
| 10 |
use Ref::Util qw<is_coderef>; |
| 11 |
|
| 12 |
{ |
| 13 |
package StaticContent; |
| 14 |
|
| 15 |
use Dancer2; |
| 16 |
use Encode 'encode_utf8'; |
| 17 |
|
| 18 |
set views => 't/corpus/static'; |
| 19 |
set public_dir => 't/corpus/static'; |
| 20 |
|
| 21 |
get '/' => sub { |
| 22 |
send_file 'index.html'; |
| 23 |
}; |
| 24 |
|
| 25 |
get '/illegal' => sub { |
| 26 |
send_file '../index.html'; |
| 27 |
}; |
| 28 |
|
| 29 |
prefix '/some' => sub { |
| 30 |
get '/image' => sub { |
| 31 |
send_file '1x1.png'; |
| 32 |
return "send_file returns; this content is ignored"; |
| 33 |
}; |
| 34 |
}; |
| 35 |
|
| 36 |
get '/stringref' => sub { |
| 37 |
my $string = encode_utf8("This is əɯosəʍɐ an test string"); |
| 38 |
send_file( \$string ); |
| 39 |
}; |
| 40 |
|
| 41 |
get '/filehandle' => sub { |
| 42 |
open my $fh, "<:raw", __FILE__; |
| 43 |
send_file( $fh, content_type => 'text/plain', charset => 'utf-8' ); |
| 44 |
}; |
| 45 |
|
| 46 |
get '/check_content_type' => sub { |
| 47 |
my $file = Path::Tiny::path(__FILE__)->absolute->stringify; |
| 48 |
send_file($file, content_type => 'image/png', system_path => 1); |
| 49 |
}; |
| 50 |
|
| 51 |
get '/no_streaming' => sub { |
| 52 |
my $file = Path::Tiny::path(__FILE__)->absolute->stringify; |
| 53 |
send_file( $file, system_path => 1, streaming => 0 ); |
| 54 |
}; |
| 55 |
|
| 56 |
get '/options_streaming' => sub { |
| 57 |
my $file = Path::Tiny::path(__FILE__)->absolute->stringify; |
| 58 |
send_file( $file, system_path => 1, streaming => 1 ); |
| 59 |
}; |
| 60 |
|
| 61 |
get '/content_disposition/attachment' => sub { |
| 62 |
send_file('1x1.png', filename => '1x1.png'); |
| 63 |
}; |
| 64 |
|
| 65 |
get '/content_disposition/inline' => sub { |
| 66 |
send_file('1x1.png', filename => '1x1.png', content_disposition => 'inline'); |
| 67 |
}; |
| 68 |
} |
| 69 |
|
| 70 |
my $app = StaticContent->to_app; |
| 71 |
ok( is_coderef($app), 'Got app' ); |
| 72 |
|
| 73 |
test_psgi $app, sub { |
| 74 |
my $cb = shift; |
| 75 |
|
| 76 |
subtest "Text content" => sub { |
| 77 |
my $r = $cb->( GET '/' ); |
| 78 |
|
| 79 |
is( $r->code, 200, 'send_file sets the status to 200' ); |
| 80 |
|
| 81 |
my $charset = $r->headers->content_type_charset; |
| 82 |
is( $charset, 'UTF-8', 'Text content type has default charset' ); |
| 83 |
my $test_string = encode_utf8('áéíóú'); |
| 84 |
like( |
| 85 |
$r->content, |
| 86 |
qr{$test_string}, |
| 87 |
'Text content contains UTF-8 characters', |
| 88 |
); |
| 89 |
}; |
| 90 |
|
| 91 |
subtest "Binary content" => sub { |
| 92 |
my $r = $cb->( GET '/some/image' ); |
| 93 |
|
| 94 |
is( $r->code, 200, 'send_file sets the status to 200 (binary content)' ); |
| 95 |
unlike( $r->content, qr/send_file returns/, |
| 96 |
"send_file returns immediately with content"); |
| 97 |
is( $r->header( 'Content-Type' ), 'image/png', |
| 98 |
'correct content_type in response' ); |
| 99 |
}; |
| 100 |
|
| 101 |
subtest "string refs" => sub { |
| 102 |
my $r = $cb->( GET '/stringref' ); |
| 103 |
|
| 104 |
is( $r->code, 200, 'send_file set status to 200 (string ref)'); |
| 105 |
like( $r->content, qr{test string}, 'stringref content' ); |
| 106 |
}; |
| 107 |
|
| 108 |
subtest "filehandles" => sub { |
| 109 |
my $r = $cb->( GET '/filehandle' ); |
| 110 |
|
| 111 |
is( $r->code, 200, 'send_file set status to 200 (filehandle)'); |
| 112 |
is( $r->content_type, 'text/plain', 'expected content_type'); |
| 113 |
is( $r->content_type_charset, 'UTF-8', 'expected charset'); |
| 114 |
like( $r->content, qr{package StaticContent}, 'filehandle content' ); |
| 115 |
}; |
| 116 |
|
| 117 |
subtest "no streaming" => sub { |
| 118 |
my $r = $cb->( GET '/no_streaming' ); |
| 119 |
is( $r->code, 200, 'send_file set status to 200 (no streaming)'); |
| 120 |
like( $r->content, qr{package StaticContent}, 'no streaming - content' ); |
| 121 |
}; |
| 122 |
|
| 123 |
subtest "options streaming" => sub { |
| 124 |
my $r = $cb->( GET '/options_streaming' ); |
| 125 |
is( $r->code, 200, 'send_file set status to 200 (options streaming)'); |
| 126 |
like( $r->content, qr{package StaticContent}, 'options streaming - content' ); |
| 127 |
}; |
| 128 |
|
| 129 |
subtest 'send_file returns correct content type' => sub { |
| 130 |
my $r = $cb->( GET '/check_content_type' ); |
| 131 |
|
| 132 |
ok($r->is_success, 'send_file returns success'); |
| 133 |
is($r->content_type, 'image/png', 'send_file returns correct content_type'); |
| 134 |
}; |
| 135 |
|
| 136 |
subtest 'Content-Disposition defaults to "attachment"' => sub { |
| 137 |
my $r = $cb->( GET '/content_disposition/attachment' ); |
| 138 |
ok($r->is_success, 'send_file returns success'); |
| 139 |
is($r->header('Content-Disposition'), 'attachment; filename="1x1.png"', 'send_file returns correct attachment Content-Disposition'); |
| 140 |
}; |
| 141 |
|
| 142 |
subtest 'Content-Disposition supports "inline"' => sub { |
| 143 |
my $r = $cb->( GET '/content_disposition/inline' ); |
| 144 |
ok($r->is_success, 'send_file returns success'); |
| 145 |
is($r->header('Content-Disposition'), 'inline; filename="1x1.png"', 'send_file returns correct inline Content-Disposition'); |
| 146 |
}; |
| 147 |
|
| 148 |
subtest "Illegal path" => sub { |
| 149 |
my $r = $cb->( GET '/illegal' ); |
| 150 |
is( $r->code, 403, 'Illegal path returns 403' ); |
| 151 |
is( |
| 152 |
$r->content, |
| 153 |
'Forbidden', |
| 154 |
'Text content contains UTF-8 characters', |
| 155 |
); |
| 156 |
}; |
| 157 |
|
| 158 |
}; |
| 159 |
|
| 160 |
done_testing; |