Introducing Plack::Test::Agent

| No Comments

After a discussion with Zbigniew and Miyagawa on the subject of Plack::Test and Test:: Interfaces, I wrote Plack::Test::Agent as a proof of concept.

I really like the flexibility of Plack::Test, as testing an app in process or against a backend is trivial. I understand the callback interface—it definitely feels PSGIish, and it fits the interface that Test::TCP prefers. Yet there's always room to experiment.

Plack::Test::Agent offers the ability to run tests in process or over HTTP against a server. It relies on Test::TCP and Plack::Loader for the hard work, and borrows liberally from Plack::Test. Yet in doing so, it offers an OO interface:

my $agent = Plack::Test::Agent->new( app => $app );
my $res   = $agent->get( '/?have=foo;want=foo' );
ok $res->is_success, 'Request should succeed when values match';
is $res->decoded_content, 'ok', '... with descriptive success message';

Pass a server key/value pair, and it'll do its best to find and launch the appropriate server. All of your tests should continue to pass, modulo bugs in your assumptions or the handler/server interface.

Just for kicks, I added an experimental feature:

my $mech = Plack::Test::Agent->new( app    => $app,
                                    server => 'HTTP::Server::PSGI' )->get_mech;

$mech->get_ok( '/?have=foo;want=foo',
    'Request should succeed when values match' );
$mech->content_is( 'ok', '... with descriptive success message' );

... which returns a Test::WWW::Mechanize object bound to the started server such that relative URI requests go directly to the server. (Absolute URI requests remain unchanged.) The next obvious step is to return a Test::WWW::Mechanize::PSGI object for in process testing. That's the work of a few moments.

Perhaps that goes too far, but I do like how it's simplified my test code so far.

Leave a comment