View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Yury Zavarin > Test-Mock-LWP-Dispatch-0.05 > Test::Mock::LWP::Dispatch



Annotate this POD



Open  0
View/Report Bugs
Module Version: 0.05   Source   Latest Release: Test-Mock-LWP-Dispatch-0.08


Test::Mock::LWP::Dispatch - mocks LWP::UserAgent and dispatches your requests/responses


version 0.05


  # in your *.t
  use Test::Mock::LWP::Dispatch;
  use HTTP::Response;

  # global mappings for requests and responses for LWP::UserAgent
  $mock_ua->map('', HTTP::Response->new(...));
  # or
  $mock_ua->map(qr!^!, sub { my $request = shift;
                                                    # ... create $response
                                                    return $response; });

  # or make local mappings
  my $ua = LWP::UserAgent->new;


This module intends for testing a code that heavily uses LWP::UserAgent.

Assume that function you want to test makes three different request to the server and expects to get some content from the server. To test this function you should setup request/response mappings for mocked UserAgent and test it.

For doing something with mappings, here are methods map, unmap and unmap_all. For controlling context of these mappings (is it applies for all created in your code LWP::UserAgent's or only to one specific?) you should call these functions for exported $mock_ua object (global mapping) or for newly created LWP::UserAgent (local mappings).

See also on Test::Mock::LWP, it provides mocked LWP objects for you, so probably you can solve your problems with this module too.



This is only method of LWP::UserAgent that mocked. When you make $ua->get(...) or $ua->head(...) or just get() from LWP::Simple, at some point calls simple_request() method. So for controlling responses to your requests it is only method needed to mock.

In this module simple_request() loops through your local and global mappings (in this order) and returns response on a first matched mapping. If no matched simple_request() returns HTTP::Response with 404 code.

Be accurate: method loops through mappings in order of adding these mappings.

map($req_descr, $resp_descr)

Using this method you can say what response should be on what request.

If you call this method for exported $mock_ua it will make global mappings applied for all newly created LWP::UserAgent's. If you call this method for separate LWP::UserAgent you created, then this mapping will work only for this object.

Request description $req_descr can be:


Uri for exact matching with incoming request uri in request().


Regexp on what incoming request uri will match.


You can pass arbitrary coderef, that takes incoming HTTP::Request and returns true if this request matched.

HTTP::Request object

If you pass such object, then request will compare that incoming request exactly the same that you passed in map() (this means, that all query parameters, all headers and so on must be identical).

Response description $resp_descr, that will be returned if incoming request to request() matches with $req_descr, can be:

HTTP::Response object

Ready to return HTTP::Response object.


Arbitrary coderef, that takes incoming request as parameter and returns HTTP::Response object.

Method returns index of your mapping. You can use it in unmap.


Deletes some mapping by index.


Deletes all mappings.


This mock object doesn't call fake_new(). So when you prepare response using coderef, you can be sure, that "User-Agent" header will be untouched and so on.


Mike Doherty

SEE ALSO ^ Test::Mock::LWP LWP::UserAgent


Yury Zavarin <>


This software is copyright (c) 2013 by Yury Zavarin.

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: