NAME

Mock::LWP::Request - Perl extension for mocking calls to LWP::UserAgent's request method.

SYNOPSIS

use Mock::LWP::Request;
use LWP::UserAgent;

my $mocked_lwp = Mock::LWP::Request->new();

foreach my $response_object ( @list_of_http_response_objects ) {
  $mocked_lwp->add_response( $response_object );
}

...

my $ua = LWP::UserAgent->new();

my $first_response = $ua->get('http://www.example.com/');

# $first_response contains the result of attempting to make a real
# connection to www.example.com

$mocked_lwp->enable();

my $second_response = $ua->get('http://www.example.com.ua/');

# $second_response contains the first HTTP::Response object from
# @list_of_http_response_objects above

$mocked_lwp->disable();

DESCRIPTION

This class provides a simple way to mock the request method from LWP::UserAgent by injecting HTTP Response objects which subsequent calls to request() will return in the order they are injected.

METHODS

new

Instantiate an object of the class and set configuration options.

default_response

Set or default HTTP::Response object to be returned if no responses are available and missing_response_action is set to default.

missing_response_action

What action to take when a call is made to request and mocking is enabled but there are no responses available. Must be either 'die' or 'default'.

If set to 'die' such a request call will die. If set to 'default' the default response will be returned.

response_list

An array reference containing a list of HTTP::Response objects which will be returned in order for each request().

debug

If true a warning will be issued for each call to LWP::UserAgent->request when mocking is enabled.

add_response

Adds a HTTP Response object to the list of responses.

default_response

Set a default HTTP Response for calls to LWP::UserAgent->request() to return when mocking is enabled if missing_response_action is not set to 'die'.

The response must be a valid HTTP Response object. If none is provided a default response with status code 412 (Precondition Failed) and no body is the default.

enable

Enables mocking of LWP::Request->request() calls.

disable

Disables mocking of LWP::Request->request() calls.

debug

Pass a true value to enable debug or 0 to disable.

SEE ALSO

LWP::UserAgent

AUTHOR

Jason Clifford, <jason@ukfsn.org>

COPYRIGHT AND LICENSE

Copyright (C) 2013 by Jason Clifford

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10 or, at your option, any later version of Perl 5 you may have available.