The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

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

SYNOPSIS

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

DESCRIPTION

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

FUNCTIONS

METHODS

AUTHOR

NAKAGAWA Masaki masaki@cpan.org

THANKS TO

xaicron

LICENSE

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

SEE ALSO

Test::TCP, HTTP::Daemon, HTTP::Daemon::SSL, HTTP::Message::PSGI