Mojolicious/Plugin/WebSocketProxy.pm

filter: Statementstmt 97.0% 66 / 68 Branchbran 63.6% 14 / 22 Conditioncond 43.7% 7 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 77.5% 100 / 129 CC 20 SCAR 22.2
CC
20
Cov
82%
register
16.6
__ANON__
6.7
__ANON__
3.5
CRAP 9.2
« Mojo/WebSocketProxy/Backend/JSONRPC.pm ↑ Summary Mojo/WebSocketProxy/Config.pm »
1 package Mojolicious::Plugin::WebSocketProxy;
2  
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 19 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use Mojo::WebSocketProxy::Backend;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 19 ▶use Mojo::WebSocketProxy::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use Mojo::WebSocketProxy::Dispatcher;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 19 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Other backend types may be available; we default to 'jsonrpc' in the code below
14 19 ▶use Mojo::WebSocketProxy::Backend::JSONRPC;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 19 our $VERSION = '0.16'; ## VERSION
17  
18 25 ▶sub register {
Subroutine register: called CC=14 SCAR=28.1
Pod documented
19 25 my ($self, $app, $config) = @_;
20  
21 25 ▶ die 'No base path found!' unless $config->{base_path};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"base_path"} 0 25 25
22  
23 25 my $url_setter;
24 25 ▶ $url_setter = delete $config->{url} if $config->{url} and ref($config->{url}) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"url"} and ref $config->{"url"} eq "CODE" 0 25 25
Condition66%2/3
$config->{"url"} and ref $config->{"url"} eq "CODE"
!ll&&!rl&&r
1240
MC/DC0%0/2
$config->{"url"} and ref $config->{"url"} eq "CODE"
$config->{"url"} ref $config->{"url"} eq "CODE"
Truth table66%2/3
A and B
A= $config->{"url"}B= ref $config->{"url"} eq "CODE"
ABresult
0X0
100
111
25 $app->helper(
26 2 ▶ call_rpc => sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
27 2 my ($c, $req_storage) = @_;
28 2 ▶ $url_setter->($c, $req_storage) if $url_setter && !$req_storage->{url};
Branch: 1 branch
BranchTrueFalseTotal
if $url_setter and not $req_storage->{"url"} 0 2 2
Condition33%1/3
$url_setter and not $req_storage->{"url"}
!ll&&!rl&&r
200
MC/DC0%0/2
$url_setter and not $req_storage->{"url"}
$url_setter not $req_storage->{"url"}
Truth table33%1/3
A and not(B)
A= $url_setterB= not $req_storage->{"url"}
ABresult
0X0
100
111
29 2 return $c->forward($req_storage);
30 25 });
31 $app->helper(
32 6 ▶ wsp_error => sub {
Subroutine __ANON__: called CC=5 SCAR=19.1
33 6 shift; # $c
34 6 my ($msg_type, $code, $message, $details) = @_;
35  
36 6 my $error = {
37 code => $code,
38 message => $message
39 };
40 6 ▶ $error->{details} = $details if ref($details) eq 'HASH' && keys %$details;
Branch: 1 branch
BranchTrueFalseTotal
if ref $details eq "HASH" and keys %$details 0 6 6
Condition33%1/3
ref $details eq "HASH" and keys %$details
!ll&&!rl&&r
600
MC/DC0%0/2
ref $details eq "HASH" and keys %$details
ref $details eq "HASH" keys %$details
Truth table33%1/3
A and B
A= ref $details eq "HASH"B= keys %$details
ABresult
0X0
100
111
41  
42 6 ▶ if ($details && ref($details) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($details and ref $details ne "HASH") 0 6 6
Condition33%1/3
$details and ref $details ne "HASH"
!ll&&!rl&&r
600
MC/DC0%0/2
$details and ref $details ne "HASH"
$details ref $details ne "HASH"
Truth table33%1/3
A and B
A= $detailsB= ref $details ne "HASH"
ABresult
0X0
100
111
43 0 $log->debugf("Details in a websocket error must be a hash reference instead of a %s", ref($details));
44 }
45  
46 return {
47 6 msg_type => $msg_type,
48 error => $error,
49 };
50 25 });
51  
52 25 my $r = $app->routes;
53 25 for ($r->under($config->{base_path})) {
54 25 $_->to('Dispatcher#ok', namespace => 'Mojo::WebSocketProxy');
55 25 $_->websocket('/')->to('Dispatcher#open_connection', namespace => 'Mojo::WebSocketProxy');
56 }
57  
58 25 my $actions = delete $config->{actions};
59 25 my $dispatcher_config = Mojo::WebSocketProxy::Config->new;
60 25 $dispatcher_config->init($config);
61  
62 25 ▶ if (ref $actions eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $actions eq 'ARRAY') { } 25 0 25
63 25 ▶ for (my $i = 0; $i < @$actions; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$actions) 32 25 57
64 32 $dispatcher_config->add_action($actions->[$i], $i);
65 }
66 } else {
67 0 die 'No actions found!';
68 }
69  
70 25 ▶ my $default_backend = delete $config->{default_backend} // '';
Condition50%1/2
delete $config->{"default_backend"} // ""
l!l
025
MC/DC0%0/1
delete $config->{"default_backend"} // ""
delete $config->{"default_backend"}
Truth table50%1/2
A
A= delete $config->{"default_backend"}
Aresult
11
00
71 25 ▶ if (my $backend_configs = delete $config->{backends}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $backend_configs = delete $config->{"backends"}) 1 24 25
72 1 foreach my $name (keys %$backend_configs) {
73 1 my %args = %{$backend_configs->{$name}};
74 1 ▶ my $type = delete($args{type}) // 'jsonrpc';
Condition50%1/2
delete $args{"type"} // "jsonrpc"
l!l
01
MC/DC0%0/1
delete $args{"type"} // "jsonrpc"
delete $args{"type"}
Truth table50%1/2
A
A= delete $args{"type"}
Aresult
11
00
75 1 ▶ my $key = $default_backend eq $name ? 'default' : $name;
Branch: 1 branch
BranchTrueFalseTotal
$default_backend eq $name ? : 0 1 1
76 1 $dispatcher_config->add_backend($key => Mojo::WebSocketProxy::Backend->backend_instance($type => %args));
77 }
78 }
79  
80 # For backwards compatibility, we always want to add a plain JSON::RPC backend
81 25 ▶ my $jsonrpc_backend_key = exists $dispatcher_config->{backends}->{default} ? 'http' : 'default';
Branch: 1 branch
BranchTrueFalseTotal
exists $dispatcher_config->{'backends'}{'default'} ? : 0 25 25
82 $dispatcher_config->add_backend(
83 $jsonrpc_backend_key => Mojo::WebSocketProxy::Backend->backend_instance(
84 jsonrpc => url => delete $config->{url},
85 25 ));
86  
87 $app->helper(
88 260 ▶ wsp_config => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
89 260 my $c = shift;
90 260 return $dispatcher_config;
91 25 });
92  
93 # Subscribe on notification about termination of worker.
94 25 ▶ Mojo::IOLoop->singleton->once(finish => $config->{before_shutdown}) if $config->{before_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"before_shutdown"} 1 24 25
95  
96 25 return;
97 }
98  
99 19 1;
100  
101 __END__
« Mojo/WebSocketProxy/Backend/JSONRPC.pm ↑ Summary Mojo/WebSocketProxy/Config.pm »