Test::Fake::HTTPD(3pm) User Contributed Perl Documentation Test::Fake::HTTPD(3pm)

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

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.

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

"new( %args )"

Returns a new instance.

  my $httpd = Test::Fake::HTTPD->new(%args);
    

%args are:

  • "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,
  );

NAKAGAWA Masaki <masaki@cpan.org>

xaicron

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

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

2020-08-18 perl v5.30.3