1Test::Fake::HTTPD(3pm)User Contributed Perl DocumentationTest::Fake::HTTPD(3pm)
2
3
4

NAME

6       Test::Fake::HTTPD - a fake HTTP server
7

SYNOPSIS

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 "You can connect to your server at %s.\n", $httpd->host_port;
26           # or
27           printf "You can connect to your server at 127.0.0.1:%d.\n", $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

DESCRIPTION

53       Test::Fake::HTTPD is a fake HTTP server module for testing.
54

FUNCTIONS

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

METHODS

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           ·       "port"
112
113                   local bind port number (default: auto detection)
114
115             my $httpd = Test::Fake::HTTPD->new(
116                 timeout => 10,
117                 listen  => 10,
118                 port    => 3333,
119             );
120
121       ·   "run( sub { ... } )"
122
123           Starts this HTTP server.
124
125             $httpd->run(sub { ... });
126
127       ·   "scheme"
128
129           Returns a scheme of running, "http" or "https".
130
131             my $scheme = $httpd->scheme;
132
133       ·   "port"
134
135           Returns a port number of running.
136
137             my $port = $httpd->port;
138
139       ·   "host_port"
140
141           Returns a URI host_port of running. ("127.0.0.1:{port}")
142
143             my $host_port = $httpd->host_port;
144
145       ·   "endpoint"
146
147           Returns an endpoint URI of running. ("http://127.0.0.1:{port}" URI
148           object)
149
150             use LWP::UserAgent;
151
152             my $res = LWP::UserAgent->new->get($httpd->endpoint);
153
154             my $url = $httpd->endpoint;
155             $url->path('/foo/bar');
156             my $res = LWP::UserAgent->new->get($url);
157

AUTHOR

159       NAKAGAWA Masaki <masaki@cpan.org>
160

THANKS TO

162       xaicron
163

LICENSE

165       This library is free software; you can redistribute it and/or modify it
166       under the same terms as Perl itself.
167

SEE ALSO

169       Test::TCP, HTTP::Daemon, HTTP::Daemon::SSL, HTTP::Message::PSGI
170
171
172
173perl v5.30.1                      2020-01-30            Test::Fake::HTTPD(3pm)
Impressum