| 1 | package PAGI::Test::Response; | ||||||||||||||||||||||||||||||||
| 2 | 14 | $PAGI::Test::Response::VERSION = '0.002002'; | |||||||||||||||||||||||||||||||
| 3 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 14 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 14 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | # Maximum bytes of response body to include in JSON decode error messages | ||||||||||||||||||||||||||||||||
| 8 | 14 | our $JSON_ERROR_BODY_LIMIT = 1500; | |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 86 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 11 | 86 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||
| 12 | return bless { | ||||||||||||||||||||||||||||||||
| 13 | status => $args{status} // 200, | ||||||||||||||||||||||||||||||||
| 14 | headers => $args{headers} // [], | ||||||||||||||||||||||||||||||||
| 15 | body => $args{body} // '', | ||||||||||||||||||||||||||||||||
| 16 | exception => $args{exception}, | ||||||||||||||||||||||||||||||||
| 17 | 86 | ▶ | }, $class; | ||||||||||||||||||||||||||||||
|
Condition83%5/6
$args{"status"} // 200
$args{"headers"} // []
$args{"body"} // ""
MC/DC0%0/1
$args{"status"} // 200
$args{"status"}
MC/DC100%1/1
$args{"headers"} // []
$args{"headers"}
MC/DC100%1/1
$args{"body"} // ""
$args{"body"}
Truth table50%1/2
A
A= $args{"status"}
Truth table100%2/2
A
A= $args{"headers"}
Truth table100%2/2
A
A= $args{"body"}
| |||||||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | # Status code | ||||||||||||||||||||||||||||||||
| 21 | 36 | ▶ | sub status { shift->{status} } | ||||||||||||||||||||||||||||||
|
Subroutine
status: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | # Raw body bytes | ||||||||||||||||||||||||||||||||
| 24 | 12 | ▶ | sub content { shift->{body} } | ||||||||||||||||||||||||||||||
|
Subroutine
content: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | # Decoded text based on Content-Type charset | ||||||||||||||||||||||||||||||||
| 27 | 35 | ▶ | sub text { | ||||||||||||||||||||||||||||||
|
Subroutine
text: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 28 | 35 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 29 | 35 | my $body = $self->{body}; | |||||||||||||||||||||||||||||||
| 30 | 35 | ▶ | return $body unless defined $body && length $body; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $body and length $body
MC/DC50%1/2
defined $body and length $body
defined $body
length $body
Truth table66%2/3
A and B
A= defined $bodyB= length $body
| |||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | # Parse charset from Content-Type header | ||||||||||||||||||||||||||||||||
| 33 | 34 | ▶ | my $charset = $self->_extract_charset // 'UTF-8'; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->_extract_charset // "UTF-8"
MC/DC100%1/1
$self->_extract_charset // "UTF-8"
$self->_extract_charset
Truth table100%2/2
A
A= $self->_extract_charset
| |||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | 34 | require Encode; | |||||||||||||||||||||||||||||||
| 36 | 34 | return Encode::decode($charset, $body, Encode::FB_CROAK()); | |||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | # Extract charset from Content-Type header | ||||||||||||||||||||||||||||||||
| 40 | 34 | ▶ | sub _extract_charset { | ||||||||||||||||||||||||||||||
|
Subroutine
_extract_charset: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 41 | 34 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 42 | 34 | ▶ | my $ct = $self->content_type or return undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | # Match charset=... (with or without quotes) | ||||||||||||||||||||||||||||||||
| 45 | 31 | ▶ | if ($ct =~ /charset\s*=\s*"?([^";,\s]+)"?/i) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | 4 | return $1; | |||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||
| 48 | 27 | return undef; | |||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | # Header lookup (case-insensitive) | ||||||||||||||||||||||||||||||||
| 52 | 43 | ▶ | sub header { | ||||||||||||||||||||||||||||||
|
Subroutine
header: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 53 | 43 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||
| 54 | 43 | $name = lc($name); | |||||||||||||||||||||||||||||||
| 55 | 43 | for my $pair (@{$self->{headers}}) { | |||||||||||||||||||||||||||||||
| 56 | 43 | ▶ | return $pair->[1] if lc($pair->[0]) eq $name; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||
| 58 | 4 | return undef; | |||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | # All headers as hashref (last value wins for duplicates) | ||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub headers { | ||||||||||||||||||||||||||||||
|
Subroutine
headers: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 63 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 64 | 0 | my %h; | |||||||||||||||||||||||||||||||
| 65 | 0 | for my $pair (@{$self->{headers}}) { | |||||||||||||||||||||||||||||||
| 66 | 0 | $h{lc($pair->[0])} = $pair->[1]; | |||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||
| 68 | 0 | return \%h; | |||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | # Status helpers | ||||||||||||||||||||||||||||||||
| 72 | 4 | ▶ | sub is_success { my $s = shift->status; $s >= 200 && $s < 300 } | ||||||||||||||||||||||||||||||
|
Subroutine
is_success: called CC=2 SCAR=7.2
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 73 | 1 | ▶ | sub is_redirect { my $s = shift->status; $s >= 300 && $s < 400 } | ||||||||||||||||||||||||||||||
|
Subroutine
is_redirect: called CC=2 SCAR=7.2
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 74 | 3 | ▶ | sub is_error { my $s = shift->status; $s >= 400 } | ||||||||||||||||||||||||||||||
|
Subroutine
is_error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | # Exception from app (if trapped) | ||||||||||||||||||||||||||||||||
| 77 | 11 | ▶ | sub exception { shift->{exception} } | ||||||||||||||||||||||||||||||
|
Subroutine
exception: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | # Parse body as JSON | ||||||||||||||||||||||||||||||||
| 80 | 23 | ▶ | sub json { | ||||||||||||||||||||||||||||||
|
Subroutine
json: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 81 | 23 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 82 | 23 | require JSON::MaybeXS; | |||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | 23 | my $body = $self->{body}; | |||||||||||||||||||||||||||||||
| 85 | 23 | my $data = eval { JSON::MaybeXS::decode_json($body) }; | |||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | 23 | ▶ | if (my $err = $@) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 88 | 1 | my $status = $self->status; | |||||||||||||||||||||||||||||||
| 89 | 1 | ▶ | my $ct = $self->content_type // '(none)'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->content_type // "(none)"
MC/DC0%0/1
$self->content_type // "(none)"
$self->content_type
Truth table50%1/2
A
A= $self->content_type
| |||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | # Truncate body preview if too long | ||||||||||||||||||||||||||||||||
| 92 | 1 | ▶ | my $preview = defined $body ? $body : '(undef)'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 93 | 1 | ▶ | if (length($preview) > $JSON_ERROR_BODY_LIMIT) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | 0 | $preview = substr($preview, 0, $JSON_ERROR_BODY_LIMIT) . "\n... [truncated, " . length($body) . " bytes total]"; | |||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 1 | $err =~ s/\s+at \S+ line \d+\.?\s*$//; # Strip file/line from JSON error | |||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 1 | croak "Response body is not valid JSON (status=$status, content-type=$ct)\n" | |||||||||||||||||||||||||||||||
| 100 | . "Body: $preview\n" | ||||||||||||||||||||||||||||||||
| 101 | . "JSON error: $err"; | ||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | 22 | return $data; | |||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | # Convenience header shortcuts | ||||||||||||||||||||||||||||||||
| 108 | 36 | ▶ | sub content_type { shift->header('content-type') } | ||||||||||||||||||||||||||||||
|
Subroutine
content_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 109 | 1 | ▶ | sub content_length { shift->header('content-length') } | ||||||||||||||||||||||||||||||
|
Subroutine
content_length: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 110 | 1 | ▶ | sub location { shift->header('location') } | ||||||||||||||||||||||||||||||
|
Subroutine
location: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | 14 | 1; | |||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | __END__ | ||||||||||||||||||||||||||||||||