blib/lib/HTTP/UserAgentClientHints.pm

filter: Statementstmt 100% 66 / 66 Branchbran 95.0% 19 / 20 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 97.3% 111 / 114 SCAR 27.1
CC
15
Cov
98%
_normalize
6.0
_accept_ch
4.0
_as_http_header_key
3.0
CRAP 15.0
↑ Summary Build »
1 package HTTP::UserAgentClientHints;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use HTTP::UserAgentClientHints::BrandVersion;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use HTTP::UserAgentClientHints::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.04';
8  
9 2 my $HTTP_HEADER_PREFIX = 'Sec-CH-UA';
10  
11 2 my @FIELDS = qw/
12 UA
13 Mobile
14 Platform
15 Arch
16 Bitness
17 Model
18 Full-Version-List
19 Full-Version
20 /;
21  
22 # Build getters
23 2 for my $field (@FIELDS) {
24 16 my $method = __PACKAGE__->_as_method($field);
25 2 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
26 16 *{__PACKAGE__ . '::' . $method} = sub {
27 15 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
28 15 ▶ if (exists $self->{_value}{$method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_value"}{$method}) 3 12 15
29 3 return $self->{_value}{$method};
30 }
31 12 my $raw_value = $self->{_headers}->header($self->_as_http_header_key($field));
32 12 return $self->{_value}{$method} = $self->_normalize($raw_value, $field);
33 16 };
34 16 *{__PACKAGE__ . '::' . $method . '_raw'} = sub {
35 13 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
36 13 ▶ if (exists $self->{_value_raw}{$method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_value_raw"}{$method}) 1 12 13
37 1 return $self->{_value_raw}{$method};
38 }
39 12 return $self->{_value_raw}{$method} = $self->{_headers}->header($self->_as_http_header_key($field));
40 16 };
41 }
42  
43 2 my $FULL_ACCEPT_CH = __PACKAGE__->_accept_ch;
44  
45 sub _as_method {
46 16 ▶ my ($self, $field) = @_;
Subroutine _as_method: called CC=1 SCAR=0.0
47  
48 16 $field =~ s/-/_/g;
49 16 $field = lc $field;
50  
51 16 return $field;
52 }
53  
54 sub _as_http_header_key {
55 56 ▶ my ($self, $field) = @_;
Subroutine _as_http_header_key: called CC=3 SCAR=11.1
56  
57 56 ▶ return $HTTP_HEADER_PREFIX . (!$field || $field eq 'UA' ? '' : "-$field");
Branch: 1 branch
BranchTrueFalseTotal
!$field || $field eq 'UA' ? : 8 48 56
Condition66%2/3
not $field or $field eq "UA"
l!l&&r!l&&!r
0848
MC/DC50%1/2
not $field or $field eq "UA"
not $field $field eq "UA"
Truth table66%2/3
not(A) or B
A= not $fieldB= $field eq "UA"
ABresult
1X1
011
000
58 }
59  
60 sub _normalize {
61 12 ▶ my ($self, $value, $field) = @_;
Subroutine _normalize: called CC=6 SCAR=17.9
62  
63 12 ▶ return $value unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 10 12
64  
65 10 ▶ if ($field eq 'UA' || $field eq 'Full-Version-List') {
Branch: 3 branches
BranchTrueFalseTotal
if ($field eq 'UA' or $field eq 'Full-Version-List') { } 2 8 10
elsif ($field =~ /^(?:Platform|Arch|Bitness|Model|Full-Version)$/) { } 6 2 8
elsif ($field eq 'Mobile') { } 2 0 2
Condition100%3/3
$field eq "UA" or $field eq "Full-Version-List"
l!l&&r!l&&!r
118
MC/DC100%2/2
$field eq "UA" or $field eq "Full-Version-List"
$field eq "UA" $field eq "Full-Version-List"
Truth table100%3/3
A or B
A= $field eq "UA"B= $field eq "Full-Version-List"
ABresult
1X1
011
000
66 2 $value = HTTP::UserAgentClientHints::BrandVersion->new($value);
67 }
68 elsif ($field =~ m!^(?:Platform|Arch|Bitness|Model|Full-Version)$!) {
69 6 $value = HTTP::UserAgentClientHints::Util->strip_quote($value);
70 }
71 elsif ($field eq 'Mobile') {
72 2 $value =~ s/^\?//;
73 }
74  
75 10 return $value;
76 }
77  
78 sub new {
79 3 ▶ my ($class, $http_headers_obj) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
80  
81 3 ▶ unless ($http_headers_obj->can('header')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($http_headers_obj->can("header")) 1 2 3
82 1 die q|Argument object:| . ref($http_headers_obj) . q| doesn't have "header" method to get HTTP header value.|;
83 }
84  
85 bless {
86 2 _headers => $http_headers_obj,
87 _value_raw => {},
88 _value => {},
89 }, $class;
90 }
91  
92 sub accept_ch {
93 3 ▶ return $FULL_ACCEPT_CH unless $_[1];
Subroutine accept_ch: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 1 2 3
94  
95 2 return _accept_ch(@_);
96 }
97  
98 sub _accept_ch {
99 4 ▶ my ($self, $excepts) = @_;
Subroutine _accept_ch: called CC=4 SCAR=13.9
100  
101 4 ▶ $excepts ||= [];
Condition100%2/2
$excepts ||= []
l!l
22
MC/DC100%1/1
$excepts ||= []
$excepts
Truth table100%2/2
A
A= $excepts
Aresult
11
00
102  
103 4 unshift @{$excepts}, 'Sec-CH-UA', 'Sec-CH-UA-Mobile', 'Sec-CH-UA-Platform'; # Default fields
104  
105 4 my @accept_ch;
106 4 for my $field (@FIELDS) {
107 32 my $f = $self->_as_http_header_key($field);
108 32 ▶ next if grep { lc($f) eq lc($_) } @{$excepts};
Branch: 1 branch
BranchTrueFalseTotal
if grep {lc $f eq lc $_;} @{$excepts;} 15 17 32
109 17 push @accept_ch, $f;
110 }
111  
112 4 return join(', ', @accept_ch);
113 }
114  
115 2 1;
116  
117 __END__
↑ Summary Build »