| 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
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 15 | return bless { | ||||||||||||||||||||
| 16 | port => $params{port} || undef | ||||||||||||||||||||
| 17 | 2 | ▶ | }, $class; | ||||||||||||||||||
|
Condition100%2/2
$params{'port'} || undef
MC/DC100%1/1
$params{'port'} || undef
$params{'port'}
Truth table100%2/2
A
A= $params{'port'}
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 23 | 2 | ▶ | my $proto = getprotobyname("tcp") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 24 | or die $!; | ||||||||||||||||||||
| 25 | 0 | ▶ | socket my $s, PF_INET, SOCK_STREAM, $proto | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 26 | or die $!; | ||||||||||||||||||||
| 27 | 0 | ▶ | setsockopt($s,SOL_SOCKET,SO_REUSEADDR,1) or die "setsockopt: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 31 | { | ||||||||||||||||||||
| 32 | 0 | my $addr = sockaddr_in($self->{port}, $host); | |||||||||||||||||||
| 33 | 0 | ▶ | bind($s,$addr) or die $!; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | 0 | ▶ | listen($s, 10) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 44 | or next; | ||||||||||||||||||||
| 45 | 0 | ▶ | listen($s, 10) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 113 | 0 | my $p = $parser->add($buf); | |||||||||||||||||||
| 114 | 0 | ▶ | if ($p == 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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__ | ||||||||||||||||||||