VOOZH about

URL: https://manpages.org/placktest/3

⇱ man Plack::Test (3): Test PSGI applications with various backends


Plack::Test(3) Test PSGI applications with various backends

SYNOPSIS


use Plack::Test;
use HTTP::Request::Common;
# Simple OO interface
my $app = sub { return [ 200, [], [ "Hello "] ] };
my $test = Plack::Test->create($app);
my $res = $test->request(GET "/");
is $res->content, "Hello";
# 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/;
};
# 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

Plack::Test is a unified interface to test applications using HTTP::Request and HTTP::Response objects. It also allows you to run applications in various ways. The default backend is , but you may also use any Plack::Handler implementation to run live requests against a web server.

METHODS

create
 $test = Plack::Test->create($app, %options);

creates an instance of Plack::Test implementation class. has to be a valid application code reference.

request
 $res = $test->request($request);

takes an HTTP::Request object, runs it through the application to test and returns an HTTP::Response object.

FUNCTIONS

Plack::Test also provides a functional interface that takes two callbacks, each of which represents application and client code that tests the application.
test_psgi
 test_psgi $app, $client;
 test_psgi app => $app, client => $client;

Runs the client test code against a application . The client callback gets one argument , a callback that accepts an object and returns an object.

Use HTTP::Request::Common to import shortcuts for creating requests for , , , and operations.

For your convenience, the given to the callback automatically uses the protocol and the localhost (127.0.0.1 by default), so the following code just works:

 use HTTP::Request::Common;
 test_psgi $app, sub {
 my $cb = shift;
 my $res = $cb->(GET "/hello");
 };

Note that however, it is not a good idea to pass an arbitrary (i.e. user-input) string to or even by assuming that it always represents a path, because:

 my $req = GET "//foo/bar";

would represent a request for a that has no scheme, has a hostname foo and a path /bar, instead of a path //foo/bar which you might actually want.

OPTIONS

Specify the Plack::Test backend using the environment variable or package variable.

The available values for the backend are:

MockHTTP
(Default) Creates a env hash out of HTTP::Request object, runs the application in-process and returns HTTP::Response.
Server
Runs one of Plack::Handler backends ( by default) and sends live requests to test.
ExternalServer
Runs tests against an external server specified in the environment variable instead of spawning the application in a server locally.

For instance, test your application with the server backend with:

 > env PLACK_TEST_IMPL=Server PLACK_SERVER=HTTP::Server::ServerSimple \
 prove -l t/test.t

AUTHOR

Tatsuhiko Miyagawa