PAGI-Server
view release on metacpan or search on metacpan
t/integration/response-integration.t view on Meta::CPAN
method => 'GET',
uri => URI->new("http://127.0.0.1:$port/"),
headers => { 'Connection' => 'close' },
);
is $response->code, 204, 'status 204 No Content';
is $response->decoded_content, '', 'empty body';
})->get;
1;
};
# If we got a spurious read error but tests passed, that's OK
if (!$result && $@ =~ /Spurious on_read/) {
pass('204 handled (spurious read warning expected)');
} elsif (!$result) {
fail("Unexpected error: $@");
}
};
subtest 'json error response pattern' => sub {
my $app = async sub {
my ($scope, $receive, $send) = @_;
my $res = PAGI::Response->new($scope);
$res->status(400)->json({ error => 'Bad Request', field => 'email' });
await $res->respond($send);
};
with_server($app, async sub {
my ($http, $port) = @_;
my $response = await $http->GET("http://127.0.0.1:$port/");
is $response->code, 400, 'status 400';
is $response->content_type, 'application/json', 'json content-type';
my $data = decode_json($response->decoded_content);
is $data->{error}, 'Bad Request', 'error message';
is $data->{field}, 'email', 'extra field';
})->get;
};
subtest 'cookie response' => sub {
my $app = async sub {
my ($scope, $receive, $send) = @_;
my $res = PAGI::Response->new($scope);
$res->cookie('session' => 'abc123', path => '/', httponly => 1)
->text('OK');
await $res->respond($send);
};
with_server($app, async sub {
my ($http, $port) = @_;
my $response = await $http->GET("http://127.0.0.1:$port/");
is $response->code, 200, 'status 200';
my $cookie = $response->header('Set-Cookie');
like $cookie, qr/session=abc123/, 'cookie value';
like $cookie, qr/Path=\//, 'cookie path';
like $cookie, qr/HttpOnly/, 'httponly flag';
})->get;
};
subtest 'streaming response' => sub {
my $app = async sub {
my ($scope, $receive, $send) = @_;
my $res = PAGI::Response->new($scope);
$res->content_type('text/plain')
->stream(async sub {
my ($writer) = @_;
await $writer->write("chunk1");
await $writer->write("chunk2");
await $writer->write("chunk3");
});
await $res->respond($send);
};
with_server($app, async sub {
my ($http, $port) = @_;
my $response = await $http->GET("http://127.0.0.1:$port/");
is $response->code, 200, 'status 200';
is $response->decoded_content, 'chunk1chunk2chunk3', 'all chunks received';
})->get;
};
subtest 'send_file response' => sub {
# Create temp file
my ($fh, $filename) = tempfile(UNLINK => 1, SUFFIX => '.txt');
print $fh "File content for testing";
close $fh;
my $app = async sub {
my ($scope, $receive, $send) = @_;
my $res = PAGI::Response->new($scope);
$res->send_file($filename);
await $res->respond($send);
};
with_server($app, async sub {
my ($http, $port) = @_;
my $response = await $http->GET("http://127.0.0.1:$port/");
is $response->code, 200, 'status 200';
is $response->content_type, 'text/plain', 'detected content-type';
is $response->decoded_content, 'File content for testing', 'file content';
})->get;
};
subtest 'send_file with attachment' => sub {
my ($fh, $filename) = tempfile(UNLINK => 1, SUFFIX => '.pdf');
print $fh "PDF content";
close $fh;
my $app = async sub {
my ($scope, $receive, $send) = @_;
my $res = PAGI::Response->new($scope);
$res->send_file($filename, filename => 'document.pdf');
await $res->respond($send);
};
with_server($app, async sub {
my ($http, $port) = @_;
my $response = await $http->GET("http://127.0.0.1:$port/");
( run in 1.123 second using v1.01-cache-2.11-cpan-6aa56a78535 )