Joel Berger


Webservice::Shipment::MockUserAgent - A useragent which can generate mock service call reponses


  my $mock = Webservice::Shipment::MockUserAgent->new;
  my $ship = Webservice::Shipment->new(defaults => {ua => $mock})->add_carrier(...);

  # test blocking responses
  $mock->mock_response({text => $xml, format => 'xml'});
  my $info = $ship->track($id); # receives $xml

  # test non-blocking responses
  $ship->track($id => sub{ my ($carrier, $err, $info) = @_; ... }); # receives $xml

  # test the built request
  $mock->on(mock_request => sub { ($mock, $req) = @_; ... });


A subclass of Mojo::UserAgent which can be used in place of the carrier's "ua" in Webservice::Shipment::Carrier and is capable of mocking service results. For the time being there is no packaged mock data. The author recomends extracting a response from a valid request and injecting it into the mock, thereby pinning the result and no longer relying on the external service.


Webservice::Shipment::MockUserAgent inherits all of the events from Mojo::UserAgent and emits the following new ones


  $mock->on(mock_request => sub { my ($mock, $res) = @_; ... });

Emitted when a request is emitted by the mock service.

Note that this class makes use of the existing start event to rewrite the url. This event is emitted during that process, before the url is rewritten so that users may test that request (if so desired). Since the request url is mutated, if it is to be tested later, cloneing the url is recommended.

  my $url;
  $mock->on(mock_request => sub { $url = pop->url->clone });
  is $url, $expected;


Webservice::Shipment::MockUserAgent inherits all of the attributes from Mojo::UserAgent and implements the following new ones


When true, the default, the mock service will expect a blocking request. In order to request a mock result in a non-blocking manner, set to a false value.


A hash reference, used as stash values to build a response via a very generic embedded Mojolicious app. Most users will use {text => $xml, format => 'xml'} in order to render xml (where $xml contains an xml document).