blib/lib/LWP/UserAgent/Mockable.pm

filter: Statementstmt 95.1% 99 / 104 Branchbran 83.3% 30 / 36 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 18 / 18 Podpod 100% 6 / 6 totaltotal 93.6% 161 / 172 CC 21 SCAR 12.1
CC
21
Cov
92%
reset
6.1
__ANON__
5.2
finished
4.0
CRAP 3.4
↑ Summary
1 package LWP::UserAgent::Mockable;
2  
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use Hook::LexWrap;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Safe::Isa '$_isa';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Storable qw( dclone nstore retrieve );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 our $VERSION = '1.18';
12  
13 10 my $instance = __PACKAGE__->__instance;
14 12 ▶sub __instance {
Subroutine __instance: called CC=3 SCAR=11.0
15 12 my ( $class ) = @_;
16  
17 12 ▶ if ( not defined $instance ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $instance) 12 0 12
18 12 $instance = bless {
19 action => undef,
20 file => undef,
21 current_request => undef,
22 actions => [],
23 callbacks => {},
24 wrappers => {},
25 }, $class;
26  
27 my $action = defined $ENV{ LWP_UA_MOCK }
28 ? lc $ENV{ LWP_UA_MOCK }
29 12 ▶ : 'passthrough';
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'LWP_UA_MOCK'} ? : 9 3 12
30  
31 12 $instance->reset( $action, $ENV{ LWP_UA_MOCK_FILE } );
32 }
33  
34 10 return $instance;
35 }
36  
37 23 ▶sub reset {
Subroutine reset: called CC=6 SCAR=18.1
Pod documented
38 23 my ( $class, $action, $file ) = @_;
39  
40 23 ▶ if ( scalar @{ $instance->{ actions } } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$instance->{"actions"};}) 0 23 23
41 0 die "Can't reset state whilst pending actions. Need to call finish first";
42 }
43  
44 23 ▶ if ( not defined $action ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $action) 9 14 23
45 9 $action = "passthrough";
46 }
47  
48 23 ▶ if ( $action !~ /^(playback|record|passthrough)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($action =~ /^(playback|record|passthrough)/) 0 23 23
49 0 die "Action must be one of 'passthrough', 'playback' or 'record'";
50 }
51  
52 23 ▶ if ( $action ne 'passthrough' and not defined $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action ne "passthrough" and not defined $file) 1 22 23
Condition100%3/3
$action ne "passthrough" and not defined $file
!ll&&!rl&&r
1481
MC/DC100%2/2
$action ne "passthrough" and not defined $file
$action ne "passthrough" not defined $file
Truth table100%3/3
A and not(B)
A= $action ne "passthrough"B= not defined $file
ABresult
0X0
100
111
53 1 die "No file defined. Should point to file you wish to record to or playback from";
54 }
55  
56 22 $instance->{ wrappers } = {};
57 22 $instance->{ action } = $action;
58 22 $instance->{ file } = $file;
59 22 $instance->{ callbacks } = {};
60  
61 22 $instance->__reset;
62 }
63  
64 22 ▶sub __reset {
Subroutine __reset: called CC=2 SCAR=6.9
65 22 my ( $self ) = @_;
66  
67 my ( $action, $file, $callbacks, $wrappers )
68 22 = @{ $self }{ qw( action file callbacks wrappers ) };
69  
70 22 ▶ if ( $action eq 'playback' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq 'playback') { } 5 17 22
71 5 local $Storable::Eval = 1;
72  
73 5 $self->{ actions } = retrieve( $file );
74  
75 $wrappers->{ pre } = wrap 'LWP::UserAgent::simple_request',
76 17 ▶ pre => sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
77 17 my ( $wrapped, $request ) = @_;
78  
79 17 my $current = shift @{ $self->{ actions } };
80 17 ▶ if ( not defined $current ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $current) 0 17 17
81 0 die "No further HTTP requests exist. You possibly need to re-record the LWP session";
82 }
83  
84 17 my $response = $current->{ response };
85  
86 17 ▶ if ( $callbacks->{ playback_validation } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callbacks->{"playback_validation"}) 3 14 17
87 3 my $mock_request = $current->{ request };
88  
89 3 $callbacks->{ playback_validation }( $request, $mock_request );
90 }
91  
92 17 ▶ if ( $callbacks->{ playback }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callbacks->{"playback"}) 1 16 17
93 1 $response = $callbacks->{ playback }( $request, $response );
94  
95 1 ▶ if ( not $response->$_isa( 'HTTP::Response' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->$LWP::UserAgent::Mockable::_isa("HTTP::Response")) 0 1 1
96 0 die "playback callback didn't return an HTTP::Response object";
97 }
98 }
99  
100 17 $_[ -1 ] = $response;
101 4 };
102 } else {
103 $wrappers->{ pre } = wrap 'LWP::UserAgent::simple_request',
104 25 ▶ pre => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
105 25 my ( $wrapped, $request ) = @_;
106  
107 25 $self->{ current_request } = $request;
108  
109 25 ▶ if ( $callbacks->{ pre_record } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callbacks->{"pre_record"}) 9 16 25
110 9 $_[ -1 ] = $callbacks->{ pre_record }( $request );
111  
112 9 ▶ if ( not $_[ -1 ]->$_isa( 'HTTP::Response' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[-1]->$LWP::UserAgent::Mockable::_isa("HTTP::Response")) 3 6 9
113 3 die "pre-record callback didn't return an HTTP::Response object";
114 }
115 }
116 17 };
117  
118 # It's intentional that wrap is called separately for this. We want the
119 # post action to always be called, even if the pre-action short-circuits
120 # the request. Otherwise, would need to duplicate the storing logic.
121 # This does mean that, when both pre- and post-record callbacks are being
122 # used, that the post-callback will take precedence.
123  
124 $wrappers->{ post } = wrap 'LWP::UserAgent::simple_request',
125 22 ▶ post => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
126 22 my $response = $_[ -1 ];
127 22 ▶ if ( $callbacks->{ record }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callbacks->{"record"}) 9 13 22
128 $response = $callbacks->{ record }(
129 $self->{ current_request },
130 9 $response
131 );
132  
133 9 ▶ if ( not $response->$_isa( 'HTTP::Response' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->$LWP::UserAgent::Mockable::_isa("HTTP::Response")) 3 6 9
134 3 die "record callback didn't return an HTTP::Response object";
135 }
136 }
137  
138 19 ▶ if ( $action eq 'record' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "record") 12 7 19
139 12 local $Storable::Eval = 1;
140 12 local $Storable::Deparse = 1;
141  
142 my $cloned = dclone {
143 request => $self->{ current_request },
144 12 response => $response
145 };
146  
147 12 push @{ $self->{ actions } }, $cloned;
148 }
149 17 };
150 }
151 }
152  
153 9 ▶sub finished {
Subroutine finished: called CC=4 SCAR=13.9
Pod documented
154 9 my ( $class ) = @_;
155  
156 9 my $action = $instance->{ action };
157  
158 9 ▶ if ( $action eq 'record' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($action eq 'record') { } 3 6 9
elsif ($action eq 'playback' and scalar @{$instance->{'actions'};}) { } 0 8 8
159 3 local $Storable::Deparse = 1;
160 3 local $Storable::Eval = 1;
161  
162 3 nstore $instance->{ actions }, $instance->{ file };
163 4 ▶ } elsif ( $action eq 'playback' and scalar @{ $instance->{ actions } } ) {
Condition100%2/2
$action eq "playback" and scalar @{$instance->{"actions"};}
!ll
24
MC/DC100%1/1
$action eq "playback" and scalar @{$instance->{"actions"};}
$action eq "playback"
Truth table100%2/2
A
A= $action eq "playback"
Aresult
00
11
164 0 warn "Not all HTTP requests have been played back. You possibly need to re-record the LWP session";
165 }
166  
167 9 $instance->{ actions } = [];
168 9 $instance->{ action } = 'passthrough';
169 9 $instance->{ file } = undef;
170  
171 9 $instance->reset;
172 }
173  
174 2 ▶sub set_playback_callback {
Subroutine set_playback_callback: called CC=1 SCAR=0.0
Pod documented
175 2 my ( $class, $cb ) = @_;
176  
177 2 $instance->__set_cb( playback => $cb );
178 }
179  
180 15 ▶sub set_record_callback {
Subroutine set_record_callback: called CC=1 SCAR=0.0
Pod documented
181 15 my ( $class, $cb ) = @_;
182  
183 15 $instance->__set_cb( record => $cb );
184 }
185  
186 12 ▶sub set_record_pre_callback {
Subroutine set_record_pre_callback: called CC=1 SCAR=0.0
Pod documented
187 12 my ( $class, $cb ) = @_;
188  
189 12 $instance->__set_cb( pre_record => $cb );
190 }
191  
192 1 ▶sub set_playback_validation_callback {
Subroutine set_playback_validation_callback: called CC=1 SCAR=0.0
Pod documented
193 1 my ( $class, $cb ) = @_;
194  
195 1 $instance->__set_cb( playback_validation => $cb );
196 }
197  
198 30 ▶sub __set_cb {
Subroutine __set_cb: called CC=1 SCAR=0.0
199 30 my ( $self, $type, $cb ) = @_;
200  
201 30 $self->{ callbacks }{ $type } = $cb;
202 }
203  
204 10 1;
205  
206 __END__
↑ Summary