PAGI-Tools
view release on metacpan or search on metacpan
t/middleware/15-xsendfile.t view on Meta::CPAN
type => 'http.response.body',
file => '/absolute/path/to/file.txt',
});
};
my $wrapped = $mw->wrap($app);
my @sent;
run_async(async sub {
await $wrapped->(
{ type => 'http', path => '/' },
async sub { { type => 'http.disconnect' } },
async sub { my ($event) = @_; push @sent, $event },
);
});
my $xsendfile = find_header($sent[0]{headers}, 'X-Sendfile');
is $xsendfile, '/absolute/path/to/file.txt', 'path unchanged without mapping';
};
# =============================================================================
# Test: Non-file responses pass through unchanged
# =============================================================================
subtest 'regular body response passes through' => sub {
my $mw = PAGI::Middleware::XSendfile->new(type => 'X-Sendfile');
my $app = async sub {
my ($scope, $receive, $send) = @_;
await $send->({
type => 'http.response.start',
status => 200,
headers => [['content-type', 'text/plain']],
});
await $send->({
type => 'http.response.body',
body => 'Hello, World!',
});
};
my $wrapped = $mw->wrap($app);
my @sent;
run_async(async sub {
await $wrapped->(
{ type => 'http', path => '/' },
async sub { { type => 'http.disconnect' } },
async sub { my ($event) = @_; push @sent, $event },
);
});
is scalar(@sent), 2, 'two events sent';
# No X-Sendfile header
my $xsendfile = find_header($sent[0]{headers}, 'X-Sendfile');
is $xsendfile, undef, 'no X-Sendfile header for regular body';
is $sent[1]{body}, 'Hello, World!', 'body passed through unchanged';
};
subtest 'streaming response passes through' => sub {
my $mw = PAGI::Middleware::XSendfile->new(type => 'X-Sendfile');
my $app = async sub {
my ($scope, $receive, $send) = @_;
await $send->({
type => 'http.response.start',
status => 200,
headers => [],
});
await $send->({
type => 'http.response.body',
body => 'chunk1',
more => 1,
});
await $send->({
type => 'http.response.body',
body => 'chunk2',
more => 0,
});
};
my $wrapped = $mw->wrap($app);
my @sent;
run_async(async sub {
await $wrapped->(
{ type => 'http', path => '/' },
async sub { { type => 'http.disconnect' } },
async sub { my ($event) = @_; push @sent, $event },
);
});
is scalar(@sent), 3, 'three events sent (start + 2 body chunks)';
is $sent[1]{body}, 'chunk1', 'first chunk passed through';
is $sent[2]{body}, 'chunk2', 'second chunk passed through';
};
# =============================================================================
# Test: Filehandle with path() method
# =============================================================================
subtest 'filehandle with path method works' => sub {
my $mw = PAGI::Middleware::XSendfile->new(type => 'X-Sendfile');
# Create a mock filehandle with path method
my $mock_fh = bless {}, 'MockFHWithPath';
{
no strict 'refs';
*{'MockFHWithPath::path'} = sub { '/mocked/path/file.bin' };
}
my $app = async sub {
my ($scope, $receive, $send) = @_;
await $send->({
type => 'http.response.start',
status => 200,
headers => [],
});
await $send->({
type => 'http.response.body',
( run in 1.212 second using v1.01-cache-2.11-cpan-6aa56a78535 )