lib/Kubernetes/REST/LWPIO.pm

filter: Statementstmt 92.4% 73 / 79 Branchbran 76.3% 29 / 38 Conditioncond 67.2% 37 / 55 MC/DCmcdc 42.3% 11 / 26 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 77.1% 162 / 210 CC 40 SCAR 32.5
CC
40
Cov
81%
_response
38.5
__ANON__
13.7
__ANON__
3.0
CRAP 25.7
« lib/Kubernetes/REST/CLI.pm ↑ Summary lib/Kubernetes/REST/CLI/Watch.pm »
1 package Kubernetes::REST::LWPIO;
2 91 our $VERSION = '1.110';
3 # ABSTRACT: HTTP client using LWP::UserAgent
4 91 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 91 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 91 ▶use Kubernetes::REST::HTTPResponse;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 91 ▶use Types::Standard qw/Bool/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 91 with 'Kubernetes::REST::Role::IO';
10  
11  
12 91 has ssl_verify_server => (is => 'ro', isa => Bool, default => 1);
13  
14  
15 91 has ssl_cert_file => (is => 'ro');
16  
17  
18 91 has ssl_cert_pem => (is => 'ro');
19  
20  
21 91 has ssl_key_file => (is => 'ro');
22  
23  
24 91 has ssl_key_pem => (is => 'ro');
25  
26  
27 91 has ssl_ca_file => (is => 'ro');
28  
29  
30 91 has ssl_ca_pem => (is => 'ro');
31  
32  
33 13 ▶has timeout => (is => 'ro', default => sub { 310 });
Subroutine __ANON__: called CC=1 SCAR=0.0
34  
35  
36 5 ▶has ua => (is => 'ro', lazy => 1, default => sub {
Subroutine __ANON__: called CC=9 SCAR=26.2
37 5 my $self = shift;
38 5 require IO::Socket::SSL::Utils;
39  
40 5 my %ssl_opts;
41 5 $ssl_opts{ verify_hostname } = $self->ssl_verify_server;
42 5 ▶ $ssl_opts{ SSL_verify_mode } = $self->ssl_verify_server ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->ssl_verify_server ? : 4 1 5
43  
44 # PEM data: convert to X509/EVP_PKEY objects via SSL_cert/SSL_key
45 # PEM file paths: use SSL_cert_file/SSL_key_file as normal
46 5 ▶ if (defined $self->ssl_cert_pem) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->ssl_cert_pem) { } 0 5 5
elsif (defined $self->ssl_cert_file) { } 0 5 5
47 0 $ssl_opts{ SSL_cert } = [ IO::Socket::SSL::Utils::PEM_string2cert($self->ssl_cert_pem) ];
48 } elsif (defined $self->ssl_cert_file) {
49 0 $ssl_opts{ SSL_cert_file } = $self->ssl_cert_file;
50 }
51  
52 5 ▶ if (defined $self->ssl_key_pem) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->ssl_key_pem) { } 0 5 5
elsif (defined $self->ssl_key_file) { } 0 5 5
53 0 $ssl_opts{ SSL_key } = IO::Socket::SSL::Utils::PEM_string2key($self->ssl_key_pem);
54 } elsif (defined $self->ssl_key_file) {
55 0 $ssl_opts{ SSL_key_file } = $self->ssl_key_file;
56 }
57  
58 5 ▶ if (defined $self->ssl_ca_pem) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->ssl_ca_pem) { } 0 5 5
elsif (defined $self->ssl_ca_file) { } 0 5 5
59 0 $ssl_opts{ SSL_ca } = [ IO::Socket::SSL::Utils::PEM_string2cert($self->ssl_ca_pem) ];
60 } elsif (defined $self->ssl_ca_file) {
61 0 $ssl_opts{ SSL_ca_file } = $self->ssl_ca_file;
62 }
63  
64 5 ▶ return LWP::UserAgent->new(
Condition100%2/2
$Kubernetes::REST::VERSION // "dev"
l!l
23
MC/DC100%1/1
$Kubernetes::REST::VERSION // "dev"
$Kubernetes::REST::VERSION
Truth table100%2/2
A
A= $Kubernetes::REST::VERSION
Aresult
11
00
65 agent => 'Kubernetes::REST Perl Client ' . ($Kubernetes::REST::VERSION // 'dev'),
66 timeout => $self->timeout,
67 ssl_opts => \%ssl_opts,
68 );
69 91 });
70  
71  
72 9 ▶sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod documented
73 9 my ($self, $req) = @_;
74  
75  
76 my $http_req = HTTP::Request->new(
77 $req->method,
78 $req->url,
79 9 [ %{$req->headers} ],
80 $req->content,
81 );
82  
83 9 my $res = $self->ua->request($http_req);
84  
85 9 return $self->_response($res, $req->method);
86 }
87  
88 7 ▶sub call_streaming {
Subroutine call_streaming: called CC=2 SCAR=6.9
Pod documented
89 7 my ($self, $req, $data_callback) = @_;
90  
91  
92 my $http_req = HTTP::Request->new(
93 $req->method,
94 $req->url,
95 7 [ %{$req->headers} ],
96 );
97  
98 # LWP catches a die in the content callback, aborts the transfer and only
99 # notes it in X-Died / Client-Aborted. Keep the exception, die on to stop
100 # the transfer, and rethrow it untouched once the request is over.
101 7 my ( $failed, $error );
102 7 my $received = 0;
103 6 ▶ my $res = $self->ua->request($http_req, sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
104 6 my ($chunk) = @_;
105 6 ▶ return if $failed;
Branch: 1 branch
BranchTrueFalseTotal
if $failed 0 6 6
106 6 $received += length $chunk;
107 6 ▶ unless ( eval { $data_callback->($chunk); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { &$data_callback($chunk); 1 } }) 2 4 6
108 2 ( $failed, $error ) = ( 1, $@ );
109 2 die $error;
110 }
111 7 });
112  
113 7 ▶ die $error if $failed;
Branch: 1 branch
BranchTrueFalseTotal
if $failed 2 5 7
114  
115 5 return $self->_response($res, $req->method, $received);
116 }
117  
118 # charset => 'none' undoes Content-Encoding (gzip et al) but leaves the charset
119 # alone, so the body stays bytes - which is what Kubernetes::REST and IO::K8s
120 # expect, and what HTTPTinyIO hands back. Plain decoded_content() would decode
121 # UTF-8 here and IO::K8s would then decode the characters a second time.
122 14 ▶sub _response {
Subroutine _response: called CC=29 SCAR=36.5
123 14 my ($self, $res, $method, $received) = @_;
124  
125 # LWP reports its own failures (cannot connect, TLS module missing,
126 # unsupported scheme) as ordinary-looking responses; a transfer cut off
127 # mid-body is a 200 with Client-Aborted/X-Died. Neither is an answer from
128 # the API server: map both to 599 with the message as content, as
129 # HTTPTinyIO does.
130 # (A response object without ->header, such as a test double, has no
131 # LWP markers.)
132 14 my $has_headers = $res->can('header');
133 14 ▶ my $internal = $has_headers && ( $res->header('Client-Warning') // '' ) eq 'Internal response';
Condition100%5/5
$has_headers && ($res->header('Client-Warning') // '') eq 'Internal response'
!ll&&!rl&&r
653
$res->header("Client-Warning") // ""
l!l
35
MC/DC100%2/2
$has_headers && ($res->header('Client-Warning') // '') eq 'Internal response'
$has_headers $res->header("Client-Warning")
Truth table100%3/3
A && B
A= $has_headersB= $res->header("Client-Warning")
ABresult
0X0
100
111
134 14 ▶ my $aborted = $has_headers && ( defined $res->header('Client-Aborted') || defined $res->header('X-Died') );
Condition50%3/6
$has_headers && (defined $res->header('Client-Aborted') || defined $res->header('X-Died'))
!ll&&!rl&&r
680
defined $res->header("Client-Aborted") or defined $res->header("X-Died")
l!l&&r!l&&!r
008
MC/DC0%0/3
$has_headers && (defined $res->header('Client-Aborted') || defined $res->header('X-Died'))
$has_headers defined $res->header("Client-Aborted") defined $res->header("X-Died")
Truth table50%2/4
A && B or C
A= $has_headersB= defined $res->header("Client-Aborted")C= defined $res->header("X-Died")
ABCresult
0XX0
1000
11X1
1011
135  
136 # LWP does not flag a body shorter than its Content-Length; compare the
137 # bytes received on the wire (before Content-Encoding is undone).
138 14 my $truncated;
139 # (LWP keeps an error body in the response instead of feeding the callback.)
140 14 ▶ if ($has_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_headers) 8 6 14
141 8 ▶ my $kept = length( $res->content // '' );
Condition50%1/2
$res->content // ""
l!l
80
MC/DC0%0/1
$res->content // ""
$res->content
Truth table50%1/2
A
A= $res->content
Aresult
11
00
142 8 ▶ $received = $kept if !defined $received || $kept > $received;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $received or $kept > $received 6 2 8
Condition100%3/3
not defined $received or $kept > $received
l!l&&r!l&&!r
422
MC/DC100%2/2
not defined $received or $kept > $received
not defined $received $kept > $received
Truth table100%3/3
not(A) or B
A= not defined $receivedB= $kept > $received
ABresult
1X1
011
000
143 }
144 14 ▶ if ( $has_headers && !$internal && !$aborted && ( $method // '' ) ne 'HEAD'
Branch: 1 branch
BranchTrueFalseTotal
if ($has_headers and not $internal and not $aborted and ($method // "") ne "HEAD" and defined $received and not $res->header("Transfer-Encoding")) 5 9 14
Condition70%12/17
$has_headers and not $internal and not $aborted and ($method // "") ne "HEAD" and defined $received and not $res->header("Transfer-Encoding")
!ll&&!rl&&r
905
$has_headers and not $internal and not $aborted and ($method // "") ne "HEAD" and defined $received
!ll&&!rl&&r
905
$has_headers and not $internal and not $aborted and ($method // "") ne "HEAD"
!ll&&!rl&&r
905
$has_headers and not $internal and not $aborted
!ll&&!rl&&r
905
$has_headers and not $internal
!ll&&!rl&&r
635
$method // ""
l!l
50
MC/DC33%2/6
$has_headers and not $internal and not $aborted and ($method // "") ne "HEAD" and defined $received and not $res->header("Transfer-Encoding")
$has_headers not $internal not $aborted $method defined $received not $res->header("Transfer-Encoding")
Truth table42%3/7
A and not(B) and not(C) and D and E and not(F)
A= $has_headersB= not $internalC= not $abortedD= $methodE= defined $receivedF= not $res->header("Transfer-Encoding")
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
145 && defined $received && !$res->header('Transfer-Encoding') ) {
146 5 my $expected = $res->header('Content-Length');
147 5 ▶ $truncated = 1 if defined $expected && $expected =~ /\A\d+\z/ && $received < $expected;
Branch: 1 branch
BranchTrueFalseTotal
if defined $expected and $expected =~ /\A\d+\z/ and $received < $expected 2 3 5
Condition50%3/6
defined $expected and $expected =~ /\A\d+\z/ and $received < $expected
!ll&&!rl&&r
032
defined $expected and $expected =~ /\A\d+\z/
!ll&&!rl&&r
005
MC/DC33%1/3
defined $expected and $expected =~ /\A\d+\z/ and $received < $expected
defined $expected $expected =~ /\A\d+\z/ $received < $expected
Truth table50%2/4
A and B and C
A= defined $expectedB= $expected =~ /\A\d+\z/C= $received < $expected
ABCresult
0XX0
10X0
1100
1111
148 }
149  
150 14 ▶ if ( $internal || $aborted || $truncated ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($internal or $aborted or $truncated) 5 9 14
Condition83%5/6
$internal or $aborted or $truncated
l!l&&r!l&&!r
329
$internal or $aborted
l!l&&r!l&&!r
3011
MC/DC66%2/3
$internal or $aborted or $truncated
$internal $aborted $truncated
Truth table75%3/4
A or B or C
A= $internalB= $abortedC= $truncated
ABCresult
1XX1
01X1
0011
0000
151 5 ▶ my $message = $internal
Branch: 2 branches
BranchTrueFalseTotal
$internal ? : 3 2 5
$truncated ? : 2 0 2
152 ? $res->decoded_content(charset => 'none')
153 : $truncated ? 'connection closed before the whole response body was received'
154 : $res->header('X-Died');
155 5 ▶ $message = $res->header('Client-Aborted') // 'transport error'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message and length $message 0 5 5
Condition20%1/5
defined $message and length $message
!ll&&!rl&&r
005
$res->header("Client-Aborted") // "transport error"
l!l
00
MC/DC0%0/2
defined $message and length $message
defined $message length $message
MC/DC0%0/1
$res->header("Client-Aborted") // "transport error"
$res->header("Client-Aborted")
Truth table33%1/3
A and B
A= defined $messageB= length $message
ABresult
0X0
100
111
Truth table0%0/2
A
A= $res->header("Client-Aborted")
Aresult
11
00
156 unless defined $message && length $message;
157 5 chomp $message;
158 5 return Kubernetes::REST::HTTPResponse->new(
159 status => 599,
160 content => $message,
161 );
162 }
163  
164 9 my $content = $res->decoded_content(charset => 'none');
165  
166 9 ▶ return Kubernetes::REST::HTTPResponse->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $content && length $content ? : 6 3 9
Condition66%2/3
defined $content and length $content
!ll&&!rl&&r
036
MC/DC50%1/2
defined $content and length $content
defined $content length $content
Truth table66%2/3
A and B
A= defined $contentB= length $content
ABresult
0X0
100
111
167 status => $res->code,
168 (defined $content && length $content) ? ( content => $content ) : (),
169 );
170 }
171  
172 91 1;
173  
174 __END__
« lib/Kubernetes/REST/CLI.pm ↑ Summary lib/Kubernetes/REST/CLI/Watch.pm »