blib/lib/Plack/Middleware/JSON.pm

filter: Statementstmt 100% 48 / 48 Branchbran 71.4% 10 / 14 Conditioncond 64.7% 11 / 17 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 83.0% 83 / 100 CC 14 SCAR 20.6
CC
14
Cov
87%
__ANON__
13.6
prepare_app
3.0
call
1.0
CRAP 7.8
↑ Summary
1 package Plack::Middleware::JSON;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use parent qw/Plack::Middleware/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 our $VERSION = 0.01;
8  
9 2 ▶use Plack::Util::Accessor qw/json_key callback_key/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶sub prepare_app {
Subroutine prepare_app: called CC=3 SCAR=11.0
Pod documented
12 4 my $self = shift;
13 4 ▶ unless (defined $self->callback_key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->callback_key) 3 1 4
14 3 $self->callback_key('callback');
15 }
16 4 ▶ if (defined $self->json_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->json_key) { } 2 2 4
17 2 my $json_key = $self->json_key() . '|' . $self->callback_key;
18 2 $self->json_key($json_key);
19 }
20 else {
21 2 $self->json_key($self->callback_key);
22 }
23 }
24  
25 4 ▶sub call {
Subroutine call: called CC=1 SCAR=0.1
Pod documented
26 4 my($self, $env) = @_;
27 4 my $res = $self->app->($env);
28 4 ▶ $self->response_cb($res, sub {
Subroutine __ANON__: called CC=12 SCAR=26.1
29 4 my $res = shift;
30 4 ▶ if (defined $res->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res->[2]) 4 0 4
31 4 my $h = Plack::Util::headers($res->[1]);
32 4 my $json_key = $self->json_key;
33 4 ▶ my $content_type = $h->get('Content-Type') || '';
Condition50%1/2
$h->get('Content-Type') || ''
l!l
40
MC/DC0%0/1
$h->get('Content-Type') || ''
$h->get('Content-Type')
Truth table50%1/2
A
A= $h->get('Content-Type')
Aresult
11
00
34 4 ▶ if (($json_key and $env->{QUERY_STRING} =~ /(?:^|&)($json_key)=([^&]+)/) or $content_type =~ m!/(?:json|javascript)!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json_key and $env->{"QUERY_STRING"} =~ /(?:^|&)($json_key)=([^&]+)/ or $content_type =~ m[/(?:json|javascript)]) 4 0 4
Condition66%4/6
$json_key and $env->{"QUERY_STRING"} =~ /(?:^|&)($json_key)=([^&]+)/ or $content_type =~ m[/(?:json|javascript)]
l!l&&r!l&&!r
310
$json_key and $env->{"QUERY_STRING"} =~ /(?:^|&)($json_key)=([^&]+)/
!ll&&!rl&&r
013
MC/DC0%0/3
$json_key and $env->{"QUERY_STRING"} =~ /(?:^|&)($json_key)=([^&]+)/ or $content_type =~ m[/(?:json|javascript)]
$json_key $env->{"QUERY_STRING"} =~ /(?:^|&)($json_key)=([^&]+)/ $content_type =~ m[/(?:json|javascript)]
Truth table40%2/5
A and B or C
A= $json_keyB= $env->{"QUERY_STRING"} =~ /(?:^|&)($json_key)=([^&]+)/C= $content_type =~ m[/(?:json|javascript)]
ABCresult
11X1
0X11
1011
0X00
1000
35 # json
36 4 ▶ if ((ref $res->[2][0] eq 'ARRAY') or (ref $res->[2][0] eq 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res->[2][0] eq "ARRAY" or ref $res->[2][0] eq "HASH") 4 0 4
Condition33%1/3
ref $res->[2][0] eq "ARRAY" or ref $res->[2][0] eq "HASH"
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $res->[2][0] eq "ARRAY" or ref $res->[2][0] eq "HASH"
ref $res->[2][0] eq "ARRAY" ref $res->[2][0] eq "HASH"
Truth table33%1/3
A or B
A= ref $res->[2][0] eq "ARRAY"B= ref $res->[2][0] eq "HASH"
ABresult
1X1
011
000
37 4 $res->[2] = [ encode_json($res->[2][0]) ];
38 }
39 # jsonp
40 4 ▶ if (defined $self->callback_key and $1 and $1 eq $self->callback_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->callback_key and $1 and $1 eq $self->callback_key) 1 3 4
Condition83%5/6
defined $self->callback_key and $1 and $1 eq $self->callback_key
!ll&&!rl&&r
121
defined $self->callback_key and $1
!ll&&!rl&&r
013
MC/DC66%2/3
defined $self->callback_key and $1 and $1 eq $self->callback_key
defined $self->callback_key $1 $1 eq $self->callback_key
Truth table75%3/4
A and B and C
A= defined $self->callback_keyB= $1C= $1 eq $self->callback_key
ABCresult
0XX0
10X0
1100
1111
41 1 my $cb = URI::Escape::uri_unescape($2);
42 1 ▶ if ($cb =~ /^[\w\.\[\]]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb =~ /^[\w\.\[\]]+$/) 1 0 1
43 1 my $body;
44 1 ▶ Plack::Util::foreach($res->[2], sub { $body .= $_[0] });
Subroutine __ANON__: called CC=1 SCAR=0.0
45 1 my $jsonp = "$cb($body)";
46 1 $res->[2] = [ $jsonp ];
47 }
48 }
49 4 $h->set('Content-Length', length $res->[2][0]);
50 4 $h->set('Content-Type', 'application/json; charset=utf-8');
51 }
52 }
53 4 });
54 }
55  
56 2 1;
57 __END__
↑ Summary