Once.pm

filter: Statementstmt 32.1% 37 / 115 Branchbran 6.2% 2 / 32 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 70.5% 12 / 17 Podpod 100% 6 / 6 totaltotal 34.2% 61 / 178 CC 19 SCAR 33.4
CC
19
Cov
28%
bind_mock_server
77.0
__ANON__
30.0
start_mock_server
12.0
CRAP 28.3
« Once/REST.pm ↑ Summary
1 package Test::HTTP::MockServer::Once;
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 HTTP::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Storable qw(freeze);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '0.0.2';
11  
12 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
13 2 my ($class, %params) = @_;
14 2 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
15 return bless {
16 port => $params{port} || undef
17 2 ▶ }, $class;
Condition100%2/2
$params{'port'} || undef
l!l
11
MC/DC100%1/1
$params{'port'} || undef
$params{'port'}
Truth table100%2/2
A
A= $params{'port'}
Aresult
11
00
18 }
19  
20 2 ▶sub bind_mock_server {
Subroutine bind_mock_server: called CC=10 SCAR=43.4
Pod documented
21 2 my $self = shift;
22 2 ▶ if (!$self->{socket}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"socket"}) 2 0 2
23 2 ▶ my $proto = getprotobyname("tcp")
Branch: 1 branch
BranchTrueFalseTotal
my $proto = getprotobyname "tcp" or die $! 0 2 2
24 or die $!;
25 0 ▶ socket my $s, PF_INET, SOCK_STREAM, $proto
Branch: 1 branch
BranchTrueFalseTotal
socket my $s, 2, 1, $proto or die $! 0 0 0
26 or die $!;
27 0 ▶ setsockopt($s,SOL_SOCKET,SO_REUSEADDR,1) or die "setsockopt: $!";
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $s, 1, 2, 1 or die "setsockopt: $!" 0 0 0
28 0 my $host_s = '127.0.0.1';
29 0 my $host = inet_aton($host_s);
30 0 ▶ if(defined($self->{port}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'port'}) { } 0 0 0
31 {
32 0 my $addr = sockaddr_in($self->{port}, $host);
33 0 ▶ bind($s,$addr) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
bind $s, $addr or die $! 0 0 0
34 0 ▶ listen($s, 10)
Branch: 1 branch
BranchTrueFalseTotal
listen $s, 10 or die $! 0 0 0
35 or die $!;
36 }
37 else
38 {
39 0 my $random_port;
40 0 while (1) {
41 0 $random_port = int(rand(5000))+10000;
42 0 my $addr = sockaddr_in($random_port, $host);
43 0 ▶ bind($s,$addr)
Branch: 1 branch
BranchTrueFalseTotal
bind $s, $addr or next 0 0 0
44 or next;
45 0 ▶ listen($s, 10)
Branch: 1 branch
BranchTrueFalseTotal
listen $s, 10 or die $! 0 0 0
46 or die $!;
47 0 last;
48 }
49 0 $self->{port} = $random_port;
50 }
51 0 $self->{host} = $host_s;
52 0 $self->{socket} = $s;
53 }
54 0 return 1;
55 }
56  
57 1 ▶sub host {
Subroutine host: called CC=1 SCAR=0.4
Pod documented
58 1 my $self = shift;
59 1 $self->bind_mock_server;
60 0 return $self->{host};
61 }
62  
63 1 ▶sub port {
Subroutine port: called CC=1 SCAR=0.4
Pod documented
64 1 my $self = shift;
65 1 $self->bind_mock_server;
66 0 return $self->{port};
67 }
68  
69 1 ▶sub base_url {
Subroutine base_url: called CC=1 SCAR=1.2
Pod documented
70 1 my $self = shift;
71 1 my $host = $self->host;
72 0 my $port = $self->port;
73 0 return "http://$host:$port";
74 }
75  
76 0 ▶my $request_handle = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
77 0 my $self = shift;
78 0 my $rp = shift;
79 0 my $request = shift;
80 0 my $response;
81 0 eval {
82 0 $response = HTTP::Response->new(200, 'OK');
83 0 $response->header('Content-type' => 'text/plain');
84 0 $rp->($request, $response);
85 };
86 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
87 0 my $err = $@;
88 0 return HTTP::Response->new(
89 500, 'Internal Server Error',
90 HTTP::Headers->new("Content-type" => "text/plain"),
91 $err
92 );
93 } else {
94 0 return $response;
95 }
96 2 };
97  
98 0 ▶my $client_handle = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
99 0 my $self = shift;
100 0 my $rp = shift;
101 0 my $client = shift;
102 0 my $parser = HTTP::Parser->new(request => 1);
103  
104 0 my $ret;
105 0 while (1) {
106 0 my $buf;
107 # read a byte at a time so we can do a blocking read instead of
108 # having to manage a non-blocking read loop.
109 0 my $r = read $client, $buf, 1;
110 0 my $request;
111 my $closed;
112 0 ▶ if (defined $r) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r) { } 0 0 0
113 0 my $p = $parser->add($buf);
114 0 ▶ if ($p == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p == 0) 0 0 0
115 0 $request = $parser->object;
116 }
117 0 $closed = 0;
118 } else {
119 0 $request = $parser->object;
120 0 $closed = 1;
121 }
122 0 ▶ if ($request) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request) 0 0 0
123 0 my $copy = $request;
124 0 $ret->{request} = $request;
125 0 $request = undef;
126 0 my $response = $request_handle->($self, $rp, $copy);
127 0 $response->header('Content-length' => length($response->content));
128 0 $ret->{response} = $response;
129 0 my $strout = "HTTP/1.1 ".($response->as_string("\015\012"));
130 0 $client->print($strout);
131 # we don't support keep-alive
132 0 $closed = 1;
133 }
134 0 ▶ last if $closed;
Branch: 1 branch
BranchTrueFalseTotal
if $closed 0 0 0
135 }
136 0 return $ret;
137 2 };
138  
139 0 ▶sub start_mock_server {
Subroutine start_mock_server: not called CC=3 SCAR=24.8
Pod documented
140 0 my $self = shift;
141 0 ▶ my $rp = shift or die "No request processor";
Branch: 1 branch
BranchTrueFalseTotal
my $rp = shift() or die "No request processor" 0 0 0
142  
143 0 $self->bind_mock_server();
144  
145 0 $DB::signal = 1;
146 0 ▶ $SIG{INT} = sub { exit 1; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 0 ▶ $SIG{TERM} = sub { exit 1; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
148 accept my $client, $self->{socket}
149 0 ▶ or die "Failed to accept new connections: $!";
Branch: 1 branch
BranchTrueFalseTotal
accept my $client, $self->{"socket"} or die "Failed to accept new connections: $!" 0 0 0
150 0 my $interaction;
151 0 eval {
152 0 $interaction = $client_handle->($self, $rp, $client);
153 };
154 0 close $client;
155 0 close $self->{socket};
156 0 return freeze $interaction;
157 }
158  
159 2 1;
160  
161 __END__
« Once/REST.pm ↑ Summary