blib/lib/Discord/Client/REST.pm

filter: Statementstmt 98.4% 252 / 256 Branchbran 85.7% 108 / 126 Conditioncond 57.9% 106 / 183 MC/DCmcdc 29.4% 28 / 95 Subroutinesub 96.0% 49 / 51 Podpod 100% 18 / 18 totaltotal 76.9% 561 / 729 CC 134 SCAR 32.5
CC
134
Cov
82%
new
96.6
request
23.1
_valid_message_query
18.3
CRAP 25.8
« blib/lib/Discord/Client/Interaction.pm ↑ Summary blib/lib/Discord/Client/Gateway.pm »
1 package Discord::Client::REST;
2  
3 22 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use Digest::SHA qw(sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use Net::Async::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 22 ▶use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC time);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 22 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 22 ▶use URI::Escape qw(uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 22 ▶use Discord::Client ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 22 ▶use Discord::Client::REST::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 22 ▶use Discord::Client::REST::RateLimiter;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 22 our $VERSION = '0.003';
21  
22 148 ▶sub new {
Subroutine new: called CC=34 SCAR=45.7
Pod documented
23 148 my ( $class, %args ) = @_;
24  
25 # Validate authentication and capacity before creating a transport.
26 148 ▶ my $auth = $args{auth} // 1;
Condition100%2/2
$args{"auth"} // 1
l!l
20128
MC/DC100%1/1
$args{"auth"} // 1
$args{"auth"}
Truth table100%2/2
A
A= $args{"auth"}
Aresult
11
00
27 148 ▶ croak 'auth must be zero or one' unless !ref($auth) && $auth =~ /\A[01]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $auth and $auth =~ /\A[01]\z/u 0 148 148
Condition33%1/3
not ref $auth and $auth =~ /\A[01]\z/u
!ll&&!rl&&r
00148
MC/DC0%0/2
not ref $auth and $auth =~ /\A[01]\z/u
not ref $auth $auth =~ /\A[01]\z/u
Truth table33%1/3
not(A) and B
A= not ref $authB= $auth =~ /\A[01]\z/u
ABresult
0X0
100
111
28 148 ▶ croak 'token is required' if $auth && ( !defined( $args{token} ) || !length( $args{token} ) );
Branch: 1 branch
BranchTrueFalseTotal
if $auth and not defined $args{"token"} && length $args{"token"} 0 148 148
Condition50%3/6
$auth and not defined $args{"token"} && length $args{"token"}
!ll&&!rl&&r
201280
defined $args{'token'} && length $args{'token'}
!ll&&!rl&&r
00128
MC/DC0%0/3
$auth and not defined $args{"token"} && length $args{"token"}
$auth defined $args{'token'} length $args{'token'}
Truth table50%2/4
A and not(B && C)
A= $authB= defined $args{'token'}C= length $args{'token'}
ABCresult
0XX0
1110
10X1
1101
29 croak 'token must be a scalar without whitespace'
30 if defined( $args{token} )
31 148 ▶ && ( ref( $args{token} ) || $args{token} =~ /[\s\x00-\x1f\x7f]/ );
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"token"} and ref $args{"token"} || $args{"token"} =~ /[\s\x00-\x1f\x7f]/u 0 148 148
Condition50%3/6
defined $args{"token"} and ref $args{"token"} || $args{"token"} =~ /[\s\x00-\x1f\x7f]/u
!ll&&!rl&&r
201280
ref $args{"token"} or $args{"token"} =~ /[\s\x00-\x1f\x7f]/u
l!l&&r!l&&!r
00128
MC/DC0%0/3
defined $args{"token"} and ref $args{"token"} || $args{"token"} =~ /[\s\x00-\x1f\x7f]/u
defined $args{"token"} ref $args{"token"} $args{"token"} =~ /[\s\x00-\x1f\x7f]/u
Truth table50%2/4
A and B or C
A= defined $args{"token"}B= ref $args{"token"}C= $args{"token"} =~ /[\s\x00-\x1f\x7f]/u
ABCresult
0XX0
1000
11X1
1011
32 148 ▶ my $loop = $args{loop} // IO::Async::Loop->new;
Condition33%1/3
$args{"loop"} // "IO::Async::Loop"->new
l!l&&r!l&&!r
14800
MC/DC0%0/2
$args{"loop"} // "IO::Async::Loop"->new
$args{"loop"} "IO::Async::Loop"->new
Truth table33%1/3
A // B
A= $args{"loop"}B= "IO::Async::Loop"->new
ABresult
1X1
011
000
33 148 my %limits = _scheduler_limits( \%args );
34 148 ▶ my $timeout = $args{timeout} // 30;
Condition100%2/2
$args{"timeout"} // 30
l!l
5143
MC/DC100%1/1
$args{"timeout"} // 30
$args{"timeout"}
Truth table100%2/2
A
A= $args{"timeout"}
Aresult
11
00
35 148 ▶ croak 'timeout must be positive'
Branch: 1 branch
BranchTrueFalseTotal
if ref $timeout or not $timeout =~ /\A\d+(?:\.\d+)?\z/u or $timeout <= 0 0 148 148
Condition33%2/6
ref $timeout or not $timeout =~ /\A\d+(?:\.\d+)?\z/u or $timeout <= 0
l!l&&r!l&&!r
00148
ref $timeout or not $timeout =~ /\A\d+(?:\.\d+)?\z/u
l!l&&r!l&&!r
00148
MC/DC0%0/3
ref $timeout or not $timeout =~ /\A\d+(?:\.\d+)?\z/u or $timeout <= 0
ref $timeout not $timeout =~ /\A\d+(?:\.\d+)?\z/u $timeout <= 0
Truth table25%1/4
A or not(B) or C
A= ref $timeoutB= not $timeout =~ /\A\d+(?:\.\d+)?\z/uC= $timeout <= 0
ABCresult
1XX1
01X1
0011
0000
36 if ref($timeout)
37 || $timeout !~ /\A\d+(?:\.\d+)?\z/
38 || $timeout <= 0;
39  
40 # Build an explicit API origin; credentials, query strings and fragments are invalid.
41 148 ▶ my $base = $args{base_url} // 'https://discord.com/api/v10';
Condition100%2/2
$args{"base_url"} // "https://discord.com/api/v10"
l!l
7141
MC/DC100%1/1
$args{"base_url"} // "https://discord.com/api/v10"
$args{"base_url"}
Truth table100%2/2
A
A= $args{"base_url"}
Aresult
11
00
42 148 my $uri = URI->new($base);
43 148 ▶ croak 'base_url must be an absolute HTTP(S) URL without credentials, query or fragment'
Branch: 1 branch
BranchTrueFalseTotal
unless ($uri->scheme // "") =~ /\Ahttps?\z/u and $uri->host and not defined $uri->userinfo and not defined $uri->query and not defined $uri->fragment 0 148 148
Condition35%5/14
($uri->scheme // "") =~ /\Ahttps?\z/u and $uri->host and not defined $uri->userinfo and not defined $uri->query and not defined $uri->fragment
!ll&&!rl&&r
00148
($uri->scheme // "") =~ /\Ahttps?\z/u and $uri->host and not defined $uri->userinfo and not defined $uri->query
!ll&&!rl&&r
00148
($uri->scheme // "") =~ /\Ahttps?\z/u and $uri->host and not defined $uri->userinfo
!ll&&!rl&&r
00148
($uri->scheme // "") =~ /\Ahttps?\z/u and $uri->host
!ll&&!rl&&r
00148
$uri->scheme // ""
l!l
1480
MC/DC0%0/5
($uri->scheme // "") =~ /\Ahttps?\z/u and $uri->host and not defined $uri->userinfo and not defined $uri->query and not defined $uri->fragment
$uri->scheme $uri->host not defined $uri->userinfo not defined $uri->query not defined $uri->fragment
Truth table16%1/6
A and B and not(C) and not(D) and not(E)
A= $uri->schemeB= $uri->hostC= not defined $uri->userinfoD= not defined $uri->queryE= not defined $uri->fragment
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
44 unless ( $uri->scheme // '' ) =~ /\Ahttps?\z/
45 && $uri->host
46 && !defined( $uri->userinfo )
47 && !defined( $uri->query )
48 && !defined( $uri->fragment );
49 148 $base =~ s{/\z}{};
50 148 ▶ my $url = $args{user_agent_url} // 'https://github.com/viasnake/Discord-Client';
Condition50%1/2
$args{"user_agent_url"} // "https://github.com/viasnake/Discord-Client"
l!l
0148
MC/DC0%0/1
$args{"user_agent_url"} // "https://github.com/viasnake/Discord-Client"
$args{"user_agent_url"}
Truth table50%1/2
A
A= $args{"user_agent_url"}
Aresult
11
00
51 148 ▶ croak 'user_agent_url must be an HTTP(S) URL without whitespace or parentheses'
Branch: 1 branch
BranchTrueFalseTotal
if ref $url or not $url =~ m[\Ahttps?://[^\s()]+\z]u 0 148 148
Condition33%1/3
ref $url or not $url =~ m[\Ahttps?://[^\s()]+\z]u
l!l&&r!l&&!r
00148
MC/DC0%0/2
ref $url or not $url =~ m[\Ahttps?://[^\s()]+\z]u
ref $url not $url =~ m[\Ahttps?://[^\s()]+\z]u
Truth table33%1/3
A or not(B)
A= ref $urlB= not $url =~ m[\Ahttps?://[^\s()]+\z]u
ABresult
1X1
011
000
52 if ref($url) || $url !~ m{\Ahttps?://[^\s()]+\z};
53  
54 148 for my $key (qw(clock wall_clock delay on_diagnostic)) {
55 592 ▶ croak "$key must be a code reference" if exists( $args{$key} ) && ref( $args{$key} ) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$key} and ref $args{$key} ne "CODE" 0 592 592
Condition66%2/3
exists $args{$key} and ref $args{$key} ne "CODE"
!ll&&!rl&&r
2243680
MC/DC0%0/2
exists $args{$key} and ref $args{$key} ne "CODE"
exists $args{$key} ref $args{$key} ne "CODE"
Truth table66%2/3
A and B
A= exists $args{$key}B= ref $args{$key} ne "CODE"
ABresult
0X0
100
111
56 }
57  
58 # A supplied transport stays caller-owned; an owned transport verifies TLS.
59 148 my $http = $args{http};
60 148 my $owns_http = !$http;
61 148 ▶ if ($owns_http) {
Branch: 1 branch
BranchTrueFalseTotal
if ($owns_http) 23 125 148
62 $http = Net::Async::HTTP->new(
63 fail_on_error => 0,
64 max_redirects => 0,
65 pipeline => 0,
66 SSL_verify_mode => 1,
67 SSL_verifycn_scheme => 'http',
68 max_connections_per_host => $limits{max_in_flight},
69 23 timeout => $timeout,
70 );
71 23 $loop->add($http);
72 }
73 148 ▶ croak 'http must provide do_request' unless blessed($http) && $http->can('do_request');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($http) and $http->can("do_request") 0 148 148
Condition33%1/3
builtin::blessed($http) and $http->can("do_request")
!ll&&!rl&&r
00148
MC/DC0%0/2
builtin::blessed($http) and $http->can("do_request")
builtin::blessed($http) $http->can("do_request")
Truth table33%1/3
A and B
A= builtin::blessed($http)B= $http->can("do_request")
ABresult
0X0
100
111
74 my $self = bless {
75 token => $args{token},
76 148 auth => 0 + $auth,
77 loop => $loop,
78 http => $http,
79 owns_http => $owns_http,
80 base_url => $base,
81 timeout => $timeout,
82 user_agent => "DiscordBot ($url, $Discord::Client::VERSION)",
83 json => JSON::PP->new->utf8->allow_nonref,
84 }, $class;
85  
86 # Inject clocks and delays so scheduling is deterministic without network I/O.
87 $self->{limiter} = Discord::Client::REST::RateLimiter->new(
88 %limits,
89 loop => $loop,
90 on_diagnostic => $args{on_diagnostic},
91 37 ▶ clock => $args{clock} // sub { clock_gettime(CLOCK_MONOTONIC) },
Subroutine __ANON__: called CC=1 SCAR=0.0
92 0 ▶ wall_clock => $args{wall_clock} // sub { time() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
93 0 ▶ delay => $args{delay} // sub { $loop->delay_future( after => $_[0] ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
94 148 ▶ );
Condition66%6/9
$args{"clock"} // sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
l!l&&r!l&&!r
122260
$args{"wall_clock"} // sub { &Discord::Client::REST::time(); }
l!l&&r!l&&!r
122260
$args{"delay"} // sub { $loop->delay_future("after", $_[0]); }
l!l&&r!l&&!r
122260
MC/DC0%0/2
$args{"clock"} // sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
$args{"clock"} sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
MC/DC0%0/2
$args{"wall_clock"} // sub { &Discord::Client::REST::time(); }
$args{"wall_clock"} sub { &Discord::Client::REST::time(); }
MC/DC0%0/2
$args{"delay"} // sub { $loop->delay_future("after", $_[0]); }
$args{"delay"} sub { $loop->delay_future("after", $_[0]); }
Truth table66%2/3
A // B
A= $args{"clock"}B= sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $args{"wall_clock"}B= sub { &Discord::Client::REST::time(); }
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $args{"delay"}B= sub { $loop->delay_future("after", $_[0]); }
ABresult
1X1
011
000
95 148 return $self;
96 }
97  
98 148 ▶sub _scheduler_limits {
Subroutine _scheduler_limits: called CC=9 SCAR=24.6
99 148 my ($options) = @_;
100  
101 # Retries may be disabled; queue and concurrency capacities must remain positive.
102 148 my %limits = ( max_pending => 1000, max_buckets => 1024, max_in_flight => 8, max_retries => 3 );
103 148 for my $key ( keys %limits ) {
104 592 ▶ $limits{$key} = $options->{$key} if exists $options->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{$key} 7 585 592
105 592 ▶ my $requirement = $key eq 'max_retries' ? 'a nonnegative integer' : 'a positive integer';
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'max_retries' ? : 148 444 592
106 croak "$key must be $requirement"
107 if !defined( $limits{$key} )
108 || ref( $limits{$key} )
109 || $limits{$key} !~ /\A\d+\z/
110 592 ▶ || ( $limits{$key} == 0 && $key ne 'max_retries' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $limits{$key} or ref $limits{$key} or not $limits{$key} =~ /\A\d+\z/u or $limits{$key} == 0 and $key ne "max_retries" 0 592 592
Condition41%5/12
not defined $limits{$key} or ref $limits{$key} or not $limits{$key} =~ /\A\d+\z/u or $limits{$key} == 0 and $key ne "max_retries"
l!l&&r!l&&!r
00592
not defined $limits{$key} or ref $limits{$key} or not $limits{$key} =~ /\A\d+\z/u
l!l&&r!l&&!r
00592
not defined $limits{$key} or ref $limits{$key}
l!l&&r!l&&!r
00592
$limits{$key} == 0 and $key ne "max_retries"
!ll&&!rl&&r
59110
MC/DC0%0/5
not defined $limits{$key} or ref $limits{$key} or not $limits{$key} =~ /\A\d+\z/u or $limits{$key} == 0 and $key ne "max_retries"
not defined $limits{$key} ref $limits{$key} not $limits{$key} =~ /\A\d+\z/u $limits{$key} == 0 $key ne "max_retries"
Truth table33%2/6
not(A) or B or not(C) or D and E
A= not defined $limits{$key}B= ref $limits{$key}C= not $limits{$key} =~ /\A\d+\z/uD= $limits{$key} == 0E= $key ne "max_retries"
ABCDEresult
1XXXX1
01XXX1
001XX1
000111
0000X0
000100
111 }
112 148 return %limits;
113 }
114  
115 78 ▶sub loop { $_[0]->{loop} }
Subroutine loop: called CC=1 SCAR=0.0
Pod documented
116 11 ▶sub stats { $_[0]->{limiter}->stats }
Subroutine stats: called CC=1 SCAR=0.0
Pod documented
117  
118 23 ▶sub _endpoint {
Subroutine _endpoint: called CC=5 SCAR=16.7
119 23 my ( $self, $method, $pattern, $ids, %args ) = @_;
120  
121 # Validate each resource segment before inserting it into a fixed endpoint.
122 23 for my $id (@$ids) {
123 30 ▶ return $self->_fail( $self->{loop}->new_future, 'Invalid resource ID', 'request' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and not ref $id and $id =~ /\A\d{1,20}\z/u 1 29 30
Condition50%3/6
defined $id and not ref $id and $id =~ /\A\d{1,20}\z/u
!ll&&!rl&&r
0129
defined $id and not ref $id
!ll&&!rl&&r
0030
MC/DC33%1/3
defined $id and not ref $id and $id =~ /\A\d{1,20}\z/u
defined $id not ref $id $id =~ /\A\d{1,20}\z/u
Truth table50%2/4
A and not(B) and C
A= defined $idB= not ref $idC= $id =~ /\A\d{1,20}\z/u
ABCresult
0XX0
10X0
1100
1111
124 unless defined($id) && !ref($id) && $id =~ /\A\d{1,20}\z/;
125 29 $pattern =~ s/\{id\}/$id/;
126 }
127 22 return $self->request( $method, $pattern, %args );
128 }
129 4 ▶sub get_current_user { $_[0]->request( 'GET', '/users/@me' ) }
Subroutine get_current_user: called CC=1 SCAR=0.0
Pod documented
130  
131 1 ▶sub get_user {
Subroutine get_user: called CC=1 SCAR=0.0
Pod documented
132 1 my ( $self, $user_id ) = @_;
133 1 return $self->_endpoint( 'GET', '/users/{id}', [$user_id] );
134 }
135  
136 2 ▶sub get_channel {
Subroutine get_channel: called CC=1 SCAR=0.0
Pod documented
137 2 my ( $self, $channel_id ) = @_;
138 2 return $self->_endpoint( 'GET', '/channels/{id}', [$channel_id] );
139 }
140  
141 1 ▶sub get_guild {
Subroutine get_guild: called CC=1 SCAR=0.0
Pod documented
142 1 my ( $self, $guild_id, %query ) = @_;
143 1 return $self->_endpoint( 'GET', '/guilds/{id}', [$guild_id], query => \%query );
144 }
145  
146 3 ▶sub get_guild_channels {
Subroutine get_guild_channels: called CC=1 SCAR=0.0
Pod documented
147 3 my ( $self, $guild_id ) = @_;
148 3 return $self->_endpoint( 'GET', '/guilds/{id}/channels', [$guild_id] );
149 }
150  
151 1 ▶sub get_guild_member {
Subroutine get_guild_member: called CC=1 SCAR=0.0
Pod documented
152 1 my ( $self, $guild_id, $user_id ) = @_;
153 1 return $self->_endpoint( 'GET', '/guilds/{id}/members/{id}', [ $guild_id, $user_id ] );
154 }
155  
156 2 ▶sub get_message {
Subroutine get_message: called CC=1 SCAR=0.0
Pod documented
157 2 my ( $self, $channel_id, $message_id ) = @_;
158 2 return $self->_endpoint( 'GET', '/channels/{id}/messages/{id}', [ $channel_id, $message_id ] );
159 }
160  
161 4 ▶sub get_messages {
Subroutine get_messages: called CC=2 SCAR=6.9
Pod documented
162 4 my ( $self, $channel_id, %query ) = @_;
163 4 ▶ return $self->_fail( $self->{loop}->new_future, 'Invalid message pagination', 'request' )
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_message_query(\%query) 3 1 4
164 unless _valid_message_query( \%query );
165 1 return $self->_endpoint( 'GET', '/channels/{id}/messages', [$channel_id], query => \%query );
166 }
167  
168 4 ▶sub _valid_message_query {
Subroutine _valid_message_query: called CC=14 SCAR=29.0
169 4 my ($query) = @_;
170  
171 # Pagination selects at most one position and a bounded page size.
172 4 ▶ return 0 if grep { $_ !~ /\A(?:before|after|around|limit)\z/ } keys %$query;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $_ =~ /\A(?:before|after|around|limit)\z/u;} keys %$query 0 4 4
173 4 ▶ return 0 if ( grep { exists $query->{$_} } qw(before after around) ) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if grep({exists $query->{$_};} "before", "after", "around") > 1 1 3 4
174 3 for my $key (qw(before after around)) {
175 9 ▶ next unless exists $query->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $query->{$key} 7 2 9
176 2 my $id = $query->{$key};
177 2 ▶ return 0 unless defined($id) && !ref($id) && $id =~ /\A\d{1,20}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and not ref $id and $id =~ /\A\d{1,20}\z/u 1 1 2
Condition50%3/6
defined $id and not ref $id and $id =~ /\A\d{1,20}\z/u
!ll&&!rl&&r
011
defined $id and not ref $id
!ll&&!rl&&r
002
MC/DC33%1/3
defined $id and not ref $id and $id =~ /\A\d{1,20}\z/u
defined $id not ref $id $id =~ /\A\d{1,20}\z/u
Truth table50%2/4
A and not(B) and C
A= defined $idB= not ref $idC= $id =~ /\A\d{1,20}\z/u
ABCresult
0XX0
10X0
1100
1111
178 }
179 2 ▶ if ( exists $query->{limit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $query->{"limit"}) 2 0 2
180 2 my $limit = $query->{limit};
181 2 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $limit and not ref $limit and $limit =~ /\A\d+\z/u and $limit >= 1 and $limit <= 100 1 1 2
Condition41%5/12
defined $limit and not ref $limit and $limit =~ /\A\d+\z/u and $limit >= 1 and $limit <= 100
!ll&&!rl&&r
011
defined $limit and not ref $limit and $limit =~ /\A\d+\z/u and $limit >= 1
!ll&&!rl&&r
002
defined $limit and not ref $limit and $limit =~ /\A\d+\z/u
!ll&&!rl&&r
002
defined $limit and not ref $limit
!ll&&!rl&&r
002
MC/DC20%1/5
defined $limit and not ref $limit and $limit =~ /\A\d+\z/u and $limit >= 1 and $limit <= 100
defined $limit not ref $limit $limit =~ /\A\d+\z/u $limit >= 1 $limit <= 100
Truth table33%2/6
A and not(B) and C and D and E
A= defined $limitB= not ref $limitC= $limit =~ /\A\d+\z/uD= $limit >= 1E= $limit <= 100
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
182 unless defined($limit)
183 && !ref($limit)
184 && $limit =~ /\A\d+\z/
185 && $limit >= 1
186 && $limit <= 100;
187 }
188 1 return 1;
189 }
190  
191 9 ▶sub _message_body {
Subroutine _message_body: called CC=4 SCAR=14.1
192 9 my ( $self, $method, $path, $ids, $body ) = @_;
193  
194 # Accept text as a convenience while preserving native Discord message fields.
195 9 ▶ $body = { content => "$body" } if defined($body) && !ref($body);
Branch: 1 branch
BranchTrueFalseTotal
if defined $body and not ref $body 5 4 9
Condition66%2/3
defined $body and not ref $body
!ll&&!rl&&r
045
MC/DC50%1/2
defined $body and not ref $body
defined $body not ref $body
Truth table66%2/3
A and not(B)
A= defined $bodyB= not ref $body
ABresult
0X0
100
111
196 9 ▶ return $self->_fail( $self->{loop}->new_future, 'Message body must be text or a hash reference', 'request' )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" 0 9 9
197 unless ref($body) eq 'HASH';
198 9 return $self->_endpoint( $method, $path, $ids, json => $body );
199 }
200  
201 7 ▶sub create_message {
Subroutine create_message: called CC=1 SCAR=0.0
Pod documented
202 7 my ( $self, $channel_id, $body ) = @_;
203 7 return $self->_message_body( 'POST', '/channels/{id}/messages', [$channel_id], $body );
204 }
205  
206 2 ▶sub edit_message {
Subroutine edit_message: called CC=1 SCAR=0.0
Pod documented
207 2 my ( $self, $channel_id, $message_id, $body ) = @_;
208 2 return $self->_message_body( 'PATCH', '/channels/{id}/messages/{id}', [ $channel_id, $message_id ], $body );
209 }
210  
211 2 ▶sub delete_message {
Subroutine delete_message: called CC=1 SCAR=0.0
Pod documented
212 2 my ( $self, $channel_id, $message_id ) = @_;
213 2 return $self->_endpoint( 'DELETE', '/channels/{id}/messages/{id}', [ $channel_id, $message_id ] );
214 }
215  
216 2 ▶sub create_dm {
Subroutine create_dm: called CC=4 SCAR=14.1
Pod documented
217 2 my ( $self, $id ) = @_;
218 2 ▶ return $self->_fail( $self->{loop}->new_future, 'Invalid resource ID', 'request' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and not ref $id and $id =~ /\A\d{1,20}\z/u 1 1 2
Condition66%4/6
defined $id and not ref $id and $id =~ /\A\d{1,20}\z/u
!ll&&!rl&&r
101
defined $id and not ref $id
!ll&&!rl&&r
011
MC/DC33%1/3
defined $id and not ref $id and $id =~ /\A\d{1,20}\z/u
defined $id not ref $id $id =~ /\A\d{1,20}\z/u
Truth table50%2/4
A and not(B) and C
A= defined $idB= not ref $idC= $id =~ /\A\d{1,20}\z/u
ABCresult
0XX0
10X0
1100
1111
219 unless defined($id) && !ref($id) && $id =~ /\A\d{1,20}\z/;
220 1 return $self->request( 'POST', '/users/@me/channels', json => { recipient_id => "$id" } );
221 }
222  
223 1 ▶sub trigger_typing {
Subroutine trigger_typing: called CC=1 SCAR=0.0
Pod documented
224 1 my ( $self, $channel_id ) = @_;
225 1 return $self->_endpoint( 'POST', '/channels/{id}/typing', [$channel_id] );
226 }
227  
228 158 ▶sub request {
Subroutine request: called CC=19 SCAR=31.4
Pod documented
229 158 my ( $self, $method, $path, %args ) = @_;
230  
231 # Reject invalid arguments before admitting the request to the scheduler.
232 158 my $future = $self->{loop}->new_future;
233 158 ▶ my $auth = exists( $args{auth} ) ? $args{auth} : $self->{auth};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'auth'} ? : 43 115 158
234 158 ▶ return $self->_fail( $future, 'auth must be zero or one', 'request' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $auth and not ref $auth and $auth =~ /\A[01]\z/u 0 158 158
Condition33%2/6
defined $auth and not ref $auth and $auth =~ /\A[01]\z/u
!ll&&!rl&&r
00158
defined $auth and not ref $auth
!ll&&!rl&&r
00158
MC/DC0%0/3
defined $auth and not ref $auth and $auth =~ /\A[01]\z/u
defined $auth not ref $auth $auth =~ /\A[01]\z/u
Truth table25%1/4
A and not(B) and C
A= defined $authB= not ref $authC= $auth =~ /\A[01]\z/u
ABCresult
0XX0
10X0
1100
1111
235 unless defined($auth) && !ref($auth) && $auth =~ /\A[01]\z/;
236 return $self->_fail( $future, 'Bot authorization requires a token', 'request' )
237 158 ▶ if $auth && ( !defined( $self->{token} ) || !length( $self->{token} ) );
Branch: 1 branch
BranchTrueFalseTotal
if $auth and not defined $self->{"token"} && length $self->{"token"} 1 157 158
Condition83%5/6
$auth and not defined $self->{"token"} && length $self->{"token"}
!ll&&!rl&&r
441131
defined $self->{'token'} && length $self->{'token'}
!ll&&!rl&&r
10113
MC/DC66%2/3
$auth and not defined $self->{"token"} && length $self->{"token"}
$auth defined $self->{'token'} length $self->{'token'}
Truth table75%3/4
A and not(B && C)
A= $authB= defined $self->{'token'}C= length $self->{'token'}
ABCresult
0XX0
1110
10X1
1101
238 157 ▶ return $self->_fail( $future, 'Invalid HTTP method', 'request' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method and not ref $method and $method =~ /\A(?:GET|POST|PUT|PATCH|DELETE|HEAD)\z/u 0 157 157
Condition33%2/6
defined $method and not ref $method and $method =~ /\A(?:GET|POST|PUT|PATCH|DELETE|HEAD)\z/u
!ll&&!rl&&r
00157
defined $method and not ref $method
!ll&&!rl&&r
00157
MC/DC0%0/3
defined $method and not ref $method and $method =~ /\A(?:GET|POST|PUT|PATCH|DELETE|HEAD)\z/u
defined $method not ref $method $method =~ /\A(?:GET|POST|PUT|PATCH|DELETE|HEAD)\z/u
Truth table25%1/4
A and not(B) and C
A= defined $methodB= not ref $methodC= $method =~ /\A(?:GET|POST|PUT|PATCH|DELETE|HEAD)\z/u
ABCresult
0XX0
10X0
1100
1111
239 unless defined($method) && !ref($method) && $method =~ /\A(?:GET|POST|PUT|PATCH|DELETE|HEAD)\z/;
240 157 ▶ return $self->_fail( $future, 'Path must be relative to the Discord API', 'request' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and not ref $path and $path =~ m[\A/[^/]]u and not $path =~ /[\s\x00-\x1f\x7f#]/u and not $path =~ m[(?:\A|/)\.\.?(?:/|\?|\z)]u 5 152 157
Condition75%9/12
defined $path and not ref $path and $path =~ m[\A/[^/]]u and not $path =~ /[\s\x00-\x1f\x7f#]/u and not $path =~ m[(?:\A|/)\.\.?(?:/|\?|\z)]u
!ll&&!rl&&r
41152
defined $path and not ref $path and $path =~ m[\A/[^/]]u and not $path =~ /[\s\x00-\x1f\x7f#]/u
!ll&&!rl&&r
22153
defined $path and not ref $path and $path =~ m[\A/[^/]]u
!ll&&!rl&&r
02155
defined $path and not ref $path
!ll&&!rl&&r
00157
MC/DC60%3/5
defined $path and not ref $path and $path =~ m[\A/[^/]]u and not $path =~ /[\s\x00-\x1f\x7f#]/u and not $path =~ m[(?:\A|/)\.\.?(?:/|\?|\z)]u
defined $path not ref $path $path =~ m[\A/[^/]]u not $path =~ /[\s\x00-\x1f\x7f#]/u not $path =~ m[(?:\A|/)\.\.?(?:/|\?|\z)]u
Truth table66%4/6
A and not(B) and C and not(D) and not(E)
A= defined $pathB= not ref $pathC= $path =~ m[\A/[^/]]uD= not $path =~ /[\s\x00-\x1f\x7f#]/uE= not $path =~ m[(?:\A|/)\.\.?(?:/|\?|\z)]u
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
241 unless defined($path)
242 && !ref($path)
243 && $path =~ m{\A/[^/]}
244 && $path !~ /[\s\x00-\x1f\x7f#]/
245 && $path !~ m{(?:\A|/)\.\.?(?:/|\?|\z)};
246  
247 # Prepare the complete wire request before reserving any scheduler capacity.
248 152 my ( $request, $error ) = $self->_build_request( $method, $path, $auth, \%args );
249 152 ▶ return $self->_fail( $future, $error, 'request' ) if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 1 151 152
250 151 my $uri = $request->uri;
251  
252 # Separate route scheduling from secret values retained only for error redaction.
253 151 my ( $route, $major, $secrets ) = _route( $method, URI->new($path)->path );
254 151 ▶ push @$secrets, $self->{token} if defined $self->{token};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"token"} 149 2 151
255 my $source = $self->{limiter}->submit(
256 route => $route,
257 major => $major,
258 auth => 0 + $auth,
259 global_scope => $auth ? 'bot' : 'unauthenticated',
260 150 ▶ send => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
261 $self->{http}->do_request(
262 request => $request,
263 host => $uri->host,
264 port => $uri->port,
265 SSL => $uri->scheme eq 'https',
266 timeout => $self->{timeout},
267 150 max_redirects => 0,
268 );
269 },
270 151 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$auth ? : 107 44 151
271  
272 # Propagate cancellation inward and sanitize transport results on the way out.
273 151 ▶ $future->on_cancel( sub { $source->cancel unless $source->is_ready } );
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
unless $source->is_ready 10 0 10
274 $source->on_ready(
275 151 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
276 151 $self->_complete_request( $future, $source, $method, $secrets, $args{raw_response} );
277 }
278 151 );
279 151 return $future;
280 }
281  
282 152 ▶sub _build_request {
Subroutine _build_request: called CC=9 SCAR=22.0
283 152 my ( $self, $method, $path, $auth, $options ) = @_;
284  
285 # Resolve the API-relative path and encode repeated query parameters explicitly.
286 152 my $uri = URI->new( $self->{base_url} . $path );
287  
288 152 ▶ if ( exists $options->{query} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"query"}) 6 146 152
289 return ( undef, 'query must be a hash reference' )
290 6 ▶ unless ref( $options->{query} ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $options->{"query"} eq "HASH" 0 6 6
291 6 my @query;
292 6 for my $key ( sort keys %{ $options->{query} } ) {
293 8 my $value = $options->{query}{$key};
294 8 ▶ push @query, $key, $_ for ref($value) eq 'ARRAY' ? @$value : ($value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 1 7 8
295 }
296 6 $uri->query_form(@query);
297 }
298  
299 # Construct the wire request without allowing caller-supplied authorization headers.
300 152 my $request = HTTP::Request->new( $method => $uri );
301 152 $request->protocol('HTTP/1.1');
302 152 $request->header( 'Host' => $uri->authority );
303 152 ▶ $request->header( 'Authorization' => 'Bot ' . $self->{token} ) if $auth;
Branch: 1 branch
BranchTrueFalseTotal
if $auth 108 44 152
304 152 $request->header( 'User-Agent' => $self->{user_agent} );
305 152 $request->header( 'Content-Type' => 'application/json' );
306 152 $request->header( 'Accept' => 'application/json' );
307  
308 # Serialize once so any rate-limit retry sends identical request bytes.
309 152 ▶ if ( exists $options->{json} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"json"}) 70 82 152
310 70 my $body = eval { $self->{json}->encode( $options->{json} ) };
311 70 ▶ return ( undef, 'Request body is not JSON encodable' ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 69 70
312 69 $request->content($body);
313 }
314  
315 151 return ( $request, undef );
316 }
317  
318 151 ▶sub _complete_request {
Subroutine _complete_request: called CC=16 SCAR=28.0
319 151 my ( $self, $future, $source, $method, $secrets, $raw_response ) = @_;
320  
321 # Cancellation callbacks run before readiness; do not re-enter cancellation
322 # when the scheduler reports the cancellation we just propagated inward.
323 151 ▶ return if $future->is_ready || $future->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
if $future->is_ready or $future->is_cancelled 10 141 151
Condition66%2/3
$future->is_ready or $future->is_cancelled
l!l&&r!l&&!r
010141
MC/DC50%1/2
$future->is_ready or $future->is_cancelled
$future->is_ready $future->is_cancelled
Truth table66%2/3
A or B
A= $future->is_readyB= $future->is_cancelled
ABresult
1X1
011
000
324 141 ▶ if ( $source->is_cancelled ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->is_cancelled) 0 141 141
325 0 $future->cancel;
326 0 return;
327 }
328  
329 # Scheduler failures may carry an HTTP response with useful status information.
330 141 my ( $message, $category, $response );
331 141 ▶ if ( $source->is_failed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->is_failed) { } 20 121 141
332 20 ( $message, $category, $response ) = $source->failure;
333 20 ▶ return $self->_fail( $future, $message, $category ) unless $response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 16 4 20
334 }
335 else {
336 121 ($response) = $source->result;
337 }
338  
339 # HEAD and 204 responses have no JSON body to decode.
340 125 my $data;
341 125 my $valid_json = 1;
342 125 ▶ if ( $response->code != 204 && $method ne 'HEAD' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code != 204 and $method ne "HEAD") 91 34 125
Condition100%3/3
$response->code != 204 and $method ne "HEAD"
!ll&&!rl&&r
33191
MC/DC100%2/2
$response->code != 204 and $method ne "HEAD"
$response->code != 204 $method ne "HEAD"
Truth table100%3/3
A and B
A= $response->code != 204B= $method ne "HEAD"
ABresult
0X0
100
111
343 91 $data = eval { $self->{json}->decode( $response->content ) };
344 91 $valid_json = !$@;
345 }
346  
347 # Expose structured diagnostics only after removing known credentials.
348 125 ▶ if ( !$response->is_success || defined $category ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $response->is_success or defined $category) 17 108 125
Condition66%2/3
not $response->is_success or defined $category
l!l&&r!l&&!r
170108
MC/DC50%1/2
not $response->is_success or defined $category
not $response->is_success defined $category
Truth table66%2/3
not(A) or B
A= not $response->is_successB= defined $category
ABresult
1X1
011
000
349 17 ▶ $category //= _http_error_category( $response->code );
Condition66%2/3
$category //= _http_error_category($response->code)
l!l&&r!l&&!r
4130
MC/DC0%0/2
$category //= _http_error_category($response->code)
$category _http_error_category($response->code)
Truth table66%2/3
A //= B
A= $categoryB= _http_error_category($response->code)
ABresult
1X1
011
000
350 17 ▶ $message //= 'Discord REST request failed (HTTP ' . $response->code . ')';
Condition100%2/2
$message //= 'Discord REST request failed (HTTP ' . $response->code . ')'
l!l
413
MC/DC100%1/1
$message //= 'Discord REST request failed (HTTP ' . $response->code . ')'
$message
Truth table100%2/2
A
A= $message
Aresult
11
00
351 17 ▶ my $request_id = $response->header('X-Request-ID') // $response->header('CF-Ray');
Condition66%2/3
$response->header("X-Request-ID") // $response->header("CF-Ray")
l!l&&r!l&&!r
5012
MC/DC50%1/2
$response->header("X-Request-ID") // $response->header("CF-Ray")
$response->header("X-Request-ID") $response->header("CF-Ray")
Truth table66%2/3
A // B
A= $response->header("X-Request-ID")B= $response->header("CF-Ray")
ABresult
1X1
011
000
352 17 ▶ return $self->_fail(
Branch: 1 branch
BranchTrueFalseTotal
$valid_json ? : 16 1 17
353 $future, $message, $category,
354 status => $response->code,
355 discord_code => _discord_error_code($data),
356 request_id => _sanitize( $request_id, $secrets ),
357 details => $valid_json ? _sanitize( $data, $secrets ) : undef,
358 );
359 }
360 108 ▶ return $self->_fail( $future, 'Discord returned invalid JSON', 'json', status => $response->code )
Branch: 1 branch
BranchTrueFalseTotal
unless $valid_json 1 107 108
361 unless $valid_json;
362  
363 # A raw response must not retain the authenticated originating request.
364 107 ▶ if ($raw_response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_response) { } 1 106 107
365 1 my $copy = $response->clone;
366 1 $copy->request(undef);
367 1 $future->done($copy);
368 }
369 else {
370 106 $future->done($data);
371 }
372 }
373  
374 13 ▶sub _http_error_category {
Subroutine _http_error_category: called CC=5 SCAR=16.1
375 13 my ($status) = @_;
376 13 ▶ return 'authentication' if $status == 401;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 401 4 9 13
377 9 ▶ return 'permission' if $status == 403;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 403 1 8 9
378 8 ▶ return 'not_found' if $status == 404;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 404 1 7 8
379 7 ▶ return 'server' if $status >= 500;
Branch: 1 branch
BranchTrueFalseTotal
if $status >= 500 3 4 7
380 4 return 'http';
381 }
382  
383 17 ▶sub _discord_error_code {
Subroutine _discord_error_code: called CC=5 SCAR=16.2
384 17 my ($data) = @_;
385  
386 # Only expose a scalar numeric Discord code from the decoded response.
387 return undef
388 unless ref($data) eq 'HASH'
389 && defined( $data->{code} )
390 && !ref( $data->{code} )
391 17 ▶ && $data->{code} =~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" and defined $data->{"code"} and not ref $data->{"code"} and $data->{"code"} =~ /\A\d+\z/u 7 10 17
Condition77%7/9
ref $data eq "HASH" and defined $data->{"code"} and not ref $data->{"code"} and $data->{"code"} =~ /\A\d+\z/u
!ll&&!rl&&r
7010
ref $data eq "HASH" and defined $data->{"code"} and not ref $data->{"code"}
!ll&&!rl&&r
7010
ref $data eq "HASH" and defined $data->{"code"}
!ll&&!rl&&r
1610
MC/DC50%2/4
ref $data eq "HASH" and defined $data->{"code"} and not ref $data->{"code"} and $data->{"code"} =~ /\A\d+\z/u
ref $data eq "HASH" defined $data->{"code"} not ref $data->{"code"} $data->{"code"} =~ /\A\d+\z/u
Truth table60%3/5
A and B and not(C) and D
A= ref $data eq "HASH"B= defined $data->{"code"}C= not ref $data->{"code"}D= $data->{"code"} =~ /\A\d+\z/u
ABCDresult
0XXX0
10XX0
110X0
11100
11111
392 10 return $data->{code};
393 }
394  
395 151 ▶sub _route {
Subroutine _route: called CC=10 SCAR=23.0
396 151 my ( $method, $path ) = @_;
397  
398 # Keep major resource IDs distinct while normalizing ordinary resource IDs.
399 151 my @segments = split m{/}, $path;
400 151 my ( @major, @secrets );
401 151 ▶ for ( my $segment_index = 1 ; $segment_index < @segments ; ++$segment_index ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($segment_index < @segments) 350 151 501
402 350 my $segment = $segments[$segment_index];
403  
404 # Major resource IDs partition buckets even when the remaining route shape matches.
405 350 ▶ if ( $segment =~ /\A(?:channels|guilds|webhooks)\z/ && defined( $segments[ $segment_index + 1 ] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($segment =~ /\A(?:channels|guilds|webhooks)\z/u and defined $segments[$segment_index + 1]) 92 258 350
Condition100%3/3
$segment =~ /\A(?:channels|guilds|webhooks)\z/u and defined $segments[$segment_index + 1]
!ll&&!rl&&r
254492
MC/DC100%2/2
$segment =~ /\A(?:channels|guilds|webhooks)\z/u and defined $segments[$segment_index + 1]
$segment =~ /\A(?:channels|guilds|webhooks)\z/u defined $segments[$segment_index + 1]
Truth table100%3/3
A and B
A= $segment =~ /\A(?:channels|guilds|webhooks)\z/uB= defined $segments[$segment_index + 1]
ABresult
0X0
100
111
406 92 push @major, $segment, $segments[ $segment_index + 1 ];
407 92 ++$segment_index;
408 92 $segments[$segment_index] = ':major';
409  
410 # Reserved subresource names are not treated as webhook token segments.
411 92 my $token = $segments[ $segment_index + 1 ];
412 92 ▶ if ( $segment eq 'webhooks' && defined($token) && $token !~ /\A(?:messages|github|slack)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($segment eq "webhooks" and defined $token and not $token =~ /\A(?:messages|github|slack)\z/u) 16 76 92
Condition66%4/6
$segment eq "webhooks" and defined $token and not $token =~ /\A(?:messages|github|slack)\z/u
!ll&&!rl&&r
76016
$segment eq "webhooks" and defined $token
!ll&&!rl&&r
76016
MC/DC33%1/3
$segment eq "webhooks" and defined $token and not $token =~ /\A(?:messages|github|slack)\z/u
$segment eq "webhooks" defined $token not $token =~ /\A(?:messages|github|slack)\z/u
Truth table50%2/4
A and B and not(C)
A= $segment eq "webhooks"B= defined $tokenC= not $token =~ /\A(?:messages|github|slack)\z/u
ABCresult
0XX0
10X0
1100
1111
413  
414 # Hash the token for bucket identity, but retain both spellings for redaction.
415 16 push @major, sha256_hex($token);
416 16 push @secrets, $token, uri_unescape($token);
417 16 ++$segment_index;
418 16 $segments[$segment_index] = ':token';
419 }
420 92 next;
421 }
422  
423 # Callback tokens are opaque, including tokens consisting only of digits.
424 258 ▶ if ( $segments[1] eq 'interactions' && $segment_index == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($segments[1] eq "interactions" and $segment_index == 3) 34 224 258
Condition100%3/3
$segments[1] eq "interactions" and $segment_index == 3
!ll&&!rl&&r
12210234
MC/DC100%2/2
$segments[1] eq "interactions" and $segment_index == 3
$segments[1] eq "interactions" $segment_index == 3
Truth table100%3/3
A and B
A= $segments[1] eq "interactions"B= $segment_index == 3
ABresult
0X0
100
111
425 34 push @secrets, $segment, uri_unescape($segment);
426 34 $segments[$segment_index] = ':token';
427 34 next;
428 }
429 224 ▶ $segments[$segment_index] = ':id' if $segment =~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $segment =~ /\A\d+\z/u 49 175 224
430 }
431  
432 # Only token hashes participate in bucket keys; plaintext is used for redaction.
433 151 my $major = join( ':', @major );
434 151 return ( $method . ' ' . join( '/', @segments ) . ' ' . $major, $major, \@secrets );
435 }
436  
437 68 ▶sub _sanitize {
Subroutine _sanitize: called CC=8 SCAR=20.8
438 68 my ( $value, $secrets ) = @_;
439  
440 # Redact both sensitive field names and known secrets nested in response data.
441 68 ▶ if ( ref($value) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 22 46 68
442 return {
443 22 ▶ map { $_ => /(?:token|authorization|secret)/i ? '[REDACTED]' : _sanitize( $value->{$_}, $secrets ) }
Branch: 1 branch
BranchTrueFalseTotal
/(?:token|authorization|secret)/iu ? : 2 34 36
444 keys %$value
445 };
446 }
447 46 ▶ if ( ref($value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 1 45 46
448 1 return [ map { _sanitize( $_, $secrets ) } @$value ];
449 }
450 45 ▶ return $value if !defined($value) || ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value 12 33 45
Condition66%2/3
not defined $value or ref $value
l!l&&r!l&&!r
12033
MC/DC50%1/2
not defined $value or ref $value
not defined $value ref $value
Truth table66%2/3
not(A) or B
A= not defined $valueB= ref $value
ABresult
1X1
011
000
451 33 for my $secret (@$secrets) {
452 41 ▶ $value =~ s/\Q$secret\E/[REDACTED]/g if length $secret;
Branch: 1 branch
BranchTrueFalseTotal
if length $secret 41 0 41
453 }
454 33 return $value;
455 }
456  
457 46 ▶sub _fail {
Subroutine _fail: called CC=1 SCAR=0.0
458 46 my ( $self, $future, $message, $category, %details ) = @_;
459  
460 # Use the same sanitized details in the Future tuple and structured REST error.
461 46 my $error = Discord::Client::REST::Error->new(
462 message => $message,
463 category => $category,
464 %details,
465 );
466 46 return $future->fail( $message, $category, $error );
467 }
468  
469 148 ▶sub close {
Subroutine close: called CC=3 SCAR=11.0
Pod documented
470 148 my ($self) = @_;
471  
472 # Stop scheduled requests before detaching an owned HTTP transport.
473 148 my $future = $self->{limiter}->close;
474 148 ▶ if ( $self->{owns_http} && $self->{http}->loop ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"owns_http"} and $self->{"http"}->loop) 22 126 148
Condition66%2/3
$self->{"owns_http"} and $self->{"http"}->loop
!ll&&!rl&&r
126022
MC/DC50%1/2
$self->{"owns_http"} and $self->{"http"}->loop
$self->{"owns_http"} $self->{"http"}->loop
Truth table66%2/3
A and B
A= $self->{"owns_http"}B= $self->{"http"}->loop
ABresult
0X0
100
111
475 22 $self->{loop}->remove( $self->{http} );
476 }
477 148 return $future;
478 }
479  
480 22 1;
481  
482 __END__
« blib/lib/Discord/Client/Interaction.pm ↑ Summary blib/lib/Discord/Client/Gateway.pm »