File Coverage

blib/lib/Webservice/Shipment/MockUserAgent.pm
Criterion Covered Total %
statement 21 21 100.0
branch 2 2 100.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 30 30 100.0


line stmt bran cond sub pod time code
1             package Webservice::Shipment::MockUserAgent;
2              
3 3     3   1534 use Mojo::Base 'Mojo::UserAgent';
  3         8  
  3         18  
4              
5 3     3   2172 use Mojolicious;
  3         195467  
  3         33  
6 3     3   120 use Mojo::URL;
  3         8  
  3         18  
7              
8             has mock_blocking => 1;
9             has mock_response => sub { {} };
10              
11             sub new {
12 3     3 1 317 my $self = shift->SUPER::new(@_);
13              
14 3         42 my $app = Mojolicious->new;
15 3     8   68413 $app->routes->any('/*any' => {any => ''} => sub { shift->render(%{$self->mock_response}) });
  8         62879  
  8         31  
16 3         1581 $self->server->app($app);
17              
18             $self->on(start => sub {
19 8     8   3462 my ($self, $tx) = @_;
20 8         45 $self->emit(mock_request => $tx->req);
21 8 100       120 my $port = $self->mock_blocking ? $self->server->url->port : $self->server->nb_url->port;
22 8         11966 $tx->req->url->host('')->scheme('')->port($port);
23 3         198 });
24              
25 3         26 return $self;
26             }
27              
28             1;
29              
30             =head1 NAME
31              
32             Webservice::Shipment::MockUserAgent - A useragent which can generate mock service call reponses
33              
34             =head1 SYNOPSIS
35              
36             my $mock = Webservice::Shipment::MockUserAgent->new;
37             my $ship = Webservice::Shipment->new(defaults => {ua => $mock})->add_carrier(...);
38              
39             # test blocking responses
40             $mock->mock_response({text => $xml, format => 'xml'});
41             my $info = $ship->track($id); # receives $xml
42              
43             # test non-blocking responses
44             $mock->mock_blocking(0);
45             $ship->track($id => sub{ my ($carrier, $err, $info) = @_; ... }); # receives $xml
46              
47             # test the built request
48             $mock->on(mock_request => sub { ($mock, $req) = @_; ... });
49              
50             =head1 DESCRIPTION
51              
52             A subclass of L which can be used in place of the carrier's L and is capable of mocking service results.
53             For the time being there is no packaged mock data.
54             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.
55              
56             =head1 EVENTS
57              
58             L inherits all of the events from L and emits the following new ones
59              
60             =head2 mock_request
61              
62             $mock->on(mock_request => sub { my ($mock, $res) = @_; ... });
63              
64             Emitted when a request is emitted by the mock service.
65              
66             Note that this class makes use of the existing C event to rewrite the url.
67             This event is emitted during that process, before the url is rewritten so that users may test that request (if so desired).
68             Since the request url is mutated, if it is to be tested later, Cing the url is recommended.
69              
70             my $url;
71             $mock->on(mock_request => sub { $url = pop->url->clone });
72             is $url, $expected;
73              
74             =head1 ATTRIBUTES
75              
76             L inherits all of the attributes from L and implements the following new ones
77              
78             =head2 mock_blocking
79              
80             When true, the default, the mock service will expect a blocking request.
81             In order to request a mock result in a non-blocking manner, set to a false value.
82              
83             =head2 mock_response
84              
85             A hash reference, used as stash values to build a response via a very generic embedded Mojolicious app.
86             Most users will use C<< {text => $xml, format => 'xml'} >> in order to render xml (where C<$xml> contains an xml document).
87              
88