| 1 | package Paws::Net::MockCaller; | ||||||||||||||
| 2 | 6 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 6 | with 'Paws::Net::RetryCallerRole', 'Paws::Net::CallerRole'; | |||||||||||||
| 4 | 6 | ▶ | use Paws::Net::APIResponse; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 6 | ▶ | use File::Slurper qw(read_text write_text); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 6 | ▶ | use JSON::MaybeXS; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 6 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 6 | ▶ | use Path::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 6 | ▶ | use Paws::Net::FileMockCaller; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 6 | ▶ | use Paws::Net::NoResponseMockCaller; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | has real_caller => ( | ||||||||||||||
| 14 | is => 'ro', | ||||||||||||||
| 15 | does => 'Paws::Net::CallerRole', | ||||||||||||||
| 16 | 12 | ▶ | default => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 12 | require Paws::Net::Caller; | |||||||||||||
| 18 | 12 | Paws::Net::Caller->new; | |||||||||||||
| 19 | } | ||||||||||||||
| 20 | 6 | ); | |||||||||||||
| 21 | |||||||||||||||
| 22 | has mock_type => ( | ||||||||||||||
| 23 | is => 'ro', | ||||||||||||||
| 24 | isa => 'Str', | ||||||||||||||
| 25 | 14 | ▶ | default => sub { 'FileMockCaller' }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 26 | 6 | ); | |||||||||||||
| 27 | |||||||||||||||
| 28 | has mock_mode => ( | ||||||||||||||
| 29 | is => 'ro', | ||||||||||||||
| 30 | lazy => 1, | ||||||||||||||
| 31 | isa => enum([ 'REPLAY', 'RECORD' ]), | ||||||||||||||
| 32 | required => 0, | ||||||||||||||
| 33 | 0 | ▶ | default => sub { $ENV{PAWS_MOCK_MODE} } | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 34 | 6 | ); | |||||||||||||
| 35 | |||||||||||||||
| 36 | has mock_dir => ( | ||||||||||||||
| 37 | is => 'ro', | ||||||||||||||
| 38 | isa => 'Str', | ||||||||||||||
| 39 | lazy => 1, | ||||||||||||||
| 40 | required => 0, | ||||||||||||||
| 41 | 0 | ▶ | default => sub { $ENV{PAWS_MOCK_DIR} } | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 42 | 6 | ); | |||||||||||||
| 43 | |||||||||||||||
| 44 | 6 | has _request_num => ( | |||||||||||||
| 45 | is => 'ro', | ||||||||||||||
| 46 | isa => 'Int', | ||||||||||||||
| 47 | default => 1, | ||||||||||||||
| 48 | traits => [ 'Counter' ], | ||||||||||||||
| 49 | handles => { | ||||||||||||||
| 50 | _next_request => 'inc' | ||||||||||||||
| 51 | } | ||||||||||||||
| 52 | ); | ||||||||||||||
| 53 | |||||||||||||||
| 54 | 6 | has _test_file => (is => 'rw', isa => 'Str'); | |||||||||||||
| 55 | |||||||||||||||
| 56 | has caller => ( | ||||||||||||||
| 57 | is => 'ro', | ||||||||||||||
| 58 | lazy => 1, | ||||||||||||||
| 59 | 14 | ▶ | default => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 60 | 14 | my $self = shift; | |||||||||||||
| 61 | 14 | my $mock_class = 'Paws::Net::' . $self->mock_type; | |||||||||||||
| 62 | 14 | $mock_class->new( | |||||||||||||
| 63 | real_caller => $self->real_caller, | ||||||||||||||
| 64 | ); | ||||||||||||||
| 65 | } | ||||||||||||||
| 66 | 6 | ); | |||||||||||||
| 67 | |||||||||||||||
| 68 | 6 | has result_hook => ( | |||||||||||||
| 69 | is => 'ro', | ||||||||||||||
| 70 | isa => 'CodeRef', | ||||||||||||||
| 71 | ); | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 0 | ▶ | sub actual_request { | ||||||||||||
|
Subroutine
actual_request: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 74 | 0 | return $_[0]->caller->actual_request; | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | |||||||||||||||
| 77 | 73 | ▶ | sub send_request { | ||||||||||||
|
Subroutine
send_request: called CC=4 SCAR=16.8
Pod
undocumented
| |||||||||||||||
| 78 | 73 | my ($self, $service, $call_object) = @_; | |||||||||||||
| 79 | |||||||||||||||
| 80 | 73 | ▶ | if ($self->mock_type eq 'FileMockCaller') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 81 | 73 | $self->_test_file(sprintf("%s/%04d.response", $self->mock_dir, $self->_request_num)); | |||||||||||||
| 82 | 73 | $self->_next_request; | |||||||||||||
| 83 | |||||||||||||||
| 84 | 73 | ▶ | if ($self->mock_mode eq 'REPLAY') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 85 | 73 | $self->caller->file($self->_test_file); | |||||||||||||
| 86 | 73 | return $self->caller->send_request($service, $call_object); | |||||||||||||
| 87 | } elsif ($self->mock_mode eq 'RECORD') { | ||||||||||||||
| 88 | 0 | return $self->real_caller->send_request($service, $call_object); | |||||||||||||
| 89 | } else { | ||||||||||||||
| 90 | 0 | die "Unsupported record mode " . $self->mock_mode; | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | } else { | ||||||||||||||
| 93 | 0 | return $self->caller->send_request($service, $call_object); | |||||||||||||
| 94 | } | ||||||||||||||
| 95 | }; | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 73 | ▶ | sub caller_to_response { | ||||||||||||
|
Subroutine
caller_to_response: called CC=8 SCAR=21.6
Pod
undocumented
| |||||||||||||||
| 98 | 73 | my ($self, $service, $call_object, $response) = @_; | |||||||||||||
| 99 | |||||||||||||||
| 100 | 73 | ▶ | if ($self->mock_type eq 'FileMockCaller') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 101 | 73 | my $content = $response->content; | |||||||||||||
| 102 | 73 | my $headers = $response->headers; | |||||||||||||
| 103 | |||||||||||||||
| 104 | 73 | ▶ | $content =~ s/<(RequestId)>.*<\/(RequestId)>/<$1>000000000000000000000000000000000000<\/$2>/ if (defined $content); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 105 | 73 | ▶ | $content =~ s/<(RequestID)>.*<\/(RequestID)>/<$1>000000000000000000000000000000000000<\/$2>/ if (defined $content); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 106 | |||||||||||||||
| 107 | 73 | ▶ | if ($headers->{ "x-amzn-requestid" }) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 108 | 24 | $headers->{ "x-amzn-requestid" } = '000000000000000000000000000000000000' | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | |||||||||||||||
| 111 | 73 | ▶ | if ($headers->{ "x-amz-request-id" }) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 112 | 35 | $headers->{ "x-amz-request-id" } = '000000000000000000000000000000000000' | |||||||||||||
| 113 | } | ||||||||||||||
| 114 | |||||||||||||||
| 115 | 73 | ▶ | if ($self->mock_mode eq 'RECORD') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 116 | 0 | my $file = path $self->_test_file; | |||||||||||||
| 117 | 0 | $file->parent->mkpath; | |||||||||||||
| 118 | |||||||||||||||
| 119 | 0 | write_text($self->_test_file, encode_json({ | |||||||||||||
| 120 | request => { | ||||||||||||||
| 121 | params => $service->to_hash($call_object), | ||||||||||||||
| 122 | service => $service->service, | ||||||||||||||
| 123 | call => $call_object->_api_call, | ||||||||||||||
| 124 | }, | ||||||||||||||
| 125 | response => { | ||||||||||||||
| 126 | content => $response->content, | ||||||||||||||
| 127 | headers => $response->headers, | ||||||||||||||
| 128 | status => $response->status, | ||||||||||||||
| 129 | } | ||||||||||||||
| 130 | })); | ||||||||||||||
| 131 | } | ||||||||||||||
| 132 | 73 | my $result = $self->caller->caller_to_response($service, $call_object, $response); | |||||||||||||
| 133 | 73 | ▶ | $self->result_hook->($self, $result) if (defined $self->result_hook); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 134 | 73 | return $result; | |||||||||||||
| 135 | } else { | ||||||||||||||
| 136 | 0 | return $self->caller->caller_to_response($service, $call_object, $response); | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | }; | ||||||||||||||
| 139 | |||||||||||||||
| 140 | 6 | 1; | |||||||||||||