blib/lib/Kossy/Response.pm

filter: Statementstmt 87.1% 54 / 62 Branchbran 65.7% 25 / 38 Conditioncond 53.3% 8 / 15 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 74.4% 102 / 137 CC 25 SCAR 20.5
CC
25
Cov
76%
headers
18.3
new
8.4
_body
7.6
CRAP 7.8
« blib/lib/Kossy/Connection.pm ↑ Summary blib/lib/Kossy.pm »
1 package Kossy::Response;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use parent qw/Plack::Response/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use HTTP::Headers::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Cookie::Baker;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 our $VERSION = '0.50';
11  
12 11 our $DIRECT;
13 11 our $SECURITY_HEADER = 1;
14  
15 sub new {
16 34 ▶ my ($class, $rc, $headers, $content) = @_;
Subroutine new: called CC=8 SCAR=21.3
Pod documented
17 34 ▶ if ( defined $headers ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $headers) 30 4 34
18 30 ▶ if (ref $headers eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $headers eq 'ARRAY') { } 12 18 30
elsif (ref $headers eq 'HASH') { } 0 18 18
19 12 ▶ Carp::carp("Odd number of headers") if @$headers % 2 != 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$headers % 2 != 0 0 12 12
20 12 $headers = HTTP::Headers::Fast->new(@$headers);
21 } elsif (ref $headers eq 'HASH') {
22 0 $headers = HTTP::Headers::Fast->new(%$headers);
23 }
24 }
25 bless {
26 34 ▶ defined $rc ? ( status => $rc ) : (),
Branch: 3 branches
BranchTrueFalseTotal
defined $rc ? : 34 0 34
defined $content ? : 12 22 34
defined $headers ? : 30 4 34
27 defined $content ? ( body => $content ) : (),
28 defined $headers ? ( headers => $headers ) : (),
29 }, $class;
30 }
31  
32 sub headers {
33 30 ▶ my $self = shift;
Subroutine headers: called CC=6 SCAR=29.1
Pod documented
34  
35 30 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 30 30
36 0 my $headers = shift;
37 0 ▶ if (ref $headers eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $headers eq 'ARRAY') { } 0 0 0
elsif (ref $headers eq 'HASH') { } 0 0 0
38 0 ▶ Carp::carp("Odd number of headers") if @$headers % 2 != 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$headers % 2 != 0 0 0 0
39 0 $headers = HTTP::Headers::Fast->new(@$headers);
40 } elsif (ref $headers eq 'HASH') {
41 0 $headers = HTTP::Headers::Fast->new(%$headers);
42 }
43 0 return $self->{headers} = $headers;
44 } else {
45 30 ▶ return $self->{headers} ||= HTTP::Headers::Fast->new();
Condition66%2/3
$self->{"headers"} ||= "HTTP::Headers::Fast"->new
l!l&&r!l&&!r
2640
MC/DC0%0/2
$self->{"headers"} ||= "HTTP::Headers::Fast"->new
$self->{"headers"} "HTTP::Headers::Fast"->new
Truth table66%2/3
A ||= B
A= $self->{"headers"}B= "HTTP::Headers::Fast"->new
ABresult
1X1
011
000
46 }
47 }
48  
49 sub _body {
50 29 ▶ my $self = shift;
Subroutine _body: called CC=7 SCAR=20.2
51 29 my $body = $self->body;
52 29 ▶ $body = [] unless defined $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 4 25 29
53 29 ▶ if (!ref $body or Scalar::Util::blessed($body) && overload::Method($body, q("")) && !$body->can('getline')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $body or builtin::blessed($body) and overload::Method($body, '""') and not $body->can('getline')) { } 14 15 29
Condition44%4/9
not ref $body or builtin::blessed($body) and overload::Method($body, "\"\"") and not $body->can("getline")
l!l&&r!l&&!r
14015
builtin::blessed($body) and overload::Method($body, "\"\"") and not $body->can("getline")
!ll&&!rl&&r
1500
builtin::blessed($body) and overload::Method($body, "\"\"")
!ll&&!rl&&r
1500
MC/DC25%1/4
not ref $body or builtin::blessed($body) and overload::Method($body, "\"\"") and not $body->can("getline")
not ref $body builtin::blessed($body) overload::Method($body, "\"\"") not $body->can("getline")
Truth table40%2/5
not(A) or B and C and not(D)
A= not ref $bodyB= builtin::blessed($body)C= overload::Method($body, "\"\"")D= not $body->can("getline")
ABCDresult
1XXX1
01111
00XX0
010X0
01100
54 14 ▶ return [ Encode::encode_utf8($body) ] if Encode::is_utf8($body);
Branch: 1 branch
BranchTrueFalseTotal
if &Encode::is_utf8($body) 5 9 14
55 9 return [ $body ];
56 } else {
57 15 return $body;
58 }
59 }
60  
61 sub finalize {
62 29 ▶ my $self = shift;
Subroutine finalize: called CC=5 SCAR=16.3
Pod documented
63 29 ▶ return $DIRECT if $DIRECT;
Branch: 1 branch
BranchTrueFalseTotal
if $Kossy::Response::DIRECT 0 29 29
64 29 ▶ Carp::croak "missing status" unless $self->status();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status 0 29 29
65  
66 29 my @headers;
67 $self->headers->scan(sub{
68 38 ▶ my ($k,$v) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.6
69 38 ▶ return if $SECURITY_HEADER && $k eq 'X-XSS-Protection';
Branch: 1 branch
BranchTrueFalseTotal
if $Kossy::Response::SECURITY_HEADER and $k eq "X-XSS-Protection" 0 38 38
Condition66%2/3
$Kossy::Response::SECURITY_HEADER and $k eq "X-XSS-Protection"
!ll&&!rl&&r
9290
MC/DC0%0/2
$Kossy::Response::SECURITY_HEADER and $k eq "X-XSS-Protection"
$Kossy::Response::SECURITY_HEADER $k eq "X-XSS-Protection"
Truth table66%2/3
A and B
A= $Kossy::Response::SECURITY_HEADERB= $k eq "X-XSS-Protection"
ABresult
0X0
100
111
70 38 $v =~ s/\015\012[\040|\011]+/chr(32)/ge; # replace LWS with a single SP
71 38 $v =~ s/\015|\012//g; # remove CR and LF since the char is invalid here
72 38 push @headers, $k, $v;
73 29 });
74  
75 29 ▶ while (my($name, $val) = each %{$self->cookies}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->cookies;}) 1 29 30
76 1 my $cookie = bake_cookie($name, $val);
77 1 push @headers, 'Set-Cookie' => $cookie;
78 }
79  
80 29 ▶ push @headers, 'X-XSS-Protection' => 1 if $SECURITY_HEADER;
Branch: 1 branch
BranchTrueFalseTotal
if $Kossy::Response::SECURITY_HEADER 21 8 29
81  
82 return [
83 29 $self->status,
84 \@headers,
85 $self->_body,
86 ];
87 }
88  
89 11 1;
« blib/lib/Kossy/Connection.pm ↑ Summary blib/lib/Kossy.pm »