Once/REST.pm

filter: Statementstmt 14.4% 10 / 69 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 30.0% 3 / 10 Podpod 100% 3 / 3 totaltotal 13.9% 16 / 115 CC 13 SCAR 44.6
CC
13
Cov
11%
__ANON__
156.0
new
6.0
_wrapped_rest_call
2.0
CRAP 86.2
↑ Summary Once.pm »
1 package Test::HTTP::MockServer::Once::REST;
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 JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
7 0 my ($class) = shift;
8 0 ▶ $class = ref $class || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
9 0 my %dispatch = @_;
10 0 return bless { d => \%dispatch }, $class;
11 }
12  
13 0 ▶sub _wrapped_rest_call {
Subroutine _wrapped_rest_call: not called CC=1 SCAR=6.9
14 0 my $self = shift;
15 0 my $wrapped = shift;
16 0 ▶ return sub {
Subroutine __ANON__: not called CC=12 SCAR=50.5
17 # this is the coderef invoked by MockServer
18 0 my ($req, $res) = @_;
19 0 my $input_content = $req->content;
20 0 my $input_data;
21 0 my $input_ct = $req->header("Content-type");
22 0 ▶ if ($input_content && $input_ct && $input_ct eq 'application/json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_content and $input_ct and $input_ct eq "application/json") 0 0 0
Condition0%0/6
$input_content and $input_ct and $input_ct eq "application/json"
!ll&&!rl&&r
000
$input_content and $input_ct
!ll&&!rl&&r
000
MC/DC0%0/3
$input_content and $input_ct and $input_ct eq "application/json"
$input_content $input_ct $input_ct eq "application/json"
Truth table0%0/4
A and B and C
A= $input_contentB= $input_ctC= $input_ct eq "application/json"
ABCresult
0XX0
10X0
1100
1111
23 0 eval {
24 0 $input_data = decode_json $req->content;
25 };
26 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
27 0 $res->code(400);
28 0 $res->message('Malformed Request');
29 0 $res->content($@);
30 0 return;
31 }
32 }
33 0 my $selected_key;
34 my @captures;
35 0 my $s = join " ", $req->method(), $req->uri()->canonical()->path();
36 0 foreach my $key (keys %{$self->{d}}) {
37 0 my $rx = $self->{d}{$key};
38 0 ▶ if (@captures = ($s =~ $rx)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@captures = $s =~ /$rx/) 0 0 0
39 0 $selected_key = $key;
40 0 last;
41 }
42 }
43 0 ▶ if (!$selected_key) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selected_key) 0 0 0
44 0 $res->code(404);
45 0 $res->message('Page not found');
46 0 $res->content("$s not found");
47 0 return;
48 }
49  
50 0 my $return_data = $wrapped->(
51 $selected_key, $req, $res, \@captures, $input_data
52 );
53  
54 0 ▶ if ($return_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return_data) 0 0 0
55 0 my $a = $req->header('Accept');
56 0 ▶ if ($a && $a eq 'application/json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($a and $a eq 'application/json') { } 0 0 0
Condition0%0/3
$a and $a eq "application/json"
!ll&&!rl&&r
000
MC/DC0%0/2
$a and $a eq "application/json"
$a $a eq "application/json"
Truth table0%0/3
A and B
A= $aB= $a eq "application/json"
ABresult
0X0
100
111
57 0 eval {
58 0 my $out = encode_json $return_data;
59 0 $res->header('Content-type', 'application/json');
60 0 $res->content($out);
61 };
62 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
63 0 $res->code(500);
64 0 $res->message('Internal Server Error');
65 0 $res->content($@);
66 0 return;
67 }
68 } else {
69 0 $res->code(400);
70 0 $res->message('Malformed Request');
71 0 $res->content("Only support Accept: application/json");
72 0 return;
73 }
74 }
75 0 };
76 }
77  
78 0 ▶sub wrap_object {
Subroutine wrap_object: not called CC=1 SCAR=6.9
Pod documented
79 0 my $self = shift;
80 0 my $object = shift;
81 return $self->_wrapped_rest_call(
82 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 0 my $key = shift;
84 0 return $object->$key(@_);
85 }
86 0 );
87 }
88  
89 0 ▶sub wrap_hash {
Subroutine wrap_hash: not called CC=1 SCAR=6.9
Pod documented
90 0 my $self = shift;
91 0 my $hash = shift;
92 return $self->_wrapped_rest_call(
93 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
94 0 my $key = shift;
95 0 return $hash->{$key}->(@_)
96 }
97 0 );
98 }
99  
100 1 1;
101  
102 __END__
↑ Summary Once.pm »