WebSocket.pm

filter: Statementstmt 39.1% 52 / 133 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 58.3% 14 / 24 Podpod 50.0% 1 / 2 totaltotal 31.9% 67 / 210 CC 21 SCAR 46.3
CC
21
Cov
30%
request
272.0
_jresponse_from_wsresponse
20.0
connect_ws
12.0
CRAP 102.2
↑ Summary WebSocket/Role/WebSocketResult.pm »
1 1 ▶use v5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package JMAP::Tester::WebSocket 0.006;
5 # ABSTRACT: a WebSocket JMAP client made for testing JMAP servers
6  
7 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use IO::Async::Timer::Countdown;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Net::Async::WebSocket::Client 0.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Protocol::WebSocket::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Params::Util qw(_HASH0 _ARRAY0);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use JMAP::Tester::WebSocket::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use JMAP::Tester::WebSocket::Result::Failure;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 extends qw(JMAP::Tester);
21  
22 {
23 1 package
24 JMAP::Tester::WebSocket::LogEvent;
25  
26 1 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 has as_string => (
29 init_arg => 'payload',
30 required => 1,
31 is => 'ro',
32 );
33 }
34  
35 has +json_codec => (
36 is => 'bare',
37 handles => {
38 json_encode => 'encode',
39 json_decode => 'decode',
40 },
41 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 0 require JSON;
43  
44 # Not ->utf8-> or we die decoding things with "wide character"...
45 # Maybe to be fixed in Protocol::WebSocket? Or IO::Async is doing this
46 # for us?
47 0 return JSON->new->convert_blessed;
48 },
49 1 );
50  
51 1 has 'timeout' => (
52 is => 'rw',
53 default => 30,
54 );
55  
56 1 has 'ws_api_uri' => (
57 is => 'rw',
58 required => 1,
59 );
60  
61 1 has cache_connection => (
62 is => 'ro',
63 default => 0,
64 );
65  
66 1 has 'authorization' => (
67 is => 'rw',
68 predicate => 'has_authorization',
69 );
70  
71 1 has _cached_client => (
72 is => 'rw',
73 );
74  
75 has loop => (
76 is => 'rw',
77 0 ▶ default => sub { IO::Async::Loop->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 1 );
79  
80 0 ▶sub request {
Subroutine request: not called CC=16 SCAR=56.1
Pod documented
81 0 my ($self, $input_request) = @_;
82  
83 0 state $ident = 'a';
84 0 my %seen;
85 my @suffixed;
86  
87 0 my %default_args = %{ $self->default_arguments };
88  
89 0 ▶ my $request = _ARRAY0($input_request)
Branch: 1 branch
BranchTrueFalseTotal
&_ARRAY0($input_request) ? : 0 0 0
90 ? { methodCalls => $input_request }
91 : { %$input_request };
92  
93 0 for my $call (@{ $request->{methodCalls} }) {
94 0 my $copy = [ @$call ];
95 0 ▶ if (defined $copy->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $copy->[2]) { } 0 0 0
96 0 $seen{$call->[2]}++;
97 } else {
98 0 my $next;
99 0 ▶ do { $next = $ident++ } until ! $seen{$ident}++;
Branch: 1 branch
BranchTrueFalseTotal
not $seen{$ident}++ or do { do { $next = $ident++ } } 0 0 0
100 0 $copy->[2] = $next;
101 }
102  
103 my %arg = (
104 %default_args,
105 0 ▶ %{ $copy->[1] // {} },
Condition0%0/2
$copy->[1] // {}
l!l
00
MC/DC0%0/1
$copy->[1] // {}
$copy->[1]
Truth table0%0/2
A
A= $copy->[1]
Aresult
11
00
106 );
107  
108 0 for my $key (keys %arg) {
109 0 ▶ if ( ref $arg{$key}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg{$key} and ref $arg{$key} eq "SCALAR" and not defined ${$arg{$key};}) 0 0 0
Condition0%0/6
ref $arg{$key} and ref $arg{$key} eq "SCALAR" and not defined ${$arg{$key};}
!ll&&!rl&&r
000
ref $arg{$key} and ref $arg{$key} eq "SCALAR"
!ll&&!rl&&r
000
MC/DC0%0/3
ref $arg{$key} and ref $arg{$key} eq "SCALAR" and not defined ${$arg{$key};}
ref $arg{$key} ref $arg{$key} eq "SCALAR" not defined ${$arg{$key};}
Truth table0%0/4
A and B and not(C)
A= ref $arg{$key}B= ref $arg{$key} eq "SCALAR"C= not defined ${$arg{$key};}
ABCresult
0XX0
10X0
1100
1111
110 && ref $arg{$key} eq 'SCALAR'
111 0 && ! defined ${ $arg{$key} }
112 ) {
113 0 delete $arg{$key};
114 }
115 }
116  
117 0 $copy->[1] = \%arg;
118  
119 0 push @suffixed, $copy;
120 }
121  
122 0 $request->{methodCalls} = \@suffixed;
123  
124 $request = $request->{methodCalls}
125 0 ▶ if $ENV{JMAP_TESTER_NO_WRAPPER} && _ARRAY0($input_request);
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"JMAP_TESTER_NO_WRAPPER"} and &_ARRAY0($input_request) 0 0 0
Condition0%0/3
$ENV{"JMAP_TESTER_NO_WRAPPER"} and &_ARRAY0($input_request)
!ll&&!rl&&r
000
MC/DC0%0/2
$ENV{"JMAP_TESTER_NO_WRAPPER"} and &_ARRAY0($input_request)
$ENV{"JMAP_TESTER_NO_WRAPPER"} &_ARRAY0($input_request)
Truth table0%0/3
A and B
A= $ENV{"JMAP_TESTER_NO_WRAPPER"}B= &_ARRAY0($input_request)
ABresult
0X0
100
111
126  
127 0 ▶ if ($self->_has_default_using && ! exists $request->{using}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_default_using and not exists $request->{"using"}) 0 0 0
Condition0%0/3
$self->_has_default_using and not exists $request->{"using"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_has_default_using and not exists $request->{"using"}
$self->_has_default_using not exists $request->{"using"}
Truth table0%0/3
A and not(B)
A= $self->_has_default_usingB= not exists $request->{"using"}
ABresult
0X0
100
111
128 0 $request->{using} = $self->default_using;
129 }
130  
131 # Required by RFC 8887
132 0 $request->{'@type'} = 'Request';
133  
134 0 my $json = $self->json_encode($request);
135  
136 0 ▶ my $client = $self->_cached_client || $self->connect_ws;
Condition0%0/3
$self->_cached_client || $self->connect_ws
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_cached_client || $self->connect_ws
$self->_cached_client $self->connect_ws
Truth table0%0/3
A || B
A= $self->_cached_clientB= $self->connect_ws
ABresult
1X1
011
000
137  
138 0 $self->_logger->log_jmap_request($self, {
139 http_request => JMAP::Tester::WebSocket::LogEvent->new({ payload => $json }),
140 });
141  
142 0 $client->send_text_frame($json);
143  
144 my $watchdog = IO::Async::Timer::Countdown->new(
145 delay => $self->timeout,
146 remove_on_expire => 1,
147 0 ▶ on_expire => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
148 0 my $seconds = $self->timeout;
149  
150 0 require Carp;
151 0 Carp::confess(
152 "JMAP::Tester::WebSocket->request() timed out after $seconds seconds"
153 );
154 },
155 0 )->start;
156  
157 0 $self->loop->add($watchdog);
158  
159 0 my $res = $self->loop->run;
160  
161 0 $watchdog->stop;
162 0 $self->loop->remove($watchdog);
163  
164 0 ▶ unless ($self->_cached_client) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_cached_client) 0 0 0
165 0 $self->loop->remove($client);
166 }
167  
168 0 $self->_logger->log_jmap_response($self, {
169 http_response => JMAP::Tester::WebSocket::LogEvent->new({ payload => $res }),
170 });
171  
172 0 return $self->_jresponse_from_wsresponse($res);
173 }
174  
175 0 ▶sub connect_ws {
Subroutine connect_ws: not called CC=3 SCAR=24.8
Pod undocumented
176 0 my ($self) = @_;
177  
178 0 my $loop = $self->loop;
179  
180 0 weaken($loop);
181  
182 my $client = Net::Async::WebSocket::Client->new(
183 0 ▶ on_text_frame => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
184 0 my ($c, $f) = @_;
185  
186 0 $loop->stop($f);
187 },
188 0 );
189  
190 0 $client->{framebuffer} = Protocol::WebSocket::Frame->new(
191 max_payload_size => 0
192 );
193  
194 0 $self->loop->add($client);
195  
196 my $watchdog = IO::Async::Timer::Countdown->new(
197 delay => $self->timeout,
198 remove_on_expire => 1,
199 0 ▶ on_expire => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
200 0 my $seconds = $self->timeout;
201  
202 0 require Carp;
203 0 Carp::confess(
204 "JMAP::Tester::WebSocket->connect_ws() timed out after $seconds seconds"
205 );
206 },
207 0 )->start;
208  
209 0 $self->loop->add($watchdog);
210  
211 0 ▶ $client->connect(
Branch: 1 branch
BranchTrueFalseTotal
$self->authorization ? : 0 0 0
212 url => $self->ws_api_uri,
213 req => Protocol::WebSocket::Request->new(
214 headers => [
215 ( $self->authorization
216 ? ( Authorization => $self->authorization )
217 : ()
218 ),
219 ],
220 subprotocol => 'jmap',
221 ),
222 )->get;
223  
224 0 $watchdog->stop;
225 0 $self->loop->remove($watchdog);
226  
227 0 ▶ if ($self->cache_connection) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache_connection) 0 0 0
228 0 $self->_cached_client($client);
229 }
230  
231 0 return $client;
232 }
233  
234 0 ▶sub _jresponse_from_wsresponse {
Subroutine _jresponse_from_wsresponse: not called CC=4 SCAR=30.0
235 0 my ($self, $ws_res) = @_;
236  
237 0 my ($data, $error);
238  
239 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
240 0 $data = $self->apply_json_types($self->json_decode( $ws_res ));
241 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
242 0 $error = $_;
243 0 };
244  
245 0 ▶ if (defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 0 0 0
246 0 return JMAP::Tester::WebSocket::Result::Failure->new(
247 diagnostic_dumper => $self->default_diagnostic_dumper,
248 ws_response => $ws_res,
249 ident => $error,
250 );
251 }
252  
253 0 my ($items, $props);
254 0 ▶ if (_HASH0($data)) {
Branch: 2 branches
BranchTrueFalseTotal
if (&_HASH0($data)) { } 0 0 0
elsif (&_ARRAY0($data)) { } 0 0 0
255 0 $props = $data;
256 0 $items = $props->{methodResponses};
257 } elsif (_ARRAY0($data)) {
258 0 $props = {};
259 0 $items = $data;
260 } else {
261 0 abort("illegal response to JMAP request: $data");
262 }
263  
264 0 return JMAP::Tester::WebSocket::Response->new({
265 diagnostic_dumper => $self->default_diagnostic_dumper,
266 items => $items,
267 ws_response => $ws_res,
268 wrapper_properties => $props,
269 });
270 }
271  
272 1 1;
273  
274 =pod
275  
276 =encoding UTF-8
277  
278 =head1 NAME
279  
280 JMAP::Tester::WebSocket - a WebSocket JMAP client made for testing JMAP servers
281  
282 =head1 VERSION
283  
284 version 0.006
285  
286 =head1 SYNOPSIS
287  
288 use JMAP::Tester::WebSocket;
289  
290 my $jtest = JMAP::Tester::WebSocket->new({
291 ws_api_uri => 'ws://jmap.local/account/123',
292 });
293  
294 my $response = $jtest->request([
295 [ getMailboxes => {} ],
296 [ getMessageUpdates => { sinceState => "123" } ],
297 ]);
298  
299 =head1 DESCRIPTION
300  
301 This module provides a WebSockets wrapper around L<JMAP::Tester>.
302  
303 See L<JMAP::Tester> for more information.
304  
305 =head1 SEE ALSO
306  
307 L<JMAP::Tester> - a JMAP client made for testing JMAP servers
308  
309 =head1 AUTHOR
310  
311 Matthew Horsfall <wolfsage@gmail.com>
312  
313 =head1 CONTRIBUTOR
314  
315 =for stopwords Ricardo Signes
316  
317 Ricardo Signes <rjbs@semiotic.systems>
318  
319 =head1 COPYRIGHT AND LICENSE
320  
321 This software is copyright (c) 2018 by FastMail, Ltd.
322  
323 This is free software; you can redistribute it and/or modify it under
324 the same terms as the Perl 5 programming language system itself.
325  
326 =cut
327  
328 __END__
↑ Summary WebSocket/Role/WebSocketResult.pm »