Test::...::HTTPD(3pm)
π NAME π
Test::Fake::HTTPD β it's a fake HTTP server, bestie. Like, a whole lil server that exists purely so your tests have somewhere to play. No cap. π
β¨ SYNOPSIS β¨
Okay so there are two ways to do this. Two vibes. Pick your fighter. π
πΈ DSL-style
use Test::Fake::HTTPD;
my $httpd = run_http_server {
my $req = shift;
# ...
# 1. HTTP::Response ok
return $http_response;
# 2. Plack::Response ok
return $plack_response;
# 3. PSGI response ok
return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello World' ] ];
};
printf "Listening on address:port %s\n", $httpd->host_port;
# or
printf "Listening on address %s port %s\n", $httpd->host, $httpd->port;
# access to fake HTTP server
use LWP::UserAgent;
my $res = LWP::UserAgent->new->get($httpd->endpoint); # "http://127.0.0.1:{port}"
# Stop http server automatically at destruction time.
π¦ OO-style
use Test::Fake::HTTPD;
my $httpd = Test::Fake::HTTPD->new(
timeout => 5,
daemon_args => { ... }, # HTTP::Daemon args
);
$httpd->run(sub {
my $req = shift;
# ...
[ 200, [ 'Content-Type', 'text/plain' ], [ 'Hello World' ] ];
});
# Stop http server automatically at destruction time.
Notice that last line in both? The server shuts itself down when the object gets destroyed. She's self-sufficient. Main character energy fr fr. π
π DESCRIPTION π
Test::Fake::HTTPD is a fake HTTP server module for testing. That's it, that's the whole pitch. It's giving "I made you a server, it's fake, it's for tests, you're welcome." π
π FUNCTIONS π
run_http_server { ... }β Starts HTTP server and returns the guard instance.
my $httpd = run_http_server {
my $req = shift;
# ...
return $http_or_plack_or_psgi_res;
};
# can use $httpd guard object, same as OO-style
LWP::UserAgent->new->get($httpd->endpoint);
That $httpd guard object? Same one you'd get from OO-style. Reuse and slay. β¨
run_https_server { ... }β Starts HTTPS server and returns the guard instance.
Lowkey important: if you use this method, you MUST install HTTP::Daemon::SSL. Not optional. Not "eh maybe later". MUST. π
extra_daemon_args
SSL_key_file => "certs/server-key.pem",
SSL_cert_file => "certs/server-cert.pem";
my $httpd = run_https_server {
my $req = shift;
# ...
return $http_or_plack_or_psgi_res;
};
# can use $httpd guard object, same as OO-style
my $ua = LWP::UserAgent->new(
ssl_opts => {
SSL_verify_mode => 0,
verify_hostname => 0,
},
);
$ua->get($httpd->endpoint);
π METHODS π
new( %args )β Returns a new instance. Baby server, freshly born. πΌ
my $httpd = Test::Fake::HTTPD->new(%args);
%args are, bestie:
timeoutβ timeout value (default:5)listenβ queue size for listen (default:5)hostβ local address to listen on (default:127.0.0.1)portβ TCP port to listen on (default: auto detection)
my $httpd = Test::Fake::HTTPD->new(
timeout => 10,
listen => 10,
port => 3333,
);
run( sub { ... } )β Starts this HTTP server. Go go go. π
$httpd->run(sub { ... });
schemeβ Returns a scheme of running,"http"or"https".
my $scheme = $httpd->scheme;
hostβ Returns the address the server is listening on.portβ Returns the TCP port the server is listening on.
my $port = $httpd->port;
host_portβ Returns the host:port from"endpoint"(e.g.,"127.0.0.1:1234","[::1]:1234").
my $host_port = $httpd->host_port;
endpointβ Returns a URI object to the running server (e.g.,"http://127.0.0.1:1234","https://[::1]:1234"). And here's the lil quirk: if"host"returnsundef,'','0.0.0.0', or'::', then the host portion of the URI is set to"localhost". Periodt. β¨
use LWP::UserAgent;
my $res = LWP::UserAgent->new->get($httpd->endpoint);
my $url = $httpd->endpoint;
$url->path('/foo/bar');
my $res = LWP::UserAgent->new->get($url);
π AUTHOR π
NAKAGAWA Masaki <masaki@cpan.org> β the icon who wrote this. Respect. π«Ά
πΈ THANKS TO πΈ
xaicron β shoutout, no cap. π
π LICENSE π
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Free as in, go off, do your thing. π
π¦ SEE ALSO π¦
Test::TCPHTTP::DaemonHTTP::Daemon::SSLHTTP::Message::PSGI
And that's the whole page, bestie!! This was pulled from perl v5.30.3, dated 2020-08-18, and it lives under the Test::...::HTTPD(3pm) manual β go build something fake and beautiful. πβ¨ππ¦
π¦ where this page comes from
- debian bookworm: libtest-fake-httpd-perl 0.09-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libtest-fake-httpd-perl 0.09-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libtest-fake-httpd-perl 0.09-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libtest-fake-httpd-perl 0.09-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms