Test::MockHTTP - utility to test LWP usage without actual HTTP requests


Net-FriendFeed documentation Contained in the Net-FriendFeed distribution.

Index


Code Index:

NAME

Top

Test::MockHTTP - utility to test LWP usage without actual HTTP requests

SYNOPSIS

Top

    use Test::MockHTTP;

    my $ua;
    http_test_setup { $ua = $_[0] };

    http_cmp( sub { $ua->do_some_http_request() },
        [
            as_string => re('image1_link=\S+mail\.rambler\.ru'),
            # these are pairs of method => test againt its return
            # value
        ]
    );
    ...

FUNCTIONS

Top

http_test_setup

This function takes one coderef argument which gets called with a specially crafted (with mocked methods) LWP::UserAgent instance before each test. All actions should use this UA to be tested.

http_cmp This is a wrapper around Test::Deep::cmp_deeply which does the actual tests.

It takes 4 arguments.

$code

This is the code to test. It gets called.

$methods

This is an arrayref of pairs 'method' => 'test against its return value'. It gets passed into Test::Deep::methods to construct a test against HTTP::Request which is provided by LWP.

$msg

TAP message, is directly passed to Test::Deep::cmp_deeply.

$resp

HTTP::Response instance which is returned as a fake response to HTTP::Request from simple_request() method. Defaults to simple '200 Ok' empty response.


Net-FriendFeed documentation Contained in the Net-FriendFeed distribution.
package Test::MockHTTP;

use warnings;
use strict;

use base qw/Exporter/;

our @EXPORT = qw/http_cmp http_test_setup/;

use Test::MockObject::Extends;
use Test::Deep;
use LWP::UserAgent;

my $Http_test_setup;
my $Mock_ua;

sub http_test_setup(&) {
    $Http_test_setup = shift;
}

sub http_cmp {
    my ($code, $methods, $msg, $resp) = @_;

    $Http_test_setup->($Mock_ua) if $Http_test_setup;

    $Mock_ua->mock(simple_request => sub {
        cmp_deeply($_[1], methods(@$methods), $msg);

        return $resp || HTTP::Response->new(200, 'Ok',
            [Server => 'mock'], '{"result": "ok"}');    # JSON
    });

    $code->();
}

$Mock_ua = Test::MockObject::Extends->new(LWP::UserAgent->new);