1Plack::Test(3)        User Contributed Perl Documentation       Plack::Test(3)
2
3
4

NAME

6       Plack::Test - Test PSGI applications with various backends
7

SYNOPSIS

9         use Plack::Test;
10         use HTTP::Request::Common;
11
12         # Simple OO interface
13         my $app = sub { return [ 200, [], [ "Hello" ] ] };
14         my $test = Plack::Test->create($app);
15
16         my $res = $test->request(GET "/");
17         is $res->content, "Hello";
18
19         # traditional - named params
20         test_psgi
21             app => sub {
22                 my $env = shift;
23                 return [ 200, [ 'Content-Type' => 'text/plain' ], [ "Hello World" ] ],
24             },
25             client => sub {
26                 my $cb  = shift;
27                 my $req = HTTP::Request->new(GET => "http://localhost/hello");
28                 my $res = $cb->($req);
29                 like $res->content, qr/Hello World/;
30             };
31
32         # positional params (app, client)
33         my $app = sub { return [ 200, [], [ "Hello" ] ] };
34         test_psgi $app, sub {
35             my $cb  = shift;
36             my $res = $cb->(GET "/");
37             is $res->content, "Hello";
38         };
39

DESCRIPTION

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

METHODS

48       create
49             $test = Plack::Test->create($app, %options);
50
51           creates an instance of Plack::Test implementation class. $app has
52           to be a valid PSGI application code reference.
53
54       request
55             $res = $test->request($request);
56
57           takes an HTTP::Request object, runs it through the PSGI application
58           to test and returns an HTTP::Response object.
59

FUNCTIONS

61       Plack::Test also provides a functional interface that takes two
62       callbacks, each of which represents PSGI application and HTTP client
63       code that tests the application.
64
65       test_psgi
66             test_psgi $app, $client;
67             test_psgi app => $app, client => $client;
68
69           Runs the client test code $client against a PSGI application $app.
70           The client callback gets one argument $cb, a callback that accepts
71           an "HTTP::Request" object and returns an "HTTP::Response" object.
72
73           Use HTTP::Request::Common to import shortcuts for creating requests
74           for "GET", "POST", "DELETE", and "PUT" operations.
75
76           For your convenience, the "HTTP::Request" given to the callback
77           automatically uses the HTTP protocol and the localhost (127.0.0.1
78           by default), so the following code just works:
79
80             use HTTP::Request::Common;
81             test_psgi $app, sub {
82                 my $cb  = shift;
83                 my $res = $cb->(GET "/hello");
84             };
85
86           Note that however, it is not a good idea to pass an arbitrary (i.e.
87           user-input) string to "GET" or even "HTTP::Request->new" by
88           assuming that it always represents a path, because:
89
90             my $req = GET "//foo/bar";
91
92           would represent a request for a URL that has no scheme, has a
93           hostname foo and a path /bar, instead of a path //foo/bar which you
94           might actually want.
95

OPTIONS

97       Specify the Plack::Test backend using the environment variable
98       "PLACK_TEST_IMPL" or $Plack::Test::Impl package variable.
99
100       The available values for the backend are:
101
102       MockHTTP
103           (Default) Creates a PSGI env hash out of HTTP::Request object, runs
104           the PSGI application in-process and returns HTTP::Response.
105
106       Server
107           Runs one of Plack::Handler backends ("Standalone" by default) and
108           sends live HTTP requests to test.
109
110       ExternalServer
111           Runs tests against an external server specified in the
112           "PLACK_TEST_EXTERNALSERVER_URI" environment variable instead of
113           spawning the application in a server locally.
114
115       For instance, test your application with the
116       "HTTP::Server::ServerSimple" server backend with:
117
118         > env PLACK_TEST_IMPL=Server PLACK_SERVER=HTTP::Server::ServerSimple \
119           prove -l t/test.t
120

AUTHOR

122       Tatsuhiko Miyagawa
123
124
125
126perl v5.30.1                      2020-01-30                    Plack::Test(3)
Impressum