Twilio/Http/Client.pm

filter: Statementstmt 100% 124 / 124 Branchbran 100% 26 / 26 Conditioncond 86.4% 32 / 37 MC/DCmcdc 64.7% 11 / 17 Subroutinesub 100% 26 / 26 Podpod 100% 7 / 7 totaltotal 95.3% 226 / 237 CC 33 SCAR 16.3
CC
33
Cov
97%
prepare_request
13.1
_build_body
6.0
_multipart
5.0
CRAP 5.1
« Twilio/Base/BaseClient.pm ↑ Summary Twilio/Jwt/JWT.pm »
1 package Twilio::Http::Client;
2  
3 23 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 23 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 23 ▶use Twilio::Json;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 23 our $VERSION = '2.001';
13  
14 23 my $DEFAULT_TIMEOUT = 60;
15  
16 1922 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
17 1922 my ($class, $options) = @_;
18 1922 ▶ return bless { %{ $options // {} } }, $class;
Condition100%2/2
$options // {}
l!l
91913
MC/DC100%1/1
$options // {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
19 }
20  
21 1974 ▶sub request {
Subroutine request: called CC=1 SCAR=0.0
Pod documented
22 1974 my ($self, $method, $url, $options) = @_;
23  
24 1974 my $request = $self->prepare_request($method, $url, $options);
25 1974 $self->{last_request} = $request;
26 1974 $self->{last_response} = undef;
27  
28 1974 my $response = $self->send_request($request);
29 1968 $self->{last_response} = $response;
30 1968 return $response;
31 }
32  
33 1 ▶sub send_request {
Subroutine send_request: called CC=1 SCAR=0.0
Pod documented
34 1 my ($self) = @_;
35 1 croak ref($self) . ' must implement send_request';
36 }
37  
38 1996 ▶sub prepare_request {
Subroutine prepare_request: called CC=13 SCAR=25.7
Pod documented
39 1996 my ($self, $method, $url, $options) = @_;
40 1996 ▶ $options //= {};
Condition100%2/2
$options //= {}
l!l
198016
MC/DC100%1/1
$options //= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
41  
42 # Flex Insights takes a token of its own in Authorization, as an option of the operation, and
43 # that one is sent instead of the account's credentials.
44 1996 ▶ my %headers = %{ $options->{headers} // {} };
Condition100%2/2
$options->{"headers"} // {}
l!l
196333
MC/DC100%1/1
$options->{"headers"} // {}
$options->{"headers"}
Truth table100%2/2
A
A= $options->{"headers"}
Aresult
11
00
45 1996 my $given = grep { lc eq 'authorization' } keys %headers;
46 1996 ▶ if (!$given && length($options->{username} // q{}) && length($options->{password} // q{})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $given and length($options->{"username"} // "") and length($options->{"password"} // "")) 1895 101 1996
Condition80%8/10
not $given and length($options->{"username"} // "") and length($options->{"password"} // "")
!ll&&!rl&&r
10101895
not $given and length($options->{"username"} // "")
!ll&&!rl&&r
48531895
$options->{"username"} // ""
l!l
189553
$options->{"password"} // ""
l!l
18950
MC/DC66%2/3
not $given and length($options->{"username"} // "") and length($options->{"password"} // "")
not $given $options->{"username"} $options->{"password"}
Truth table75%3/4
not(A) and B and C
A= not $givenB= $options->{"username"}C= $options->{"password"}
ABCresult
0XX0
10X0
1100
1111
47 1895 $headers{Authorization} = _basic_auth($options->{username}, $options->{password});
48 }
49  
50 1996 my $query = $self->build_query($options->{params});
51 1996 ▶ if (length $query) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $query) 275 1721 1996
52 275 ▶ $url .= ($url =~ /[?]/ ? q{&} : q{?}) . $query;
Branch: 1 branch
BranchTrueFalseTotal
$url =~ /[?]/u ? : 1 274 275
53 }
54  
55 my %request = (
56 method => uc $method,
57 url => $url,
58 headers => \%headers,
59 1996 ▶ timeout => $options->{timeout} // $self->{timeout} // $DEFAULT_TIMEOUT,
Condition83%5/6
($options->{"timeout"} // $self->{"timeout"}) // $DEFAULT_TIMEOUT
l!l&&r!l&&!r
419920
$options->{"timeout"} // $self->{"timeout"}
l!l&&r!l&&!r
311992
MC/DC0%0/3
($options->{"timeout"} // $self->{"timeout"}) // $DEFAULT_TIMEOUT
$options->{"timeout"} $self->{"timeout"} $DEFAULT_TIMEOUT
Truth table75%3/4
A // B // C
A= $options->{"timeout"}B= $self->{"timeout"}C= $DEFAULT_TIMEOUT
ABCresult
1XX1
01X1
0011
0000
60 );
61  
62 1996 ▶ if ($request{method} =~ /\A(?:POST|PUT|PATCH)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request{"method"} =~ /\A(?:POST|PUT|PATCH)\z/u) 683 1313 1996
63 683 $request{content} = $self->_build_body($options->{data}, \%headers);
64 }
65  
66 1996 return \%request;
67 }
68  
69 2578 ▶sub build_query {
Subroutine build_query: called CC=4 SCAR=13.9
Pod documented
70 2578 my ($self, $params) = @_;
71  
72 2578 my @parts;
73 2578 ▶ for my $key (sort keys %{ $params // {} }) {
Condition100%2/2
$params // {}
l!l
10981480
MC/DC100%1/1
$params // {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
74 6266 my $value = $params->{$key};
75 6266 ▶ my @values = ref $value eq 'ARRAY' ? @{$value} : ($value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 162 6104 6266
76 4280 push @parts, map { _encode_component($key) . q{=} . _encode_component($_) }
77 6266 grep { defined } @values;
78 }
79  
80 2578 return join q{&}, @parts;
81 }
82  
83 65 ▶sub last_request { return $_[0]{last_request} }
Subroutine last_request: called CC=1 SCAR=0.0
Pod documented
84 2 ▶sub last_response { return $_[0]{last_response} }
Subroutine last_response: called CC=1 SCAR=0.0
Pod documented
85  
86 683 ▶sub _build_body {
Subroutine _build_body: called CC=6 SCAR=18.0
87 683 my ($self, $data, $headers) = @_;
88  
89 683 ▶ if (($headers->{'Content-Type'} // q{}) =~ m{\Aapplication/json\b}i) {
Branch: 1 branch
BranchTrueFalseTotal
if (($headers->{"Content-Type"} // "") =~ m[\Aapplication/json\b]iu) 122 561 683
Condition100%2/2
$headers->{"Content-Type"} // ""
l!l
122561
MC/DC100%1/1
$headers->{"Content-Type"} // ""
$headers->{"Content-Type"}
Truth table100%2/2
A
A= $headers->{"Content-Type"}
Aresult
11
00
90 122 ▶ return Twilio::Json::encode($data // {});
Condition50%1/2
$data // {}
l!l
1220
MC/DC0%0/1
$data // {}
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
91 }
92  
93 561 ▶ if (grep { _is_file($_) } _form_values($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {_is_file($_);} _form_values($data)) 7 554 561
94 7 my ($boundary, $body) = _multipart($data);
95 7 $headers->{'Content-Type'} = "multipart/form-data; boundary=$boundary";
96 7 return $body;
97 }
98  
99 554 ▶ $headers->{'Content-Type'} //= 'application/x-www-form-urlencoded';
Condition50%1/2
$headers->{"Content-Type"} //= "application/x-www-form-urlencoded"
l!l
0554
MC/DC0%0/1
$headers->{"Content-Type"} //= "application/x-www-form-urlencoded"
$headers->{"Content-Type"}
Truth table50%1/2
A
A= $headers->{"Content-Type"}
Aresult
11
00
100 554 return $self->build_query($data);
101 }
102  
103 561 ▶sub _form_values {
Subroutine _form_values: called CC=3 SCAR=11.0
104 561 my ($data) = @_;
105 561 ▶ return map { ref eq 'ARRAY' ? @{$_} : $_ } values %{ $data // {} };
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 118 3949 4067
Condition100%2/2
$data // {}
l!l
54714
MC/DC100%1/1
$data // {}
$data
Truth table100%2/2
A
A= $data
Aresult
11
00
106 }
107  
108 4198 ▶sub _is_file {
Subroutine _is_file: called CC=2 SCAR=6.9
109 4198 my ($value) = @_;
110 4198 ▶ return blessed($value) && $value->isa('Twilio::Http::File');
Condition100%3/3
builtin::blessed($value) && $value->isa('Twilio::Http::File')
!ll&&!rl&&r
4181116
MC/DC100%2/2
builtin::blessed($value) && $value->isa('Twilio::Http::File')
builtin::blessed($value) $value->isa('Twilio::Http::File')
Truth table100%3/3
A && B
A= builtin::blessed($value)B= $value->isa('Twilio::Http::File')
ABresult
0X0
100
111
111 }
112  
113 # Keys go in sorted order, as in a form, and a list sends one part per item.
114 7 ▶sub _multipart {
Subroutine _multipart: called CC=5 SCAR=16.1
115 7 my ($data) = @_;
116  
117 7 my @parts;
118 7 for my $key (sort keys %{$data}) {
119 49 my $value = $data->{$key};
120 49 ▶ for my $item (grep { defined } ref $value eq 'ARRAY' ? @{$value} : $value) {
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 1 48 49
121 16 ▶ push @parts, _is_file($item) ? _file_part($key, $item) : _text_part($key, $item);
Branch: 1 branch
BranchTrueFalseTotal
_is_file($item) ? : 8 8 16
122 }
123 }
124  
125 7 my $boundary = _boundary(@parts);
126 7 my $body = join q{}, map { "--$boundary\r\n$_\r\n" } @parts;
127 7 return ($boundary, "$body--$boundary--\r\n");
128 }
129  
130 8 ▶sub _text_part {
Subroutine _text_part: called CC=1 SCAR=0.0
131 8 my ($name, $value) = @_;
132 8 return _disposition($name) . "\r\n\r\n" . _utf8_bytes($value);
133 }
134  
135 8 ▶sub _file_part {
Subroutine _file_part: called CC=2 SCAR=6.9
136 8 my ($name, $file) = @_;
137  
138 8 ▶ my $type = $file->get_content_type // 'application/octet-stream';
Condition100%2/2
$file->get_content_type // "application/octet-stream"
l!l
26
MC/DC100%1/1
$file->get_content_type // "application/octet-stream"
$file->get_content_type
Truth table100%2/2
A
A= $file->get_content_type
Aresult
11
00
139 return
140 8 _disposition($name)
141 . '; filename="'
142 . _quoted($file->upload_name)
143 . "\"\r\nContent-Type: $type\r\n\r\n"
144 . $file->bytes;
145 }
146  
147 16 ▶sub _disposition {
Subroutine _disposition: called CC=1 SCAR=0.0
148 16 my ($name) = @_;
149 16 return 'Content-Disposition: form-data; name="' . _quoted($name) . q{"};
150 }
151  
152 # Escaped as browsers do in a form, so a name cannot end its header or add another.
153 24 ▶sub _quoted {
Subroutine _quoted: called CC=1 SCAR=0.0
154 24 my ($text) = @_;
155 24 my $bytes = _utf8_bytes($text);
156 24 $bytes =~ s/(["\r\n])/sprintf('%%%02X', ord $1)/ge;
157 24 return $bytes;
158 }
159  
160 # The boundary must not occur in any part, which is checked rather than left to chance.
161 7 ▶sub _boundary {
Subroutine _boundary: called CC=2 SCAR=6.9
162 7 my (@parts) = @_;
163  
164 7 my $boundary = _random_boundary();
165 7 ▶ $boundary = _random_boundary() while grep { index($_, $boundary) >= 0 } @parts;
Branch: 1 branch
BranchTrueFalseTotal
grep {index($_, $boundary) != -1;} @parts and do { $boundary = _random_boundary() } 1 7 8
166 7 return $boundary;
167 }
168  
169 6 ▶sub _random_boundary {
Subroutine _random_boundary: called CC=1 SCAR=0.0
170 6 return 'twilio-' . join q{}, map { sprintf '%04x', int rand 0x10000 } 1 .. 8;
171 }
172  
173 1895 ▶sub _basic_auth {
Subroutine _basic_auth: called CC=1 SCAR=0.0
174 1895 my ($username, $password) = @_;
175 1895 return 'Basic ' . encode_base64(_utf8_bytes("$username:$password"), q{});
176 }
177  
178 # Encodes like PHP's rawurlencode, but with spaces as "+", which is what Twilio's
179 # signature validation and the official libraries expect in form bodies.
180 8560 ▶sub _encode_component {
Subroutine _encode_component: called CC=1 SCAR=0.0
181 8560 my ($value) = @_;
182 8560 my $encoded = _utf8_bytes($value);
183 8560 $encoded =~ s/([^A-Za-z0-9._~ -])/sprintf('%%%02X', ord $1)/ge;
184 8560 $encoded =~ tr/ /+/;
185 8560 return $encoded;
186 }
187  
188 # Callers may hold text as characters or as UTF-8 bytes, for example a literal in a script
189 # without "use utf8". Treating valid UTF-8 bytes as already encoded sends both correctly,
190 # where always encoding would double-encode the second case and corrupt the message.
191 10487 ▶sub _utf8_bytes {
Subroutine _utf8_bytes: called CC=3 SCAR=11.0
192 10487 my ($value) = @_;
193 10487 my $text = "$value";
194  
195 10487 ▶ if (!utf8::is_utf8($text)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::is_utf8($text)) 10484 3 10487
196 10484 my $probe = $text;
197 10484 ▶ return $text if utf8::decode($probe);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::decode($probe) 10483 1 10484
198 }
199  
200 4 utf8::encode($text);
201 4 return $text;
202 }
203  
204 23 1;
205  
206 __END__
« Twilio/Base/BaseClient.pm ↑ Summary Twilio/Jwt/JWT.pm »