blib/lib/PAGI/Test/Response.pm

filter: Statementstmt 88.8% 56 / 63 Branchbran 77.7% 14 / 18 Conditioncond 76.9% 10 / 13 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 94.4% 17 / 18 Podpod 100% 14 / 14 totaltotal 86.4% 115 / 133 CC 18 SCAR 11.1
CC
18
Cov
85%
headers
6.0
json
5.2
new
4.0
CRAP 3.0
« blib/lib/PAGI/Context/HTTP.pm ↑ Summary lib/PAGI/Middleware/Head.pm »
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
l!l
860
$args{"headers"} // []
l!l
788
$args{"body"} // ""
l!l
815
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"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"headers"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"body"}
Aresult
11
00
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
BranchTrueFalseTotal
unless defined $body and length $body 1 34 35
Condition66%2/3
defined $body and length $body
!ll&&!rl&&r
0134
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
ABresult
0X0
100
111
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"
l!l
430
MC/DC100%1/1
$self->_extract_charset // "UTF-8"
$self->_extract_charset
Truth table100%2/2
A
A= $self->_extract_charset
Aresult
11
00
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
BranchTrueFalseTotal
my $ct = $self->content_type or return undef 31 3 34
43  
44 # Match charset=... (with or without quotes)
45 31 ▶ if ($ct =~ /charset\s*=\s*"?([^";,\s]+)"?/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct =~ /charset\s*=\s*"?([^";,\s]+)"?/i) 4 27 31
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
BranchTrueFalseTotal
if lc $pair->[0] eq $name 39 4 43
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
BranchTrueFalseTotal
$s >= 200 and $s < 300 4 0 4
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
BranchTrueFalseTotal
$s >= 300 and $s < 400 1 0 1
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
BranchTrueFalseTotal
if (my $err = $@) 1 22 23
88 1 my $status = $self->status;
89 1 ▶ my $ct = $self->content_type // '(none)';
Condition50%1/2
$self->content_type // "(none)"
l!l
01
MC/DC0%0/1
$self->content_type // "(none)"
$self->content_type
Truth table50%1/2
A
A= $self->content_type
Aresult
11
00
90  
91 # Truncate body preview if too long
92 1 ▶ my $preview = defined $body ? $body : '(undef)';
Branch: 1 branch
BranchTrueFalseTotal
defined $body ? : 1 0 1
93 1 ▶ if (length($preview) > $JSON_ERROR_BODY_LIMIT) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $preview > $PAGI::Test::Response::JSON_ERROR_BODY_LIMIT) 0 1 1
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__
« blib/lib/PAGI/Context/HTTP.pm ↑ Summary lib/PAGI/Middleware/Head.pm »