blib/lib/Plack/Middleware/FixMissingBodyInRedirect.pm

filter: Statementstmt 100% 54 / 54 Branchbran 85.0% 17 / 20 Conditioncond 83.3% 15 / 18 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 92.1% 106 / 115 CC 17 SCAR 16.6
CC
17
Cov
93%
__ANON__
9.1
_is_body_set
6.0
__ANON__
4.0
CRAP 5.3
↑ Summary
1 package Plack::Middleware::FixMissingBodyInRedirect;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use parent qw( Plack::Middleware );
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 # ABSTRACT: Plack::Middleware which sets body for redirect response, if it's not already set
10  
11 1 our $VERSION = '0.12';
12  
13 14 ▶sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod documented
14 14 my $self = shift;
15  
16 14 ▶ return $self->response_cb($self->app->(@_), sub {
Subroutine __ANON__: called CC=9 SCAR=22.1
17 14 my $res = shift;
18 14 ▶ return unless $res->[0] >= 300 && $res->[0] < 400;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] >= 300 and $res->[0] < 400 0 14 14
Condition33%1/3
$res->[0] >= 300 and $res->[0] < 400
!ll&&!rl&&r
0014
MC/DC0%0/2
$res->[0] >= 300 and $res->[0] < 400
$res->[0] >= 300 $res->[0] < 400
Truth table33%1/3
A and B
A= $res->[0] >= 300B= $res->[0] < 400
ABresult
0X0
100
111
19 14 my $headers = Plack::Util::headers($res->[1]); # first index contains HTTP header
20 14 ▶ if( $headers->exists('Location') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers->exists("Location")) 14 0 14
21 14 my $location = $headers->get("Location");
22 # checking if body (which is at index 2) is set or not
23 14 ▶ if (@$res == 3 && !_is_body_set($res->[2])) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$res == 3 and not _is_body_set($res->[2])) { } 5 9 14
elsif (@$res == 2 or builtin::blessed($res->[2])) { } 4 5 9
Condition100%3/3
@$res == 3 and not _is_body_set($res->[2])
!ll&&!rl&&r
275
MC/DC100%2/2
@$res == 3 and not _is_body_set($res->[2])
@$res == 3 not _is_body_set($res->[2])
Truth table100%3/3
A and not(B)
A= @$res == 3B= not _is_body_set($res->[2])
ABresult
0X0
100
111
24 5 my $body = $self->_default_html_body($location);
25 5 $res->[2] = [$body];
26 5 my $content_length = Plack::Util::content_length([$body]);
27 5 $headers->set('Content-Length' => $content_length);
28 5 $headers->set('Content-Type' => 'text/html; charset=utf-8');
29 5 return;
30 }
31 ▶ elsif (@$res == 2 || blessed($res->[2])) {
Condition100%3/3
@$res == 2 or builtin::blessed($res->[2])
l!l&&r!l&&!r
225
MC/DC100%2/2
@$res == 2 or builtin::blessed($res->[2])
@$res == 2 builtin::blessed($res->[2])
Truth table100%3/3
A or B
A= @$res == 2B= builtin::blessed($res->[2])
ABresult
1X1
011
000
32 4 ▶ if(! $headers->exists('Content-Type')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers->exists("Content-Type")) 2 2 4
33 2 $headers->set('Content-Type' => 'text/html; charset=utf-8')
34 }
35 4 my $done;
36 8 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
37 8 my $chunk = shift;
38 8 ▶ return $chunk if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 4 4 8
39 4 ▶ if (!defined $chunk) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $chunk) { } 2 2 4
elsif (length $chunk) { } 2 0 2
40 2 $done = 1;
41 2 return $self->_default_html_body($location);
42 }
43 elsif (length $chunk) {
44 2 $done = 1;
45 }
46 2 return $chunk;
47 4 };
48 }
49 }
50 14 });
51 }
52  
53 7 ▶sub _default_html_body {
Subroutine _default_html_body: called CC=1 SCAR=0.0
54 7 my ($self_or_class, $location) = @_;
55 7 my $encoded_location = encode_entities($location);
56 7 return <<"EOF";
57 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
58 <html xmlns="http://www.w3.org/1999/xhtml">
59 <head>
60 <title>Moved</title>
61 </head>
62 <body>
63 <p>This item has moved <a href="$encoded_location">here</a>.</p>
64 </body>
65 </html>
66 EOF
67 }
68  
69 12 ▶sub _is_body_set {
Subroutine _is_body_set: called CC=6 SCAR=17.9
70 12 my $body = shift;
71 12 ▶ if (ref $body eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $body eq 'ARRAY') { } 8 4 12
elsif (&Plack::Util::is_real_fh($body) and -f $body and -z _) { } 1 5 6
72 8 ▶ return grep { defined && length } @$body;
Condition100%3/3
defined $_ && length $_
!ll&&!rl&&r
254
MC/DC100%2/2
defined $_ && length $_
defined $_ length $_
Truth table100%3/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
73 }
74 ▶ elsif (Plack::Util::is_real_fh($body) && -f $body && -z _) {
Condition83%5/6
&Plack::Util::is_real_fh($body) and -f $body and -z _
!ll&&!rl&&r
211
&Plack::Util::is_real_fh($body) and -f $body
!ll&&!rl&&r
202
MC/DC66%2/3
&Plack::Util::is_real_fh($body) and -f $body and -z _
&Plack::Util::is_real_fh($body) -f $body -z _
Truth table75%3/4
A and B and C
A= &Plack::Util::is_real_fh($body)B= -f $bodyC= -z _
ABCresult
0XX0
10X0
1100
1111
75 1 return 0;
76 }
77 3 return 1;
78 }
79  
80 1 1;
81  
82 __END__
↑ Summary