File Coverage

blib/lib/Paws/Net/MockCaller.pm
Criterion Covered Total %
statement 33 41 80.4
branch 8 16 50.0
condition n/a
subroutine 8 8 100.0
pod 0 2 0.0
total 49 67 73.1


line stmt bran cond sub pod time code
1             package Paws::Net::MockCaller;
2 3     3   9163 use Moose;
  3         9  
  3         22  
3             with 'Paws::Net::RetryCallerRole', 'Paws::Net::CallerRole';
4              
5 3     3   31814 use File::Slurper qw(read_text write_text);
  3         63130  
  3         222  
6 3     3   28 use JSON::MaybeXS;
  3         7  
  3         159  
7 3     3   21 use Moose::Util::TypeConstraints;
  3         6  
  3         34  
8 3     3   16490 use Path::Tiny;
  3         39734  
  3         186  
9 3     3   1886 use Paws::Net::FileMockCaller;
  3         13  
  3         1841  
10              
11             has real_caller => (
12             is => 'ro',
13             does => 'Paws::Net::CallerRole',
14             default => sub {
15             require Paws::Net::Caller;
16             Paws::Net::Caller->new;
17             }
18             );
19              
20             has mock_mode => (
21             is => 'ro',
22             isa => enum([ 'REPLAY', 'RECORD' ]),
23             required => 1,
24             default => sub { $ENV{PAWS_MOCK_MODE} }
25             );
26              
27             has mock_dir => (
28             is => 'ro',
29             isa => 'Str',
30             required => 1,
31             default => sub { $ENV{PAWS_MOCK_DIR} }
32             );
33              
34             has _request_num => (
35             is => 'ro',
36             isa => 'Int',
37             default => 1,
38             traits => [ 'Counter' ],
39             handles => {
40             _next_request => 'inc'
41             }
42             );
43              
44             has _test_file => (is => 'rw', isa => 'Str');
45              
46             has caller => (
47             is => 'ro',
48             lazy => 1,
49             default => sub {
50             my $self = shift;
51             Paws::Net::FileMockCaller->new(
52             real_caller => $self->real_caller,
53             );
54             }
55             );
56              
57             has result_hook => (
58             is => 'ro',
59             isa => 'CodeRef',
60             );
61              
62             sub send_request {
63 61     61 0 178 my ($self, $service, $call_object) = @_;
64              
65 61         1558 $self->_test_file(sprintf("%s/%04d.response", $self->mock_dir, $self->_request_num));
66 61         2030 $self->_next_request;
67              
68 61 50       1530 if ($self->mock_mode eq 'REPLAY') {
    0          
69 61         1466 $self->caller->file($self->_test_file);
70 61         1293 return $self->caller->send_request($service, $call_object);
71             } elsif ($self->mock_mode eq 'RECORD') {
72 0         0 return $self->real_caller->send_request($service, $call_object);
73             } else {
74 0         0 die "Unsupported record mode " . $self->mock_mode;
75             }
76             };
77              
78             sub caller_to_response {
79 61     61 0 216 my ($self, $service, $call_object, $status, $content, $headers) = @_;
80            
81 61 50       556 $content =~ s/<(RequestId)>.*<\/(RequestId)>/<$1>000000000000000000000000000000000000<\/$2>/ if (defined $content);
82 61 50       473 $content =~ s/<(RequestID)>.*<\/(RequestID)>/<$1>000000000000000000000000000000000000<\/$2>/ if (defined $content);
83              
84 61 100       221 if ($headers->{ "x-amzn-requestid" }) {
85 20         41 $headers->{ "x-amzn-requestid" } = '000000000000000000000000000000000000'
86             }
87              
88 61 100       188 if ($headers->{ "x-amz-request-id" }) {
89 27         66 $headers->{ "x-amz-request-id" } = '000000000000000000000000000000000000'
90             }
91              
92 61 50       1429 if ($self->mock_mode eq 'RECORD') {
93 0         0 my $file = path $self->_test_file;
94 0         0 $file->parent->mkpath;
95              
96 0         0 write_text($self->_test_file, encode_json({
97             request => {
98             params => $service->to_hash($call_object),
99             service => $service->service,
100             call => $call_object->_api_call,
101             },
102             response => {
103             content => $content,
104             headers => $headers,
105             status => $status
106             }
107             }));
108              
109 0         0 my $result = $self->real_caller->caller_to_response($service, $call_object, $status, $content, $headers);
110 0 0       0 $self->result_hook->($self, $result) if (defined $self->result_hook);
111 0         0 return $result;
112             } else {
113 61         1453 return $self->real_caller->caller_to_response($service, $call_object, $status, $content, $headers);
114             }
115             };
116              
117             1;