Plack::Test::Server - Run HTTP tests through live Plack servers


Plack documentation Contained in the Plack distribution.

Index


Code Index:

NAME

Top

Plack::Test::Server - Run HTTP tests through live Plack servers

DESCRIPTION

Top

Plack::Test::Server is a utility to run PSGI application with Plack server implementations, and run the live HTTP tests with the server using a callback. See Plack::Test how to use this module.

AUTHOR

Top

Tatsuhiko Miyagawa

Tokuhiro Matsuno

SEE ALSO

Top

Plack::Loader Test::TCP Plack::Test


Plack documentation Contained in the Plack distribution.

package Plack::Test::Server;
use strict;
use warnings;
use Carp;
use LWP::UserAgent;
use Test::TCP;
use Plack::Loader;

sub test_psgi {
    my %args = @_;

    my $client = delete $args{client} or croak "client test code needed";
    my $app    = delete $args{app}    or croak "app needed";
    my $ua     = delete $args{ua} || LWP::UserAgent->new;

    test_tcp(
        client => sub {
            my $port = shift;
            my $cb = sub {
                my $req = shift;
                $req->uri->scheme('http');
                $req->uri->host($args{host} || '127.0.0.1');
                $req->uri->port($port);
                return $ua->request($req);
            };
            $client->($cb);
        },
        server => $args{server} || sub {
            my $port = shift;
            my $server = Plack::Loader->auto(port => $port, host => ($args{host} || '127.0.0.1'));
            $server->run($app);
        },
    );
}

1;

__END__