blib/lib/HTTP/API/Core/Response.pm

filter: Statementstmt 100% 104 / 104 Branchbran 98.5% 67 / 68 Conditioncond 97.9% 47 / 48 MC/DCmcdc 96.4% 27 / 28 Subroutinesub 100% 24 / 24 Podpod 100% 17 / 17 totaltotal 98.9% 286 / 289 CC 59 SCAR 24.6
CC
59
Cov
99%
new
26.0
_same_header_values
12.0
_scalar_header_value
7.0
CRAP 11.7
« blib/lib/HTTP/API/Core/RateLimit.pm ↑ Summary examples/HTTP/API/Core/Example/Jira.pm »
1 package HTTP::API::Core::Response;
2  
3 41 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 41 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 41 ▶use JSON::PP qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 41 ▶use HTTP::API::Core::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 41 ▶use HTTP::API::Core::RateLimit;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 238 ▶sub new {
Subroutine new: called CC=26 SCAR=32.6
Pod documented
10 238 my $class = shift;
11 238 ▶ die "response constructor options must be key/value pairs\n" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 237 238
12 die "constructor option names must be scalars\n"
13 237 ▶ if grep { ref($_) ne '' } @_[ grep { $_ % 2 == 0 } 0 .. $#_ ];
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "";} @_[grep {$_ % 2 == 0;} 0 .. $#_] 1 236 237
14 236 my %args = @_;
15  
16 die "response status must be a valid HTTP status\n"
17 if !defined($args{status})
18 || ref($args{status}) ne ''
19 || $args{status} !~ /\A[0-9]{3}\z/
20 || $args{status} < 100
21 236 ▶ || $args{status} > 599;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $args{"status"} or ref $args{"status"} ne "" or not $args{"status"} =~ /\A[0-9]{3}\z/ or $args{"status"} < 100 or $args{"status"} > 599 8 228 236
Condition100%12/12
not defined $args{"status"} or ref $args{"status"} ne "" or not $args{"status"} =~ /\A[0-9]{3}\z/ or $args{"status"} < 100 or $args{"status"} > 599
l!l&&r!l&&!r
71228
not defined $args{"status"} or ref $args{"status"} ne "" or not $args{"status"} =~ /\A[0-9]{3}\z/ or $args{"status"} < 100
l!l&&r!l&&!r
61229
not defined $args{"status"} or ref $args{"status"} ne "" or not $args{"status"} =~ /\A[0-9]{3}\z/
l!l&&r!l&&!r
24230
not defined $args{"status"} or ref $args{"status"} ne ""
l!l&&r!l&&!r
11234
MC/DC100%5/5
not defined $args{"status"} or ref $args{"status"} ne "" or not $args{"status"} =~ /\A[0-9]{3}\z/ or $args{"status"} < 100 or $args{"status"} > 599
not defined $args{"status"} ref $args{"status"} ne "" not $args{"status"} =~ /\A[0-9]{3}\z/ $args{"status"} < 100 $args{"status"} > 599
Truth table100%6/6
not(A) or B or not(C) or D or E
A= not defined $args{"status"}B= ref $args{"status"} ne ""C= not $args{"status"} =~ /\A[0-9]{3}\z/D= $args{"status"} < 100E= $args{"status"} > 599
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
22  
23 228 ▶ my $headers = defined($args{headers}) ? $args{headers} : {};
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'headers'} ? : 222 6 228
24 228 ▶ die "response headers must be a hash reference\n" if ref($headers) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $headers ne "HASH" 1 227 228
25 die "response header names must be scalars\n"
26 227 ▶ if grep { ref($_) ne '' } keys %$headers;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "";} keys %$headers 0 227 227
27 227 for my $value (values %$headers) {
28 154 ▶ next if !defined($value) || ref($value) eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value eq "" 125 29 154
Condition100%3/3
not defined $value or ref $value eq ""
l!l&&r!l&&!r
312229
MC/DC100%2/2
not defined $value or ref $value eq ""
not defined $value ref $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= ref $value eq ""
ABresult
1X1
011
000
29 die "response header values must be scalars, arrays of scalars, or undef\n"
30 if ref($value) ne 'ARRAY'
31 29 ▶ || grep { defined($_) && ref($_) ne '' } @$value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value ne "ARRAY" or grep {defined $_ and ref $_ ne "";} @$value 2 27 29
Condition100%6/6
ref $value ne "ARRAY" or grep {defined $_ and ref $_ ne "";} @$value
l!l&&r!l&&!r
1127
defined $_ && ref $_ ne ''
!ll&&!rl&&r
14331
MC/DC100%2/2
ref $value ne "ARRAY" or grep {defined $_ and ref $_ ne "";} @$value
ref $value ne "ARRAY" grep {defined $_ and ref $_ ne "";} @$value
MC/DC100%2/2
defined $_ && ref $_ ne ''
defined $_ ref $_ ne ''
Truth table100%3/3
A or B
A= ref $value ne "ARRAY"B= grep {defined $_ and ref $_ ne "";} @$value
ABresult
1X1
011
000
Truth table100%3/3
A && B
A= defined $_B= ref $_ ne ''
ABresult
0X0
100
111
32 }
33  
34 225 for my $name (qw(reason content method url elapsed)) {
35 die "response $name must be a scalar or undef\n"
36 1115 ▶ if defined($args{$name}) && ref($args{$name}) ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{$name} and ref $args{$name} ne "" 5 1110 1115
Condition100%3/3
defined $args{$name} and ref $args{$name} ne ""
!ll&&!rl&&r
1629485
MC/DC100%2/2
defined $args{$name} and ref $args{$name} ne ""
defined $args{$name} ref $args{$name} ne ""
Truth table100%3/3
A and B
A= defined $args{$name}B= ref $args{$name} ne ""
ABresult
0X0
100
111
37 }
38  
39 # Conflicting case-insensitive header names are ambiguous. Never let
40 # hash iteration order decide which value controls retry behavior.
41 220 my (%normalized_headers, %conflicts);
42 220 for my $name (keys %$headers) {
43 152 my $key = lc $name;
44 152 my $value = $headers->{$name};
45 152 ▶ if (exists $normalized_headers{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $normalized_headers{$key}) 14 138 152
46 $conflicts{$key} = 1
47 14 ▶ if !_same_header_values($normalized_headers{$key}, $value);
Branch: 1 branch
BranchTrueFalseTotal
unless _same_header_values($normalized_headers{$key}, $value) 8 6 14
48 }
49 152 ▶ $normalized_headers{$key} = ref($value) eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 27 125 152
50 ? [ @$value ] : $value;
51 }
52 220 $normalized_headers{$_} = undef for keys %conflicts;
53  
54 return bless {
55 status => $args{status},
56 reason => $args{reason},
57 headers => \%normalized_headers,
58 content => defined($args{content}) ? $args{content} : '',
59 method => $args{method},
60 url => $args{url},
61 elapsed => $args{elapsed},
62 220 ▶ }, $class;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'content'} ? : 208 12 220
63 }
64  
65 # Compare values without stringifying array references (or choosing a
66 # case-insensitive duplicate arbitrarily).
67 14 ▶sub _same_header_values {
Subroutine _same_header_values: called CC=12 SCAR=24.9
68 14 my ($left, $right) = @_;
69 14 ▶ return 0 if ref($left) ne ref($right);
Branch: 1 branch
BranchTrueFalseTotal
if ref $left ne ref $right 1 13 14
70  
71 13 ▶ if (ref($left) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $left eq "ARRAY") 5 8 13
72 5 ▶ return 0 if @$left != @$right;
Branch: 1 branch
BranchTrueFalseTotal
if @$left != @$right 1 4 5
73 4 for my $i (0 .. $#$left) {
74 6 ▶ return 0 if defined($left->[$i]) != defined($right->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
if defined $left->[$i] != defined $right->[$i] 1 5 6
75 5 ▶ return 0 if defined($left->[$i]) && $left->[$i] ne $right->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if defined $left->[$i] and $left->[$i] ne $right->[$i] 1 4 5
Condition100%3/3
defined $left->[$i] and $left->[$i] ne $right->[$i]
!ll&&!rl&&r
221
MC/DC100%2/2
defined $left->[$i] and $left->[$i] ne $right->[$i]
defined $left->[$i] $left->[$i] ne $right->[$i]
Truth table100%3/3
A and B
A= defined $left->[$i]B= $left->[$i] ne $right->[$i]
ABresult
0X0
100
111
76 }
77 2 return 1;
78 }
79  
80 8 ▶ return 1 if !defined($left) && !defined($right);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $left or defined $right 1 7 8
Condition100%3/3
defined $left or defined $right
l!l&&r!l&&!r
611
MC/DC100%2/2
defined $left or defined $right
defined $left defined $right
Truth table100%3/3
A or B
A= defined $leftB= defined $right
ABresult
1X1
011
000
81 7 ▶ return defined($left) && defined($right) && $left eq $right;
Condition83%5/6
defined $left && defined $right && $left eq $right
!ll&&!rl&&r
133
defined $left and defined $right
!ll&&!rl&&r
106
MC/DC66%2/3
defined $left && defined $right && $left eq $right
defined $left defined $right $left eq $right
Truth table75%3/4
A and B && C
A= defined $leftB= defined $rightC= $left eq $right
ABCresult
0XX0
10X0
1100
1111
82 }
83  
84 # Scalar accessors are intentionally conservative: identical repeated values
85 # are unambiguous, while conflicting values do not control API policy.
86 636 ▶sub _scalar_header_value {
Subroutine _scalar_header_value: called CC=7 SCAR=19.5
87 636 my ($value) = @_;
88 636 ▶ return $value if ref($value) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value ne "ARRAY" 622 14 636
89 14 ▶ return undef if !@$value;
Branch: 1 branch
BranchTrueFalseTotal
unless @$value 2 12 14
90  
91 12 my $first = $value->[0];
92 12 for my $item (@$value) {
93 24 ▶ return undef if defined($first) != defined($item);
Branch: 1 branch
BranchTrueFalseTotal
if defined $first != defined $item 4 20 24
94 20 ▶ return undef if defined($first) && $first ne $item;
Branch: 1 branch
BranchTrueFalseTotal
if defined $first and $first ne $item 3 17 20
Condition100%3/3
defined $first and $first ne $item
!ll&&!rl&&r
1073
MC/DC100%2/2
defined $first and $first ne $item
defined $first $first ne $item
Truth table100%3/3
A and B
A= defined $firstB= $first ne $item
ABresult
0X0
100
111
95 }
96 5 return $first;
97 }
98  
99 393 ▶sub status { $_[0]->{status} }
Subroutine status: called CC=1 SCAR=0.0
Pod documented
100 182 ▶sub reason { $_[0]->{reason} }
Subroutine reason: called CC=1 SCAR=0.0
Pod documented
101 17 ▶sub headers {
Subroutine headers: called CC=3 SCAR=11.0
Pod documented
102 17 my ($self) = @_;
103 17 my %headers = %{ $self->{headers} };
104 17 for my $name (keys %headers) {
105 20 $headers{$name} = [ @{ $headers{$name} } ]
106 36 ▶ if ref($headers{$name}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $headers{$name} eq "ARRAY" 20 16 36
107 }
108 17 return \%headers;
109 }
110 7 ▶sub content { $_[0]->{content} }
Subroutine content: called CC=1 SCAR=0.0
Pod documented
111 2 ▶sub text { $_[0]->{content} }
Subroutine text: called CC=1 SCAR=0.0
Pod documented
112 3 ▶sub has_content { length($_[0]->{content}) ? 1 : 0 }
Subroutine has_content: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
length $_[0]{'content'} ? : 2 1 3
113  
114 14 ▶sub content_type {
Subroutine content_type: called CC=3 SCAR=11.0
Pod documented
115 14 my ($self) = @_;
116 14 my $value = $self->header('content-type');
117 14 ▶ return undef if !defined $value || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 8 6 14
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
716
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
118 6 $value =~ s/\s*;.*\z//;
119 6 $value =~ s/^\s+|\s+$//g;
120 6 return lc $value;
121 }
122  
123 4 ▶sub is_json {
Subroutine is_json: called CC=4 SCAR=13.9
Pod documented
124 4 my ($self) = @_;
125 4 my $type = $self->content_type;
126 4 ▶ return 0 if !defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 1 3 4
127 3 ▶ return 1 if $type eq 'application/json';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "application/json" 1 2 3
128 2 ▶ return 1 if $type =~ m{\Aapplication/[a-z0-9.!#\$&^_+\-]+\+json\z}i;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[\Aapplication/[a-z0-9.!#\$&^_+\-]+\+json\z]i 1 1 2
129 1 return 0;
130 }
131  
132 3 ▶sub method { $_[0]->{method} }
Subroutine method: called CC=1 SCAR=0.0
Pod documented
133 3 ▶sub elapsed { $_[0]->{elapsed} }
Subroutine elapsed: called CC=1 SCAR=0.0
Pod documented
134  
135 190 ▶sub request_id {
Subroutine request_id: called CC=4 SCAR=13.9
Pod documented
136 190 my ($self) = @_;
137 190 for my $name (qw(x-request-id request-id x-correlation-id)) {
138 533 my $value = $self->header($name);
139 533 ▶ return $value if defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and length $value 27 506 533
Condition100%3/3
defined $value and length $value
!ll&&!rl&&r
505127
MC/DC100%2/2
defined $value and length $value
defined $value length $value
Truth table100%3/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
140 }
141 163 return undef;
142 }
143  
144 6 ▶sub url { $_[0]->{url} }
Subroutine url: called CC=1 SCAR=0.0
Pod documented
145 187 ▶sub is_success { $_[0]->{status} >= 200 && $_[0]->{status} < 300 }
Subroutine is_success: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"status"} >= 200 and $_[0]{"status"} < 300 185 2 187
146 638 ▶sub header {
Subroutine header: called CC=3 SCAR=11.0
Pod documented
147 638 my ($self, $name) = @_;
148 638 ▶ die "response header name must be a defined scalar\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name or ref $name ne "" 2 636 638
Condition100%3/3
not defined $name or ref $name ne ""
l!l&&r!l&&!r
11636
MC/DC100%2/2
not defined $name or ref $name ne ""
not defined $name ref $name ne ""
Truth table100%3/3
not(A) or B
A= not defined $nameB= ref $name ne ""
ABresult
1X1
011
000
149 if !defined($name) || ref($name) ne '';
150 636 return _scalar_header_value($self->{headers}{lc $name});
151 }
152 116 ▶sub rate_limit { HTTP::API::Core::RateLimit->from_headers($_[0]->{headers}) }
Subroutine rate_limit: called CC=1 SCAR=0.0
Pod documented
153  
154 33 ▶sub json {
Subroutine json: called CC=3 SCAR=11.0
Pod documented
155 33 my ($self) = @_;
156 33 ▶ return undef if $self->{content} !~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"content"} =~ /\S/ 2 31 33
157  
158 31 my $decoded = eval { decode_json($self->{content}) };
159 31 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 3 28 31
160 die HTTP::API::Core::Error->new(
161 category => 'decode',
162 status => $self->{status},
163 method => $self->{method},
164 url => $self->{url},
165 3 response => $self,
166 message => "failed to decode JSON response: $@",
167 );
168 }
169 28 return $decoded;
170 }
171  
172 41 1;
173  
174 __END__
« blib/lib/HTTP/API/Core/RateLimit.pm ↑ Summary examples/HTTP/API/Core/Example/Jira.pm »