Test-HTTP-LocalServer

 view release on metacpan or  search on metacpan

README.mkdn  view on Meta::CPAN

        request_pause => 1, # wait one second before accepting the next request
    );

    my $res = HTTP::Tiny->new->get( $server->url );
    print $res->{content};

    $server->stop;

# DESCRIPTION

This module implements a tiny web server suitable for running "live" tests
of HTTP clients against it. It also takes care of cleaning `%ENV` from settings
that influence the use of a local proxy etc.

Use this web server if you write an HTTP client and want to exercise its
behaviour in your test suite without talking to the outside world.

# METHODS

## `Test::HTTP::LocalServer->spawn %ARGS`

    my $server = Test::HTTP::LocalServer->spawn;

This spawns a new HTTP server. The server will stay running until

    $server->stop

is called. Ideally, you explicitly call `->stop` or use

    undef $server

before the main program ends so that the program exit code reflects the
real exit code and not the chlid exit code.

Valid arguments are :

- `html =>` scalar containing the page to be served

    If this is not specified, an informative default page will be used.

- `request_pause =>` number of seconds to sleep before accepting the next
request

    If your system is slow or needs to wait some time before a socket connection
    is ready again, use this parameter to make the server wait a bit before
    handling the next connection.

- `file =>` filename containing the page to be served
- `debug => 1` to make the spawned server output debug information
- `eval =>` string that will get evaluated per request in the server

    Try to avoid characters that are special to the shell, especially quotes.
    A good idea for a slow server would be

        eval => sleep+10

All served HTML will have the first %s replaced by the current location.

The following entries will be removed from `%ENV` when making a request:

    HTTP_PROXY
    http_proxy
    HTTP_PROXY_ALL
    http_proxy_all
    HTTPS_PROXY
    https_proxy
    CGI_HTTP_PROXY
    ALL_PROXY
    all_proxy

## `$server->port`

This returns the port of the current server. As new instances
will most likely run under a different port, this is convenient
if you need to compare results from two runs.

## `$server->url`

This returns the [URI](https://metacpan.org/pod/URI) where you can contact the server. This url
is valid until the `$server` goes out of scope or you call

    $server->stop;

The returned object is a copy that you can modify at your leisure.

## `$server->server_url`

This returns the [URI](https://metacpan.org/pod/URI) object of the server URL. Use ["$server->url"](#server-url) instead.
Use this object if you want to modify the hostname or other properties of the
server object.

Consider this basically an emergency accessor. In about every case,
using `->url()` does what you want.

## `$server->stop`

This stops the server process by requesting a special
url.

## `$server->kill`

This kills the server process via `kill`. The log
cannot be retrieved then.

## `$server->get_log`

This returns the
output of the server process. This output will be a list of
all requests made to the server concatenated together
as a string.

## `$server->local`

    my $url = $server->local('foo.html');
    # file:///.../foo.html

Returns an URL for a local file which will be read and served
by the webserver. The filename must
be a relative filename relative to the location of the current
program.

# URLs implemented by the server

## arbitrary content `$server->content($html)`

    $server->content(<<'HTML');
        <script>alert("Hello World");</script>



( run in 0.982 second using v1.01-cache-2.11-cpan-71847e10f99 )