WebSocket.pm

filter: Statementstmt 39.8% 43 / 108 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 60.0% 12 / 20 Podpod 50.0% 1 / 2 totaltotal 30.9% 56 / 181 CC 21 SCAR 47.2
CC
21
Cov
29%
request
272.0
_jresponse_from_wsresponse
20.0
connect_ws
12.0
CRAP 112.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.002;
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 Net::Async::WebSocket::Client 0.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Protocol::WebSocket::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Params::Util qw(_HASH0 _ARRAY0);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use JMAP::Tester::WebSocket::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use JMAP::Tester::WebSocket::Result::Failure;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 extends qw(JMAP::Tester);
20  
21 has +json_codec => (
22 is => 'bare',
23 handles => {
24 json_encode => 'encode',
25 json_decode => 'decode',
26 },
27 default => sub {
28 0 ▶ require JSON;
Subroutine __ANON__: not called CC=1 SCAR=6.9
29  
30 # Not ->utf8-> or we die decoding things with "wide character"...
31 # Maybe to be fixed in Protocol::WebSocket? Or IO::Async is doing this
32 # for us?
33 0 return JSON->new->convert_blessed;
34 },
35 1 );
36  
37  
38 1 has 'ws_api_uri' => (
39 is => 'rw',
40 required => 1,
41 );
42  
43 1 has cache_connection => (
44 is => 'ro',
45 default => 0,
46 );
47  
48 1 has 'authorization' => (
49 is => 'rw',
50 predicate => 'has_authorization',
51 );
52  
53 1 has _cached_client => (
54 is => 'rw',
55 );
56  
57 has loop => (
58 is => 'rw',
59 0 ▶ default => sub { IO::Async::Loop->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 1 );
61  
62 sub request {
63 0 ▶ my ($self, $input_request) = @_;
Subroutine request: not called CC=16 SCAR=56.1
Pod documented
64  
65 0 state $ident = 'a';
66 0 my %seen;
67 my @suffixed;
68  
69 0 my %default_args = %{ $self->default_arguments };
70  
71 0 ▶ my $request = _ARRAY0($input_request)
Branch: 1 branch
BranchTrueFalseTotal
&_ARRAY0($input_request) ? : 0 0 0
72 ? { methodCalls => $input_request }
73 : { %$input_request };
74  
75 0 for my $call (@{ $request->{methodCalls} }) {
76 0 my $copy = [ @$call ];
77 0 ▶ if (defined $copy->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $copy->[2]) { } 0 0 0
78 0 $seen{$call->[2]}++;
79 } else {
80 0 my $next;
81 0 ▶ do { $next = $ident++ } until ! $seen{$ident}++;
Branch: 1 branch
BranchTrueFalseTotal
not $seen{$ident}++ or do { do { $next = $ident++ } } 0 0 0
82 0 $copy->[2] = $next;
83 }
84  
85 my %arg = (
86 %default_args,
87 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
88 );
89  
90 0 for my $key (keys %arg) {
91 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
92 && ref $arg{$key} eq 'SCALAR'
93 0 && ! defined ${ $arg{$key} }
94 ) {
95 0 delete $arg{$key};
96 }
97 }
98  
99 0 $copy->[1] = \%arg;
100  
101 0 push @suffixed, $copy;
102 }
103  
104 0 $request->{methodCalls} = \@suffixed;
105  
106 $request = $request->{methodCalls}
107 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
108  
109 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
110 0 $request->{using} = $self->default_using;
111 }
112  
113 # Required by RFC 8887
114 0 $request->{'@type'} = 'Request';
115  
116 0 my $json = $self->json_encode($request);
117  
118 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
119  
120 0 $client->send_text_frame($json);
121  
122 0 my $res = $self->loop->run;
123  
124 0 ▶ unless ($self->_cached_client) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_cached_client) 0 0 0
125 0 $self->loop->remove($client);
126 }
127  
128 0 return $self->_jresponse_from_wsresponse($res);
129 }
130  
131 sub connect_ws {
132 0 ▶ my ($self) = @_;
Subroutine connect_ws: not called CC=3 SCAR=24.8
Pod undocumented
133  
134 0 my $loop = $self->loop;
135  
136 0 weaken($loop);
137  
138 my $client = Net::Async::WebSocket::Client->new(
139 on_text_frame => sub {
140 0 ▶ my ($c, $f) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
141  
142 0 $loop->stop($f);
143 },
144 0 );
145  
146 0 $client->{framebuffer} = Protocol::WebSocket::Frame->new(
147 max_payload_size => 0
148 );
149  
150 0 $self->loop->add($client);
151  
152 0 ▶ $client->connect(
Branch: 1 branch
BranchTrueFalseTotal
$self->authorization ? : 0 0 0
153 url => $self->ws_api_uri,
154 req => Protocol::WebSocket::Request->new(
155 headers => [
156 ( $self->authorization
157 ? ( Authorization => $self->authorization )
158 : ()
159 ),
160 ],
161 subprotocol => 'jmap',
162 ),
163 )->get;
164  
165 0 ▶ if ($self->cache_connection) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache_connection) 0 0 0
166 0 $self->_cached_client($client);
167 }
168  
169 0 return $client;
170 }
171  
172 sub _jresponse_from_wsresponse {
173 0 ▶ my ($self, $ws_res) = @_;
Subroutine _jresponse_from_wsresponse: not called CC=4 SCAR=30.0
174  
175 0 my ($data, $error);
176  
177 try {
178 0 ▶ $data = $self->apply_json_types($self->json_decode( $ws_res ));
Subroutine __ANON__: not called CC=1 SCAR=6.9
179 } catch {
180 0 ▶ $error = $_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
181 0 };
182  
183 0 ▶ if (defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 0 0 0
184 0 return JMAP::Tester::WebSocket::Result::Failure->new(
185 ws_response => $ws_res,
186 ident => $error,
187 );
188 }
189  
190 0 my ($items, $props);
191 0 ▶ if (_HASH0($data)) {
Branch: 2 branches
BranchTrueFalseTotal
if (&_HASH0($data)) { } 0 0 0
elsif (&_ARRAY0($data)) { } 0 0 0
192 0 $props = $data;
193 0 $items = $props->{methodResponses};
194 } elsif (_ARRAY0($data)) {
195 0 $props = {};
196 0 $items = $data;
197 } else {
198 0 abort("illegal response to JMAP request: $data");
199 }
200  
201 0 return JMAP::Tester::WebSocket::Response->new({
202 items => $items,
203 ws_response => $ws_res,
204 wrapper_properties => $props,
205 });
206 }
207  
208 1 1;
209  
210 =pod
211  
212 =encoding UTF-8
213  
214 =head1 NAME
215  
216 JMAP::Tester::WebSocket - a WebSocket JMAP client made for testing JMAP servers
217  
218 =head1 VERSION
219  
220 version 0.002
221  
222 =head1 SYNOPSIS
223  
224 use JMAP::Tester::WebSocket;
225  
226 my $jtest = JMAP::Tester::WebSocket->new({
227 ws_uri => 'ws://jmap.local/account/123',
228 });
229  
230 my $response = $jtest->request([
231 [ getMailboxes => {} ],
232 [ getMessageUpdates => { sinceState => "123" } ],
233 ]);
234  
235 =head1 DESCRIPTION
236  
237 This module provides a WebSockets wrapper around L<JMAP::Tester>.
238  
239 See L<JMAP::Tester> for more information.
240  
241 =head1 SEE ALSO
242  
243 L<JMAP::Tester> - a JMAP client made for testing JMAP servers
244  
245 =head1 AUTHOR
246  
247 Matthew Horsfall <wolfsage@gmail.com>
248  
249 =head1 COPYRIGHT AND LICENSE
250  
251 This software is copyright (c) 2018 by FastMail, Ltd.
252  
253 This is free software; you can redistribute it and/or modify it under
254 the same terms as the Perl 5 programming language system itself.
255  
256 =cut
257  
258 __END__
↑ Summary WebSocket/Role/WebSocketResult.pm »