blib/lib/WWW/Mechanize/Cached.pm

filter: Statementstmt 80.6% 104 / 129 Branchbran 54.5% 36 / 66 Conditioncond 46.6% 7 / 15 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 88.2% 15 / 17 Podpod 50.0% 1 / 2 totaltotal 69.2% 164 / 237 SCAR 44.6
CC
41
Cov
70%
_response_cache_ok
62.1
_dwarn
20.0
__ANON__
6.0
CRAP 86.4
↑ Summary
1 package WWW::Mechanize::Cached;
2  
3 11 ▶use Moo 1.004005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 our $VERSION = '1.56';
7  
8 11 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Module::Runtime qw( use_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use MooX::Types::MooseLike::Base qw( AnyOf Bool Enum Maybe );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Storable qw( nfreeze thaw );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 11 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 extends 'WWW::Mechanize';
16  
17 11 has is_cached => ( is => 'rw', isa => Maybe [Bool], default => undef );
18 11 has positive_cache => ( is => 'rw', isa => Bool, default => 1 );
19 11 has ref_in_cache_key => ( is => 'rw', isa => Bool, default => 0 );
20 11 has _verbose_dwarn => ( is => 'rw', isa => Bool, default => 0 );
21 11 has _last_request => ( is => 'rw' );
22  
23 11 has [qw/ cache_undef_content_length cache_zero_content_length /] =>
24 ( is => 'rw', isa => AnyOf [ Bool, Enum ['warn'] ], default => 0 );
25  
26 11 has cache_mismatch_content_length => (
27 is => 'rw',
28 isa => AnyOf [ Bool, Enum ['warn'] ],
29 default => 'warn',
30 );
31  
32 11 has cache => ( is => 'lazy', isa => \&_isa_warn_cache );
33  
34 sub FOREIGNBUILDARGS {
35 21 ▶ my ( $class, %args ) = @_;
Subroutine FOREIGNBUILDARGS: called CC=2 SCAR=6.9
Pod undocumented
36  
37 # WWW::Mechanize/LWP::UserAgent would complain about these
38 21 for my $attribute (
39 qw(
40 is_cached
41 positive_cache
42 ref_in_cach_key
43 _verbose_dwarn
44 cache_undef_content_length
45 cache_zero_content_length
46 cache_mismatch_content_length
47 cache
48 )
49 ) {
50 168 delete $args{$attribute};
51 }
52  
53 21 return %args;
54 }
55  
56 sub _isa_warn_cache {
57 return
58 10 ▶ if 'HASH' ne ref $_[0]
Subroutine _isa_warn_cache: called CC=4 SCAR=14.1
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" ne ref $_[0] and $_[0]->can("get") and $_[0]->can("set") 9 1 10
Condition66%4/6
"HASH" ne ref $_[0] and $_[0]->can("get") and $_[0]->can("set")
!ll&&!rl&&r
109
"HASH" ne ref $_[0] and $_[0]->can("get")
!ll&&!rl&&r
109
MC/DC33%1/3
"HASH" ne ref $_[0] and $_[0]->can("get") and $_[0]->can("set")
"HASH" ne ref $_[0] $_[0]->can("get") $_[0]->can("set")
Truth table50%2/4
A and B and C
A= "HASH" ne ref $_[0]B= $_[0]->can("get")C= $_[0]->can("set")
ABCresult
0XX0
10X0
1100
1111
59 and $_[0]->can('get')
60 and $_[0]->can('set');
61 1 carp 'The cache param must be an initialized cache object';
62 1 $_[0] = undef;
63 }
64  
65 sub _build_cache {
66 2 ▶ my $self = shift;
Subroutine _build_cache: called CC=3 SCAR=16.0
67  
68 return Cache::FileCache->new(
69 {
70 default_expires_in => '1d',
71 namespace => 'www-mechanize-cached',
72 }
73 2 ▶ ) if eval { use_module('Cache::FileCache') };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { &use_module("Cache::FileCache") } } 2 0 2
74  
75 return CHI->new(
76 driver => 'File',
77 expires_in => '1d',
78 namespace => 'www-mechanize-cached',
79 0 ▶ ) if eval { use_module('CHI') };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { &use_module("CHI") } } 0 0 0
80  
81 0 croak( 'Could not create a default cache.'
82 . 'Please make sure either CHI or Cache::FileCache are installed or configure manually as appropriate'
83 );
84 }
85  
86 around _make_request => sub {
87 59 ▶ my ( $orig, $self, $request ) = splice @_, 0, 3;
Subroutine __ANON__: called CC=6 SCAR=17.9
88 59 my $req = $request;
89  
90 59 $self->is_cached(0);
91  
92 # An odd line to need.
93 # No idea what purpose this serves? OALDERS
94 59 ▶ $self->{proxy} = {} unless defined $self->{proxy}; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"proxy"} 0 59 59
95  
96 # RT #56757
97 59 ▶ if ( !$self->ref_in_cache_key ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->ref_in_cache_key) 59 0 59
98 59 my $clone = $request->clone;
99 59 $clone->header( Referer => undef );
100 59 $req = $clone->as_string;
101 }
102  
103 59 my $response = $self->cache->get($req);
104  
105 59 ▶ if ($response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) 32 27 59
106 32 $response = thaw($response);
107 }
108 59 ▶ if ( $self->_cache_ok($response) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_cache_ok($response)) 32 27 59
109 32 $self->is_cached(1);
110 32 $self->_last_request($req);
111 32 return $response;
112 }
113  
114 27 $response = $self->$orig( $request, @_ );
115  
116 # decode strips some important headers.
117 27 my $headers = $response->headers->clone;
118  
119 27 my $should_cache = $self->_response_cache_ok( $response, $headers );
120  
121 # http://rt.cpan.org/Public/Bug/Display.html?id=42693
122 27 $response->decode();
123 27 delete $response->{handlers}; ## no critic
124  
125 27 ▶ if ($should_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($should_cache) 18 9 27
126 18 $self->_last_request($req);
127 18 $self->cache->set( $req, nfreeze($response) );
128 }
129  
130 27 return $response;
131 11 };
132  
133 sub invalidate_last_request {
134 4 ▶ my $self = shift;
Subroutine invalidate_last_request: called CC=4 SCAR=14.0
Pod documented
135 4 ▶ return unless $self->is_cached;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_cached 2 2 4
136  
137 2 my $request = $self->_last_request;
138 2 ▶ return unless $request;
Branch: 1 branch
BranchTrueFalseTotal
unless $request 0 2 2
139  
140 2 $self->cache->remove($request);
141 2 ▶ return $self->is_cached( $self->cache->get($request) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$self->cache->get($request) ? : 0 2 2
142 }
143  
144 sub _dwarn_filter {
145 return {
146 0 ▶ hide_keys => [
Subroutine _dwarn_filter: not called CC=1 SCAR=6.9
147 qw( _content cookie content set-cookie handlers cookie_jar cache req res page_stack )
148 ]
149 };
150  
151 }
152  
153 sub _dwarn {
154 0 ▶ my $self = shift;
Subroutine _dwarn: not called CC=4 SCAR=30.0
155 0 my $message = shift;
156  
157 0 ▶ return unless my $handler = $self->{onwarn}; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
unless my $handler = $self->{"onwarn"} 0 0 0
158  
159 0 ▶ return if $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
if $self->quiet 0 0 0
160  
161 0 ▶ if ( $self->_verbose_dwarn ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_verbose_dwarn) { } 0 0 0
162 0 my $payload = {
163 self => $self,
164 message => $message,
165 debug => \@_,
166 };
167 0 require Data::Dump;
168 0 return $handler->( Data::Dump::dumpf( $payload, \&_dwarn_filter ) );
169 }
170 else {
171 0 return $handler->($message);
172 }
173 }
174  
175 sub _response_cache_ok {
176 30 ▶ my $self = shift;
Subroutine _response_cache_ok: called CC=20 SCAR=41.3
177 30 my $response = shift;
178 30 my $headers = shift;
179  
180 30 ▶ return 0 if !$response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 30 30
181 30 ▶ return 1 if !$self->positive_cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->positive_cache 0 30 30
182  
183 30 ▶ return 0 if $response->code < 200;
Branch: 1 branch
BranchTrueFalseTotal
if $response->code < 200 0 30 30
184 30 ▶ return 0 if $response->code > 301;
Branch: 1 branch
BranchTrueFalseTotal
if $response->code > 301 0 30 30
185  
186 30 my $size;
187 {
188 30 ▶ if ( $headers->header('Client-Transfer-Encoding') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers->header("Client-Transfer-Encoding")) 3 27 30
189 3 my @cte = $headers->header('Client-Transfer-Encoding');
190 3 for my $cte (@cte) {
191  
192 # Transfer-Encoding = chunked means document consistency
193 # is independent of Content-Length value,
194 # and that Content-Length can be safely ignored.
195 # Its not obvious how the lower levels represent a
196 # failed chunked-transfer yet.
197 # But its safe to say relying on content-length proves pointless.
198 3 ▶ return 1 if $cte eq 'chunked';
Branch: 1 branch
BranchTrueFalseTotal
if $cte eq "chunked" 2 1 3
199 }
200 }
201  
202 28 $size = $headers->{'content-length'};
203 }
204  
205 28 ▶ if ( not defined $size ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $size) 10 18 28
206 10 ▶ if ( $self->cache_undef_content_length . q{} eq q{warn} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache_undef_content_length . '' eq "warn") 0 10 10
207 0 $self->_dwarn(
208 q[Content-Length header was undefined, not caching]
209 . q[ (E=WWW_MECH_CACHED_CONTENTLENGTH_MISSING)],
210 $headers
211 );
212 0 return 0;
213 }
214 10 ▶ if ( $self->cache_undef_content_length == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache_undef_content_length == 0) 10 0 10
215 10 return 0;
216 }
217 }
218  
219 18 ▶ if ( defined $size and $size == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size and $size == 0) 0 18 18
Condition33%1/3
defined $size and $size == 0
!ll&&!rl&&r
0180
MC/DC0%0/2
defined $size and $size == 0
defined $size $size == 0
Truth table33%1/3
A and B
A= defined $sizeB= $size == 0
ABresult
0X0
100
111
220 0 ▶ if ( $self->cache_zero_content_length . q{} eq q{warn} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache_zero_content_length . '' eq "warn") 0 0 0
221 0 $self->_dwarn(
222 q{Content-Length header was 0, not caching}
223 . q{ (E=WWW_MECH_CACHED_CONTENTLENGTH_ZERO)},
224 $headers
225 );
226 0 return 0;
227 }
228 0 ▶ if ( $self->cache_zero_content_length == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache_zero_content_length == 0) 0 0 0
229 0 return 0;
230 }
231 }
232  
233 18 ▶ if ( defined $size
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size and $size != 0 and $size != length $response->content) 0 18 18
Condition33%2/6
defined $size and $size != 0 and $size != length $response->content
!ll&&!rl&&r
0180
defined $size and $size != 0
!ll&&!rl&&r
0018
MC/DC0%0/3
defined $size and $size != 0 and $size != length $response->content
defined $size $size != 0 $size != length $response->content
Truth table25%1/4
A and B and C
A= defined $sizeB= $size != 0C= $size != length $response->content
ABCresult
0XX0
10X0
1100
1111
234 and $size != 0
235 and $size != length( $response->content ) ) {
236 0 ▶ if ( $self->cache_mismatch_content_length . "" eq "warn" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache_mismatch_content_length . '' eq "warn") 0 0 0
237 0 $self->_dwarn(
238 q{Content-Length header did not match contents actual length, not caching}
239 . q{ (E=WWW_MECH_CACHED_CONTENTLENGTH_MISSMATCH)} );
240 0 return 0;
241 }
242 0 ▶ if ( $self->cache_mismatch_content_length == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache_mismatch_content_length == 0) 0 0 0
243 0 return 0;
244 }
245 }
246  
247 18 return 1;
248 }
249  
250 sub _cache_ok {
251  
252 63 ▶ my $self = shift;
Subroutine _cache_ok: called CC=5 SCAR=16.1
253 63 my $response = shift;
254  
255 63 ▶ return 0 if !$response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 27 36 63
256 36 ▶ return 1 if !$self->positive_cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->positive_cache 2 34 36
257  
258 34 ▶ return 0 if $response->code < 200;
Branch: 1 branch
BranchTrueFalseTotal
if $response->code < 200 0 34 34
259 34 ▶ return 0 if $response->code > 301;
Branch: 1 branch
BranchTrueFalseTotal
if $response->code > 301 1 33 34
260  
261 33 return 1;
262 }
263  
264 11 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
265 11 "We miss you, Spoon"; ## no critic
266  
267 # ABSTRACT: Cache response to be polite
268  
269 __END__
↑ Summary