Plack::Test(3)
π¦ NAME π¦
Plack::Test β the way to test PSGI applications with a whole bunch of different backends, bestie π
β¨ SYNOPSIS β¨
okay so first you gotta pull it in, and you're gonna want HTTP::Request::Common too:
use Plack::Test;
use HTTP::Request::Common;
the simple OO interface (my personal fave, lowkey iconic):
# Simple OO interface
my $app = sub { return [ 200, [], [ "Hello" ] ] };
my $test = Plack::Test->create($app);
my $res = $test->request(GET "/");
is $res->content, "Hello";
the traditional one, with named params (iykyk):
# traditional - named params
test_psgi
app => sub {
my $env = shift;
return [ 200, [ 'Content-Type' => 'text/plain' ], [ "Hello World" ] ],
},
client => sub {
my $cb = shift;
my $req = HTTP::Request->new(GET => "http://localhost/hello");
my $res = $cb->($req);
like $res->content, qr/Hello World/;
};
and the positional params flavor β app, then client, no cap:
# positional params (app, client)
my $app = sub { return [ 200, [], [ "Hello" ] ] };
test_psgi $app, sub {
my $cb = shift;
my $res = $cb->(GET "/");
is $res->content, "Hello";
};
πΈ DESCRIPTION πΈ
So Plack::Test is like the unified interface for testing your PSGI applications using HTTP::Request and HTTP::Response objects. it also lets you run your PSGI applications in several different ways, which is the slay part. the default backend is Plack::Test::MockHTTP, but you can absolutely also use any Plack::Handler implementation to run live HTTP requests against an actual web server π«Ά
π METHODS π
create
$test = Plack::Test->create($app, %options);
makes you an instance of a Plack::Test implementation class. heads up: $app has to be a valid PSGI application code reference, no faking it β¨
request
$res = $test->request($request);
takes an HTTP::Request object, runs it through the PSGI application you're testing, and hands you back an HTTP::Response object. it's giving very "in and out, quick little errand" π
π FUNCTIONS π
Plack::Test ALSO provides a functional interface that takes two callbacks β one is your PSGI application, the other is the HTTP client code that tests it. two besties, one mission.
test_psgi
test_psgi $app, $client;
test_psgi app => $app, client => $client;
Runs the client test code $client against a PSGI application $app. the client callback gets exactly one argument: $cb, which is a callback that accepts an HTTP::Request object and returns an HTTP::Response object. so cute, so simple.
Use HTTP::Request::Common to import shortcuts for creating requests for GET, POST, DELETE, and PUT operations. yes queen, import them all.
and for your convenience π the HTTP::Request given to the callback automatically uses the HTTP protocol and the localhost (127.0.0.1 by default), so this code just works, no extra steps:
use HTTP::Request::Common;
test_psgi $app, sub {
my $cb = shift;
my $res = $cb->(GET "/hello");
};
β οΈ but listen. it is not a good idea to pass an arbitrary (i.e. user-input) string toGETor evenHTTP::Request->newwhile assuming it always represents a path. because:perl my $req = GET "//foo/bar";would represent a request for a URL that has no scheme, has a hostnamefooand a path/barβ instead of a path//foo/barwhich you might actually want. the audacity of that parse π so like, don't do that.
π OPTIONS π
Specify the Plack::Test backend using the environment variable PLACK_TEST_IMPL or the $Plack::Test::Impl package variable. your choice, bestie π
the available values for the backend are:
MockHTTPβ (Default) Creates a PSGI env hash out of anHTTP::Requestobject, runs the PSGI application in-process and returns anHTTP::Response. the cozy in-house option, no server required πServerβ Runs one of thePlack::Handlerbackends (Standaloneby default) and sends live HTTP requests to test. actual server energy π₯ExternalServerβ Runs tests against an external server specified in thePLACK_TEST_EXTERNALSERVER_URIenvironment variable instead of spawning the application in a server locally. it's giving long-distance relationship, but make it testing.
for instance β test your application with the HTTP::Server::ServerSimple server backend like this:
> env PLACK_TEST_IMPL=Server PLACK_SERVER=HTTP::Server::ServerSimple \
prove -l t/test.t
π§ AUTHOR π§
Tatsuhiko Miyagawa β the icon, the legend, the main character π
and that's the whole vibe check, bestie π this page came from perl v5.42.3, dated 2026-10-18, and you found it sitting in the Plack::Test(3) section of the User Contributed Perl Documentation. go forth and test those PSGI apps, periodt πβ¨
π¦ where this page comes from
- fedora 43: perl-Plack-Test 1.0055-1.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- fedora 44: perl-Plack-Test 1.0055-1.fc44 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms