1Plack::Test(3) User Contributed Perl Documentation Plack::Test(3)
2
3
4
6 Plack::Test - Test PSGI applications with various backends
7
9 use Plack::Test;
10
11 # named params
12 test_psgi
13 app => sub {
14 my $env = shift;
15 return [ 200, [ 'Content-Type' => 'text/plain' ], [ "Hello World" ] ],
16 },
17 client => sub {
18 my $cb = shift;
19 my $req = HTTP::Request->new(GET => "http://localhost/hello");
20 my $res = $cb->($req);
21 like $res->content, qr/Hello World/;
22 };
23
24 use HTTP::Request::Common;
25
26 # positional params (app, client)
27 my $app = sub { return [ 200, [], [ "Hello "] ] };
28 test_psgi $app, sub {
29 my $cb = shift;
30 my $res = $cb->(GET "/");
31 is $res->content, "Hello";
32 };
33
35 Plack::Test is a unified interface to test PSGI applications using
36 standard HTTP::Request and HTTP::Response objects. It also allows you
37 to run PSGI applications in various ways, by default using "MockHTTP"
38 backend but can also use "Server" backend, which uses one of
39 Plack::Handler implementations to run the web server to do live HTTP
40 requests.
41
43 test_psgi
44 test_psgi $app, $client;
45 test_psgi app => $app, client => $client;
46
47 Runs the client test code $client against a PSGI application $app.
48 The client callback gets one argument $cb, that is a callback that
49 accepts an HTTP::Request object and returns an HTTP::Response
50 object.
51
52 For the convenience, HTTP::Request given to the callback is
53 automatically adjusted to the correct protocol (http) and host
54 names (127.0.0.1 by default), so the following code just works.
55
56 use HTTP::Request::Common;
57 test_psgi $app, sub {
58 my $cb = shift;
59 my $res = $cb->(GET "/hello");
60 };
61
62 Note that however, it is not a good idea to pass an arbitrary (i.e.
63 user-input) string to the "GET" function or even
64 "HTTP::Request->new" by assuming that it always represents a path,
65 because:
66
67 my $req = GET "//foo/bar";
68
69 would represent a request for a URL that has no scheme, has a
70 hostname foo and a path /bar, instead of a path //foo/bar which you
71 might actually want.
72
74 You can specify the Plack::Test backend using the environment variable
75 "PLACK_TEST_IMPL" or $Plack::Test::Impl package variable.
76
77 The available values for the backend are:
78
79 MockHTTP
80 (Default) Creates a PSGI env hash out of HTTP::Request object, runs
81 the PSGI application in-process and returns HTTP::Response.
82
83 Server
84 Runs one of Plack::Handler backends ("Standalone" by default) and
85 sends live HTTP requests to test.
86
87 ExternalServer
88 Runs tests against an external server specified in the
89 "PLACK_TEST_EXTERNALSERVER_URI" environment variable instead of
90 spawning the application in a server locally.
91
92 For instance, you can test your application with "ServerSimple" server
93 backends with:
94
95 > env PLACK_TEST_IMPL=Server PLACK_SERVER=ServerSimple prove -l t/test.t
96
98 Tatsuhiko Miyagawa
99
100
101
102perl v5.12.3 2011-06-22 Plack::Test(3)