Mock::LWP::Request - Perl extension for mocking calls to LWP::UserAgent's request method.
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();
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.
Instantiate an object of the class and set configuration options.
Set or default HTTP::Response object to be returned if no responses are available and missing_response_action is set to default.
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.
An array reference containing a list of HTTP::Response objects which will be returned in order for each request().
If true a warning will be issued for each call to LWP::UserAgent->request when mocking is enabled.
Adds a HTTP Response object to the list of responses.
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.
Enables mocking of LWP::Request->request() calls.
Disables mocking of LWP::Request->request() calls.
Pass a true value to enable debug or 0 to disable.
LWP::UserAgent
Jason Clifford, <jason@ukfsn.org>
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.
To install Mock::LWP::Request, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mock::LWP::Request
CPAN shell
perl -MCPAN -e shell install Mock::LWP::Request
For more information on module installation, please visit the detailed CPAN module installation guide.