Karen Etheridge > Test-LWP-UserAgent > Test::LWP::UserAgent

Download:
Test-LWP-UserAgent-0.018.tar.gz

Dependencies

Annotate this POD

Website

View/Report Bugs
Module Version: 0.018   Source  

NAME ^

Test::LWP::UserAgent - a LWP::UserAgent suitable for simulating and testing network calls

VERSION ^

version 0.018

SYNOPSIS ^

In your application code:

    use URI;
    use HTTP::Request::Common;
    use LWP::UserAgent;

    my $ua = $self->useragent || LWP::UserAgent->new;

    my $uri = URI->new('http://example.com');
    $uri->port('3000');
    $uri->path('success');
    my $request = POST($uri, a => 1);
    my $response = $ua->request($request);

Then, in your tests:

    use Test::LWP::UserAgent;
    use Test::More;

    Test::LWP::UserAgent->map_response(
        qr{example.com/success}, HTTP::Response->new('200', 'OK', ['Content-Type' => 'text/plain'], ''));
    Test::LWP::UserAgent->map_response(
        qr{example.com/fail}, HTTP::Response->new('500', 'ERROR', ['Content-Type' => 'text/plain'], ''));
    Test::LWP::UserAgent->map_response(
        qr{example.com/conditional},
        sub {
            my $request = shift;
            my $success = $request->uri =~ /success/;
            return HTTP::Response->new(
                ($success ? ( '200', 'OK') : ('500', 'ERROR'),
                ['Content-Type' => 'text/plain'], '')
            )
        },
    );

OR, you can use a PSGI app to handle the requests:

    use HTTP::Message::PSGI;
    Test::LWP::UserAgent->register_psgi('example.com' => sub {
        my $env = shift;
        # logic here...
        [ '200', [ 'Content-Type' => 'text/plain' ], [ 'some body' ] ],
    );

And then:

    # <something which calls the code being tested...>

    my $last_request = Test::LWP::UserAgent->last_http_request_sent;
    is($last_request->uri, 'http://example.com/success:3000', 'URI');
    is($last_request->content, 'a=1', 'POST content');

    # <now test that your code responded to the 200 response properly...>

This feature is useful for testing your PSGI applications (you may or may not find using Plack::Test easier), or for simulating a server so as to test your client code.

OR, you can route some or all requests through the network as normal, but still gain the hooks provided by this class to test what was sent and received:

    my $useragent = Test::LWP::UserAgent->new(network_fallback => 1);

or:

    $useragent->map_network_response(qr/real.network.host/);

    # ... generate a request...

    # and then in your tests:
    is(
        $useragent->last_useragent->timeout,
        180,
        'timeout was overridden properly',
    );
    is(
        $useragent->last_http_request_sent->uri,
        'uri my code should have constructed',
    );
    is(
        $useragent->last_http_response_received->code,
        '200',
        'I should have gotten an OK response',
    );

Note that LWP::UserAgent itself is not monkey-patched - you must use this module (or a subclass) to send your request, or it cannot be caught and processed.

One common mechanism to swap out the useragent implementation is via a lazily-built Moose attribute; if no override is provided at construction time, default to LWP::UserAgent->new(%options).

METHODS ^

All other methods from LWP::UserAgent are available unchanged.

Usage with SOAP requests ^

MOTIVATION ^

Most mock libraries on the CPAN use Test::MockObject, which is widely considered not good practice (among other things, @ISA is violated, it requires knowing far too much about the module's internals, and is very clumsy to work with).

This module is a direct descendant of LWP::UserAgent, exports nothing into your namespace, and all access is via method calls, so it is fully inheritable should you desire to add more features or override some bits of functionality.

(Aside from the constructor), it only overrides the one method in LWP::UserAgent that issues calls to the network, so real HTTP::Request and HTTP::Headers objects are used throughout. It provides a method (last_http_request_sent) to access the last HTTP::Request, for testing things like the URI and headers that your code sent to LWP::UserAgent.

SUPPORT ^

Bugs may be submitted through the RT bug tracker (or bug-Test-LWP-UserAgent@rt.cpan.org). I am also usually active on irc, as 'ether' at irc.perl.org.

ACKNOWLEDGEMENTS ^

AirG Inc., my employer, and the first user of this distribution.

mst - Matt S. Trout <mst@shadowcat.co.uk>, for the better name of this distribution, and for the PSGI registration concept.

Also Yury Zavarin, whose Test::Mock::LWP::Dispatch inspired me to write this module, and from where I borrowed some aspects of the API.

SEE ALSO ^

entry for Perl Advent 2012

Test::Mock::LWP::Dispatch

Test::Mock::LWP::UserAgent

LWP::UserAgent

PSGI, HTTP::Message::PSGI, LWP::Protocol::PSGI

AUTHOR ^

Karen Etheridge <ether@cpan.org>

COPYRIGHT AND LICENSE ^

This software is copyright (c) 2013 by Karen Etheridge.

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

syntax highlighting: