1Test::Fake::HTTPD(3pm)User Contributed Perl DocumentationTest::Fake::HTTPD(3pm)
2
3
4
6 Test::Fake::HTTPD - a fake HTTP server
7
9 DSL-style
10
11 use Test::Fake::HTTPD;
12
13 my $httpd = run_http_server {
14 my $req = shift;
15 # ...
16
17 # 1. HTTP::Response ok
18 return $http_response;
19 # 2. Plack::Response ok
20 return $plack_response;
21 # 3. PSGI response ok
22 return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello World' ] ];
23 };
24
25 printf "Listening on address:port %s\n", $httpd->host_port;
26 # or
27 printf "Listening on address %s port %s\n", $httpd->host, $httpd->port;
28
29 # access to fake HTTP server
30 use LWP::UserAgent;
31 my $res = LWP::UserAgent->new->get($httpd->endpoint); # "http://127.0.0.1:{port}"
32
33 # Stop http server automatically at destruction time.
34
35 OO-style
36
37 use Test::Fake::HTTPD;
38
39 my $httpd = Test::Fake::HTTPD->new(
40 timeout => 5,
41 daemon_args => { ... }, # HTTP::Daemon args
42 );
43
44 $httpd->run(sub {
45 my $req = shift;
46 # ...
47 [ 200, [ 'Content-Type', 'text/plain' ], [ 'Hello World' ] ];
48 });
49
50 # Stop http server automatically at destruction time.
51
53 Test::Fake::HTTPD is a fake HTTP server module for testing.
54
56 • "run_http_server { ... }"
57
58 Starts HTTP server and returns the guard instance.
59
60 my $httpd = run_http_server {
61 my $req = shift;
62 # ...
63 return $http_or_plack_or_psgi_res;
64 };
65
66 # can use $httpd guard object, same as OO-style
67 LWP::UserAgent->new->get($httpd->endpoint);
68
69 • "run_https_server { ... }"
70
71 Starts HTTPS server and returns the guard instance.
72
73 If you use this method, you MUST install HTTP::Daemon::SSL.
74
75 extra_daemon_args
76 SSL_key_file => "certs/server-key.pem",
77 SSL_cert_file => "certs/server-cert.pem";
78
79 my $httpd = run_https_server {
80 my $req = shift;
81 # ...
82 return $http_or_plack_or_psgi_res;
83 };
84
85 # can use $httpd guard object, same as OO-style
86 my $ua = LWP::UserAgent->new(
87 ssl_opts => {
88 SSL_verify_mode => 0,
89 verify_hostname => 0,
90 },
91 );
92 $ua->get($httpd->endpoint);
93
95 • "new( %args )"
96
97 Returns a new instance.
98
99 my $httpd = Test::Fake::HTTPD->new(%args);
100
101 %args are:
102
103 • "timeout"
104
105 timeout value (default: 5)
106
107 • "listen"
108
109 queue size for listen (default: 5)
110
111 • "host"
112
113 local address to listen on (default: 127.0.0.1)
114
115 • "port"
116
117 TCP port to listen on (default: auto detection)
118
119 my $httpd = Test::Fake::HTTPD->new(
120 timeout => 10,
121 listen => 10,
122 port => 3333,
123 );
124
125 • "run( sub { ... } )"
126
127 Starts this HTTP server.
128
129 $httpd->run(sub { ... });
130
131 • "scheme"
132
133 Returns a scheme of running, "http" or "https".
134
135 my $scheme = $httpd->scheme;
136
137 • "host"
138
139 Returns the address the server is listening on.
140
141 • "port"
142
143 Returns the TCP port the server is listening on.
144
145 my $port = $httpd->port;
146
147 • "host_port"
148
149 Returns the host:port from "endpoint" (e.g., "127.0.0.1:1234",
150 "[::1]:1234").
151
152 my $host_port = $httpd->host_port;
153
154 • "endpoint"
155
156 Returns a URI object to the running server (e.g.,
157 "http://127.0.0.1:1234", "https://[::1]:1234"). If "host" returns
158 "undef", '', '0.0.0.0', or '::', the host portion of the URI is set
159 to "localhost".
160
161 use LWP::UserAgent;
162
163 my $res = LWP::UserAgent->new->get($httpd->endpoint);
164
165 my $url = $httpd->endpoint;
166 $url->path('/foo/bar');
167 my $res = LWP::UserAgent->new->get($url);
168
170 NAKAGAWA Masaki <masaki@cpan.org>
171
173 xaicron
174
176 This library is free software; you can redistribute it and/or modify it
177 under the same terms as Perl itself.
178
180 Test::TCP, HTTP::Daemon, HTTP::Daemon::SSL, HTTP::Message::PSGI
181
182
183
184perl v5.36.0 2022-07-22 Test::Fake::HTTPD(3pm)