| 1 | package Test::Mock::HTTP::Tiny; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Record and replay HTTP requests/responses with HTTP::Tiny | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = '0.002'; # VERSION | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Test::Deep::NoTest; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | my $captured_data = []; | |||||||||||||||||||||||||||
| 17 | 2 | my $mocked_data = []; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 5 | ▶ | sub mocked_data { | ||||||||||||||||||||||||||
|
Subroutine
mocked_data: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 5 | return $mocked_data; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | sub set_mocked_data { | ||||||||||||||||||||||||||
|
Subroutine
set_mocked_data: called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 1 | my ($class, $new_mocked_data) = @_; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | if (ref($new_mocked_data) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 29 | # An arrayref of items was provided | ||||||||||||||||||||||||||||
| 30 | 1 | $mocked_data = [ @$new_mocked_data ]; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | elsif (ref($new_mocked_data) eq 'HASH') { | ||||||||||||||||||||||||||||
| 33 | # A single item was provided | ||||||||||||||||||||||||||||
| 34 | 0 | $mocked_data = [ { %$mocked_data } ]; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | else { | ||||||||||||||||||||||||||||
| 37 | # TODO: error | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1 | ▶ | sub append_mocked_data { | ||||||||||||||||||||||||||
|
Subroutine
append_mocked_data: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 1 | my ($class, $new_mocked_data) = @_; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | ▶ | if (ref($new_mocked_data) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 46 | # Multiple items are being appended | ||||||||||||||||||||||||||||
| 47 | 0 | push @$mocked_data, @$new_mocked_data; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | elsif (ref($new_mocked_data) eq 'HASH') { | ||||||||||||||||||||||||||||
| 50 | # Single item is being appended | ||||||||||||||||||||||||||||
| 51 | 1 | push @$mocked_data, { %$new_mocked_data }; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | else { | ||||||||||||||||||||||||||||
| 54 | # TODO: error | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | sub clear_mocked_data { | ||||||||||||||||||||||||||
|
Subroutine
clear_mocked_data: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 1 | $mocked_data = []; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | sub captured_data { | ||||||||||||||||||||||||||
|
Subroutine
captured_data: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 0 | return $captured_data; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub captured_data_dump { | ||||||||||||||||||||||||||
|
Subroutine
captured_data_dump: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 0 | local $Data::Dumper::Deepcopy = 1; | |||||||||||||||||||||||||||
| 71 | 0 | return Dumper $captured_data; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub clear_captured_data { | ||||||||||||||||||||||||||
|
Subroutine
clear_captured_data: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 0 | $captured_data = []; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | { | ||||||||||||||||||||||||||||
| 80 | ## no critic | ||||||||||||||||||||||||||||
| 81 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 82 | 2 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | 2 | my $_HTTP_Tiny__request = \&HTTP::Tiny::_request; | |||||||||||||||||||||||||||
| 84 | 2 | ▶ | *{"HTTP::Tiny::_request"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=22.6
| |||||||||||||||||||||||||||||
| 85 | 1 | my ($self, $method, $url, $args) = @_; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | my $normalized_args = { %$args }; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | ▶ | if (exists $args->{headers}{'content-type'} && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $args->{"headers"}{"content-type"} and $args->{"headers"}{"content-type"} eq "application/x-www-form-urlencoded"
MC/DC0%0/2
exists $args->{"headers"}{"content-type"} and $args->{"headers"}{"content-type"} eq "application/x-www-form-urlencoded"
exists $args->{"headers"}{"content-type"}
$args->{"headers"}{"content-type"} eq "application/x-www-form-urlencoded"
Truth table33%1/3
A and B
A= exists $args->{"headers"}{"content-type"}B= $args->{"headers"}{"content-type"} eq "application/x-www-form-urlencoded"
| |||||||||||||||||||||||||||||
| 90 | $args->{headers}{'content-type'} eq | ||||||||||||||||||||||||||||
| 91 | 'application/x-www-form-urlencoded') | ||||||||||||||||||||||||||||
| 92 | { | ||||||||||||||||||||||||||||
| 93 | # Unescape form data | ||||||||||||||||||||||||||||
| 94 | 0 | $normalized_args->{content} = {}; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | for my $param (split(/&/, $args->{content})) { | |||||||||||||||||||||||||||
| 97 | my ($name, $value) = | ||||||||||||||||||||||||||||
| 98 | 0 | map { uri_unescape($_) } split(/=/, $param, 2); | |||||||||||||||||||||||||||
| 99 | 0 | $normalized_args->{content}{$name} = $value; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 1 | for my $i (0 .. $#{$mocked_data}) { | |||||||||||||||||||||||||||
| 104 | 1 | my $mock_req = $mocked_data->[$i]; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | next if !eq_deeply( | ||||||||||||||||||||||||||||
| 107 | 1 | ▶ | [ $mock_req->{method}, $mock_req->{url}, $mock_req->{args} ], | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | [ $method, $url, $normalized_args ] | ||||||||||||||||||||||||||||
| 109 | ); | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # Found a matching request in mocked data | ||||||||||||||||||||||||||||
| 112 | 1 | $mock_req = { %$mock_req }; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # Remove the request from mocked data so that it's not used again | ||||||||||||||||||||||||||||
| 115 | 1 | splice(@$mocked_data, $i, 1); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # Return the corresponding response | ||||||||||||||||||||||||||||
| 118 | 1 | return $mock_req->{response}; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # No matching request found -- call the actual HTTP::Tiny request method | ||||||||||||||||||||||||||||
| 122 | 0 | my $response = &$_HTTP_Tiny__request($self, $method, $url, $args); | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # Save the request/response in captured data | ||||||||||||||||||||||||||||
| 125 | 0 | push @$captured_data, { | |||||||||||||||||||||||||||
| 126 | method => $method, | ||||||||||||||||||||||||||||
| 127 | url => $url, | ||||||||||||||||||||||||||||
| 128 | args => $normalized_args, | ||||||||||||||||||||||||||||
| 129 | response => $response, | ||||||||||||||||||||||||||||
| 130 | }; | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | return $response; | |||||||||||||||||||||||||||
| 133 | 2 | }; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 2 | 1; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | __END__ | ||||||||||||||||||||||||||||