blib/lib/Test/LWP/UserAgent.pm

filter: Statementstmt 90.9% 161 / 177 Branchbran 77.6% 73 / 94 Conditioncond 68.9% 20 / 29 MC/DCmcdc 47.0% 8 / 17 Subroutinesub 89.4% 34 / 38 Podpod 100% 11 / 11 totaltotal 83.8% 307 / 366 CC 60 SCAR 20.9
CC
60
Cov
85%
send_request
21.2
register_psgi
12.0
unregister_psgi
12.0
CRAP 8.1
↑ Summary
1 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Test::LWP::UserAgent; # git description: v0.035-2-g1324b10
4 # vim: set ts=8 sts=2 sw=2 tw=115 et :
5 # ABSTRACT: A LWP::UserAgent suitable for simulating and testing network calls
6 # KEYWORDS: testing useragent networking mock server client
7  
8 10 our $VERSION = '0.036';
9  
10 10 ▶use parent 'LWP::UserAgent';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Storable 'freeze';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use HTTP::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 ▶use HTTP::Status qw(:constants status_message);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 ▶use namespace::clean 0.19 -also => [qw(__isa_coderef __is_regexp __isa_response)];
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 10 my @response_map;
24 my $network_fallback;
25 10 my $last_useragent;
26  
27 sub new
28 23 ▶{
Subroutine new: called CC=3 SCAR=11.3
Pod documented
29 23 my ($class, %options) = @_;
30  
31 23 my $_network_fallback = delete $options{network_fallback};
32  
33 23 my $self = $class->SUPER::new(%options);
34 23 $self->{__last_http_request_sent} = undef;
35 23 $self->{__last_http_response_received} = undef;
36 23 $self->{__response_map} = [];
37 23 $self->{__network_fallback} = $_network_fallback;
38  
39 # strips default User-Agent header added by LWP::UserAgent, to make it
40 # easier to define literal HTTP::Requests to match against
41 23 ▶ $self->agent(undef) if defined $self->agent and $self->agent eq $self->_agent;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->agent and $self->agent eq $self->_agent 23 0 23
Condition33%1/3
defined $self->agent and $self->agent eq $self->_agent
!ll&&!rl&&r
0023
MC/DC0%0/2
defined $self->agent and $self->agent eq $self->_agent
defined $self->agent $self->agent eq $self->_agent
Truth table33%1/3
A and B
A= defined $self->agentB= $self->agent eq $self->_agent
ABresult
0X0
100
111
42  
43 23 return $self;
44 }
45  
46 sub map_response
47 30 ▶{
Subroutine map_response: called CC=9 SCAR=22.0
Pod documented
48 30 my ($self, $request_specification, $response) = @_;
49  
50 30 ▶ if (not defined $response and blessed $self)
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $response and builtin::blessed($self)) 4 26 30
Condition66%2/3
not defined $response and builtin::blessed($self)
!ll&&!rl&&r
2604
MC/DC50%1/2
not defined $response and builtin::blessed($self)
not defined $response builtin::blessed($self)
Truth table66%2/3
not(A) and B
A= not defined $responseB= builtin::blessed($self)
ABresult
0X0
100
111
51 {
52 # mask a global domain mapping
53 4 my $matched;
54 4 foreach my $mapping (@{$self->{__response_map}})
55 {
56 6 ▶ if ($mapping->[0] eq $request_specification)
Branch: 1 branch
BranchTrueFalseTotal
if ($mapping->[0] eq $request_specification) 2 4 6
57 {
58 2 $matched = 1;
59 2 undef $mapping->[1];
60 }
61 }
62  
63 4 ▶ push @{$self->{__response_map}}, [ $request_specification, undef ]
Branch: 1 branch
BranchTrueFalseTotal
unless $matched 2 2 4
64 if not $matched;
65  
66 4 return;
67 }
68  
69 26 my ($isa_response, $error_message) = __isa_response($response);
70 26 ▶ if (not $isa_response)
Branch: 1 branch
BranchTrueFalseTotal
unless ($isa_response) 3 23 26
71 {
72 3 ▶ if (try { $response->can('request') })
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&try(sub { $response->can('request'); } )) { } 2 1 3
73 {
74 2 my $oldres = $response;
75 2 ▶ $response = sub { $oldres->request($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
76 }
77 else
78 {
79 1 carp 'map_response: ', $error_message;
80 }
81 }
82  
83 26 ▶ if (blessed $self)
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) { } 18 8 26
84 {
85 18 push @{$self->{__response_map}}, [ $request_specification, $response ];
86 }
87 else
88 {
89 8 push @response_map, [ $request_specification, $response ];
90 }
91 26 return $self;
92 }
93  
94 sub map_network_response
95 2 ▶{
Subroutine map_network_response: called CC=2 SCAR=6.9
Pod documented
96 2 my ($self, $request_specification) = @_;
97  
98 push (
99 2 ▶ @{ blessed($self) ? $self->{__response_map} : \@response_map },
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($self) ? : 1 1 2
100 [ $request_specification, $self->_response_send_request ],
101 );
102  
103 2 return $self;
104 }
105  
106 sub unmap_all
107 3 ▶{
Subroutine unmap_all: called CC=4 SCAR=13.9
Pod documented
108 3 my ($self, $instance_only) = @_;
109  
110 3 ▶ if (blessed $self)
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) { } 2 1 3
111 {
112 2 $self->{__response_map} = [];
113 2 ▶ @response_map = () unless $instance_only;
Branch: 1 branch
BranchTrueFalseTotal
unless $instance_only 1 1 2
114 }
115 else
116 {
117 1 ▶ carp 'instance-only unmap requests make no sense when called globally'
Branch: 1 branch
BranchTrueFalseTotal
if $instance_only 0 1 1
118 if $instance_only;
119 1 @response_map = ();
120 }
121 3 return $self;
122 }
123  
124 sub register_psgi
125 0 ▶{
Subroutine register_psgi: not called CC=3 SCAR=24.8
Pod documented
126 0 my ($self, $domain, $app) = @_;
127  
128 0 ▶ return $self->map_response($domain, undef) if not defined $app;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $app 0 0 0
129  
130 0 ▶ carp 'register_psgi: app is not a coderef, it\'s a ', ref($app)
Branch: 1 branch
BranchTrueFalseTotal
unless __isa_coderef($app) 0 0 0
131 unless __isa_coderef($app);
132  
133 0 return $self->map_response($domain, $self->_psgi_to_response($app));
134 }
135  
136 sub unregister_psgi
137 0 ▶{
Subroutine unregister_psgi: not called CC=3 SCAR=24.8
Pod documented
138 0 my ($self, $domain, $instance_only) = @_;
139  
140 0 ▶ if (blessed $self)
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) { } 0 0 0
141 {
142 0 @{$self->{__response_map}} = grep $_->[0] ne $domain, @{$self->{__response_map}};
143  
144 0 ▶ @response_map = grep $_->[0] ne $domain, @response_map
Branch: 1 branch
BranchTrueFalseTotal
unless $instance_only 0 0 0
145 unless $instance_only;
146 }
147 else
148 {
149 0 @response_map = grep $_->[0] ne $domain, @response_map;
150 }
151 0 return $self;
152 }
153  
154 sub last_http_request_sent
155 29 ▶{
Subroutine last_http_request_sent: called CC=3 SCAR=11.1
Pod documented
156 29 my $self = shift;
157 return blessed($self)
158 ? $self->{__last_http_request_sent}
159 29 ▶ : $last_useragent
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($self) ? : 28 1 29
$last_useragent ? : 0 1 1
160 ? $last_useragent->last_http_request_sent
161 : undef;
162 }
163  
164 sub last_http_response_received
165 16 ▶{
Subroutine last_http_response_received: called CC=3 SCAR=11.1
Pod documented
166 16 my $self = shift;
167 return blessed($self)
168 ? $self->{__last_http_response_received}
169 16 ▶ : $last_useragent
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($self) ? : 15 1 16
$last_useragent ? : 0 1 1
170 ? $last_useragent->last_http_response_received
171 : undef;
172 }
173  
174 sub last_useragent
175 23 ▶{
Subroutine last_useragent: called CC=1 SCAR=0.0
Pod documented
176 23 return $last_useragent;
177 }
178  
179 sub network_fallback
180 20 ▶{
Subroutine network_fallback: called CC=4 SCAR=13.9
Pod documented
181 20 my ($self, $value) = @_;
182  
183 20 ▶ if (@_ == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 15 5 20
184 {
185 return blessed $self
186 ? $self->{__network_fallback}
187 15 ▶ : $network_fallback;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($self) ? : 12 3 15
188 }
189  
190 5 ▶ return $self->{__network_fallback} = $value if blessed $self;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($self) 3 2 5
191 2 $network_fallback = $value;
192 }
193  
194 sub send_request
195 41 ▶{
Subroutine send_request: called CC=21 SCAR=30.5
Pod documented
196 41 my ($self, $request, $arg, $size) = @_;
197  
198 41 $self->progress('begin', $request);
199 41 my $matched_response = $self->run_handlers('request_send', $request);
200  
201 41 my $uri = $request->uri;
202  
203 41 foreach my $entry (@{$self->{__response_map}}, @response_map)
204 {
205 57 ▶ last if $matched_response;
Branch: 1 branch
BranchTrueFalseTotal
if $matched_response 0 57 57
206 57 ▶ next if not defined $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry 0 57 57
207 57 my ($request_desc, $response) = @$entry;
208  
209 57 ▶ if ($request_desc->$_isa('HTTP::Request'))
Branch: 3 branches
BranchTrueFalseTotal
if ($request_desc->$Test::LWP::UserAgent::_isa('HTTP::Request')) { } 3 54 57
elsif (__is_regexp($request_desc)) { } 25 29 54
elsif (__isa_coderef($request_desc)) { } 5 24 29
210 {
211 3 local $Storable::canonical = 1;
212 3 ▶ $matched_response = $response, last
Branch: 1 branch
BranchTrueFalseTotal
if freeze($request) eq freeze($request_desc) 1 2 3
213 if freeze($request) eq freeze($request_desc);
214 }
215 elsif (__is_regexp($request_desc))
216 {
217 25 ▶ $matched_response = $response, last
Branch: 1 branch
BranchTrueFalseTotal
if $uri =~ /$request_desc/ 13 12 25
218 if $uri =~ $request_desc;
219 }
220 elsif (__isa_coderef($request_desc))
221 {
222 5 ▶ $matched_response = $response, last
Branch: 1 branch
BranchTrueFalseTotal
if &$request_desc($request) 2 3 5
223 if $request_desc->($request);
224 }
225 else
226 {
227 24 ▶ $uri = URI->new($uri) if not $uri->$_isa('URI');
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->$Test::LWP::UserAgent::_isa("URI") 0 24 24
228 24 ▶ $matched_response = $response, last
Branch: 1 branch
BranchTrueFalseTotal
if $uri->host eq $request_desc 10 14 24
229 if $uri->host eq $request_desc;
230 }
231 }
232  
233 41 $last_useragent = $self;
234 41 $self->{__last_http_request_sent} = $request;
235  
236 41 ▶ if (not defined $matched_response and
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $matched_response and $self->{"__network_fallback"} || $network_fallback) 7 34 41
Condition100%6/6
not defined $matched_response and $self->{"__network_fallback"} || $network_fallback
!ll&&!rl&&r
22127
$self->{"__network_fallback"} or $network_fallback
l!l&&r!l&&!r
5212
MC/DC100%3/3
not defined $matched_response and $self->{"__network_fallback"} || $network_fallback
not defined $matched_response $self->{"__network_fallback"} $network_fallback
Truth table100%4/4
not(A) and B or C
A= not defined $matched_responseB= $self->{"__network_fallback"}C= $network_fallback
ABCresult
0XX0
1000
11X1
1011
237 ($self->{__network_fallback} or $network_fallback))
238 {
239 7 my $response = $self->SUPER::send_request($request, $arg, $size);
240 7 $self->{__last_http_response_received} = $response;
241 7 return $response;
242 }
243  
244 34 ▶ my $response = defined $matched_response
Branch: 1 branch
BranchTrueFalseTotal
defined $matched_response ? : 22 12 34
245 ? $matched_response
246 : HTTP::Response->new('404');
247  
248 34 ▶ if (__isa_coderef($response))
Branch: 1 branch
BranchTrueFalseTotal
if (__isa_coderef($response)) 11 23 34
249 {
250 # emulates handling in LWP::UserAgent::send_request
251 11 ▶ if ($self->use_eval)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_eval) { } 10 1 11
252 {
253 10 ▶ $response = try { $response->($request) }
Subroutine __ANON__: called CC=1 SCAR=0.0
254 2 ▶ catch {
Subroutine __ANON__: called CC=3 SCAR=11.0
255 2 my $exception = $_;
256 2 ▶ if ($exception->$_isa('HTTP::Response'))
Branch: 1 branch
BranchTrueFalseTotal
if ($exception->$Test::LWP::UserAgent::_isa('HTTP::Response')) { } 1 1 2
257 {
258 1 $response = $exception;
259 }
260 else
261 {
262 1 my $full = $exception;
263 1 (my $status = $exception) =~ s/\n.*//s;
264 1 $status =~ s/ at .* line \d+.*//s; # remove file/line number
265 1 ▶ my $code = ($status =~ s/^(\d\d\d)\s+//) ? $1 : HTTP_INTERNAL_SERVER_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
$status =~ s/^(\d\d\d)\s+// ? : 0 1 1
266 # note that _new_response did not always take a fourth
267 # parameter - content used to always be "$code $message"
268 1 $response = LWP::UserAgent::_new_response($request, $code, $status, $full);
269 }
270 }
271 10 }
272 else
273 {
274 1 $response = $response->($request);
275 }
276 }
277  
278 33 ▶ if (not $response->$_isa('HTTP::Response'))
Branch: 1 branch
BranchTrueFalseTotal
if (not $response->$Test::LWP::UserAgent::_isa('HTTP::Response')) { } 2 31 33
279 {
280 2 ▶ carp 'response from coderef is not a HTTP::Response, it\'s a ',
Branch: 1 branch
BranchTrueFalseTotal
ref $response ? : 0 2 2
Condition33%1/3
builtin::blessed($response) || (ref $response ? 'unblessed ' . ref($response) : 'non-reference')
l!l&&r!l&&!r
020
MC/DC0%0/2
builtin::blessed($response) || (ref $response ? 'unblessed ' . ref($response) : 'non-reference')
builtin::blessed($response) (ref $response ? 'unblessed ' . ref($response) : 'non-reference')
Truth table33%1/3
A || B
A= builtin::blessed($response)B= (ref $response ? 'unblessed ' . ref($response) : 'non-reference')
ABresult
1X1
011
000
281 (blessed($response) || ( ref($response) ? ('unblessed ' . ref($response)) : 'non-reference' ));
282  
283 2 $response = LWP::UserAgent::_new_response($request, HTTP_INTERNAL_SERVER_ERROR, status_message(HTTP_INTERNAL_SERVER_ERROR));
284 }
285 else
286 {
287 31 $response->request($request); # record request for reference
288 31 $response->header('Client-Date' => HTTP::Date::time2str(time));
289 }
290  
291 # handle any additional arguments that were provided, such as saving the
292 # content to a file. this also runs additional handlers for us.
293 33 my $protocol = LWP::Protocol->new('no-schemes-from-TLWPUA', $self);
294 33 my $complete;
295 43 ▶ $response = $protocol->collect($arg, $response, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
296 # remove content from $response and stream it back
297 43 ▶ return \'' if $complete;
Branch: 1 branch
BranchTrueFalseTotal
if $complete 10 33 43
298 33 my $content = $response->content;
299 33 $response->content('');
300 33 $complete++;
301 33 \$content;
302 33 });
303  
304 33 $self->run_handlers('response_done', $response);
305 33 $self->progress('end', $response);
306  
307 33 $self->{__last_http_response_received} = $response;
308  
309 33 return $response;
310 }
311  
312 # turns a PSGI app into a subref returning an HTTP::Response
313 sub _psgi_to_response
314 0 ▶{
Subroutine _psgi_to_response: not called CC=3 SCAR=24.8
315 0 my ($self, $app) = @_;
316  
317 0 ▶ carp 'register_psgi: did you forget to load HTTP::Message::PSGI?'
Branch: 1 branch
BranchTrueFalseTotal
unless "HTTP::Request"->can("to_psgi") and "HTTP::Response"->can("from_psgi") 0 0 0
Condition0%0/3
"HTTP::Request"->can("to_psgi") and "HTTP::Response"->can("from_psgi")
!ll&&!rl&&r
000
MC/DC0%0/2
"HTTP::Request"->can("to_psgi") and "HTTP::Response"->can("from_psgi")
"HTTP::Request"->can("to_psgi") "HTTP::Response"->can("from_psgi")
Truth table0%0/3
A and B
A= "HTTP::Request"->can("to_psgi")B= "HTTP::Response"->can("from_psgi")
ABresult
0X0
100
111
318 unless HTTP::Request->can('to_psgi') and HTTP::Response->can('from_psgi');
319  
320 0 ▶ return sub { HTTP::Response->from_psgi($app->($_[0]->to_psgi)) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
321 }
322  
323 # returns a subref that returns an HTTP::Response from a real network request
324 sub _response_send_request
325 2 ▶{
Subroutine _response_send_request: called CC=2 SCAR=6.9
326 2 my $self = shift;
327  
328 # we cannot call ::request here, or we end up in an infinite loop
329 2 ▶ return sub { $self->SUPER::send_request($_[0]) } if blessed $self;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($self) 1 1 2
330 1 ▶ return sub { LWP::UserAgent->new->send_request($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
331 }
332  
333 sub __isa_coderef
334 95 ▶{
Subroutine __isa_coderef: called CC=4 SCAR=13.9
335 95 ▶ ref $_[0] eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "CODE" or (builtin::reftype($_[0]) || "") eq "CODE" or overload::Method($_[0], "&{}") 21 74 95
Condition100%5/5
ref $_[0] eq "CODE" or (builtin::reftype($_[0]) || "") eq "CODE"
l!l&&r!l&&!r
20174
builtin::reftype($_[0]) || ''
l!l
5124
MC/DC100%2/2
ref $_[0] eq "CODE" or (builtin::reftype($_[0]) || "") eq "CODE"
ref $_[0] eq "CODE" builtin::reftype($_[0])
Truth table100%3/3
A or B
A= ref $_[0] eq "CODE"B= builtin::reftype($_[0])
ABresult
1X1
011
000
336 or (reftype($_[0]) || '') eq 'CODE'
337 or overload::Method($_[0], '&{}')
338 }
339  
340 sub __is_regexp
341 54 ▶{
Subroutine __is_regexp: called CC=2 SCAR=7.6
342 54 ▶ re->can('is_regexp') ? re::is_regexp($_[0]) : ref($_[0]) eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
're'->can('is_regexp') ? : 54 0 54
343 }
344  
345 # returns true if is expected type for all response mappings,
346 # or (false, error message);
347 sub __isa_response
348 26 ▶{
Subroutine __isa_response: called CC=5 SCAR=16.4
349 26 ▶ __isa_coderef($_[0]) || $_[0]->$_isa('HTTP::Response')
Branch: 2 branches
BranchTrueFalseTotal
__isa_coderef($_[0]) || $_[0]->$Test::LWP::UserAgent::_isa('HTTP::Response') ? : 23 3 26
ref $_[0] ? : 0 2 2
Condition83%5/6
__isa_coderef($_[0]) or $_[0]->$Test::LWP::UserAgent::_isa("HTTP::Response")
l!l&&r!l&&!r
6173
builtin::blessed($_[0]) || (ref $_[0] ? 'unblessed ' . ref($_[0]) : 'non-reference')
l!l&&r!l&&!r
120
MC/DC100%2/2
__isa_coderef($_[0]) or $_[0]->$Test::LWP::UserAgent::_isa("HTTP::Response")
__isa_coderef($_[0]) $_[0]->$Test::LWP::UserAgent::_isa("HTTP::Response")
MC/DC0%0/2
builtin::blessed($_[0]) || (ref $_[0] ? 'unblessed ' . ref($_[0]) : 'non-reference')
builtin::blessed($_[0]) (ref $_[0] ? 'unblessed ' . ref($_[0]) : 'non-reference')
Truth table100%3/3
A or B
A= __isa_coderef($_[0])B= $_[0]->$Test::LWP::UserAgent::_isa("HTTP::Response")
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= builtin::blessed($_[0])B= (ref $_[0] ? 'unblessed ' . ref($_[0]) : 'non-reference')
ABresult
1X1
011
000
350 ? (1)
351 : (0, 'response is not a coderef or an HTTP::Response, it\'s a '
352 . (blessed($_[0]) || ( ref($_[0]) ? 'unblessed ' . ref($_[0]) : 'non-reference' )));
353 }
354  
355 10 1;
356  
357 __END__
↑ Summary