| 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.16'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 10 | my $instance = __PACKAGE__->__instance; | |||||||||||||||||||||||||||
| 14 | sub __instance { | ||||||||||||||||||||||||||||
| 15 | 12 | ▶ | my ( $class ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__instance: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 12 | ▶ | if ( not defined $instance ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 12 | $instance->reset( $action, $ENV{ LWP_UA_MOCK_FILE } ); | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 10 | return $instance; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | sub reset { | ||||||||||||||||||||||||||||
| 38 | 23 | ▶ | my ( $class, $action, $file ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
reset: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 23 | ▶ | if ( scalar @{ $instance->{ actions } } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 45 | 9 | $action = "passthrough"; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 23 | ▶ | if ( $action !~ /^(playback|record|passthrough)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$action ne "passthrough" and not defined $file
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
| |||||||||||||||||||||||||||||
| 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 | sub __reset { | ||||||||||||||||||||||||||||
| 65 | 22 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__reset: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 71 | 5 | local $Storable::Eval = 1; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 5 | $self->{ actions } = retrieve( $file ); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | $wrappers->{ pre } = wrap 'LWP::UserAgent::simple_request', | ||||||||||||||||||||||||||||
| 76 | pre => sub { | ||||||||||||||||||||||||||||
| 77 | 17 | ▶ | my ( $wrapped, $request ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 17 | my $current = shift @{ $self->{ actions } }; | |||||||||||||||||||||||||||
| 80 | 17 | ▶ | if ( not defined $current ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 93 | 1 | $response = $callbacks->{ playback }( $request, $response ); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1 | ▶ | if ( not $response->$_isa( 'HTTP::Response' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | pre => sub { | ||||||||||||||||||||||||||||
| 105 | 25 | ▶ | my ( $wrapped, $request ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 25 | $self->{ current_request } = $request; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 25 | ▶ | if ( $callbacks->{ pre_record } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 9 | $_[ -1 ] = $callbacks->{ pre_record }( $request ); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 9 | ▶ | if ( not $_[ -1 ]->$_isa( 'HTTP::Response' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | post => sub { | ||||||||||||||||||||||||||||
| 126 | 22 | ▶ | my $response = $_[ -1 ]; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 127 | 22 | ▶ | if ( $callbacks->{ record }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | $response = $callbacks->{ record }( | ||||||||||||||||||||||||||||
| 129 | $self->{ current_request }, | ||||||||||||||||||||||||||||
| 130 | 9 | $response | |||||||||||||||||||||||||||
| 131 | ); | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 9 | ▶ | if ( not $response->$_isa( 'HTTP::Response' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 3 | die "record callback didn't return an HTTP::Response object"; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 19 | ▶ | if ( $action eq 'record' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | sub finished { | ||||||||||||||||||||||||||||
| 154 | 9 | ▶ | my ( $class ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
finished: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 9 | my $action = $instance->{ action }; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 9 | ▶ | if ( $action eq 'record' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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"};}
MC/DC100%1/1
$action eq "playback" and scalar @{$instance->{"actions"};}
$action eq "playback"
Truth table100%2/2
A
A= $action eq "playback"
| |||||||||||||||||||||||||||||
| 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 | sub set_playback_callback { | ||||||||||||||||||||||||||||
| 175 | 2 | ▶ | my ( $class, $cb ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set_playback_callback: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 2 | $instance->__set_cb( playback => $cb ); | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | sub set_record_callback { | ||||||||||||||||||||||||||||
| 181 | 15 | ▶ | my ( $class, $cb ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set_record_callback: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 15 | $instance->__set_cb( record => $cb ); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | sub set_record_pre_callback { | ||||||||||||||||||||||||||||
| 187 | 12 | ▶ | my ( $class, $cb ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set_record_pre_callback: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 12 | $instance->__set_cb( pre_record => $cb ); | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | sub set_playback_validation_callback { | ||||||||||||||||||||||||||||
| 193 | 1 | ▶ | my ( $class, $cb ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set_playback_validation_callback: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 1 | $instance->__set_cb( playback_validation => $cb ); | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | sub __set_cb { | ||||||||||||||||||||||||||||
| 199 | 30 | ▶ | my ( $self, $type, $cb ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__set_cb: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 30 | $self->{ callbacks }{ $type } = $cb; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 10 | 1; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | __END__ | ||||||||||||||||||||||||||||