Plient/Handler/HTTPTiny.pm

filter: Statementstmt 19.7% 13 / 66 Branchbran 6.2% 2 / 32 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 20.0% 2 / 10 Podpod 0.0% 0 / 6 totaltotal 12.2% 17 / 139 CC 23 SCAR 37.0
CC
23
Cov
13%
__ANON__
72.0
add_headers
56.0
support_method
42.0
CRAP 40.6
« Plient/Handler/curl.pm ↑ Summary Plient/Handler/wget.pm »
1 package Plient::Handler::HTTPTiny;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶require Plient::Handler unless $Plient::bundle_mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $Plient::bundle_mode 5 0 5
6 5 our @ISA = 'Plient::Handler';
7 5 my ( $HTTPTiny, %all_protocol, %protocol, %method );
8  
9 5 %all_protocol = ( http => undef );
10 0 ▶sub all_protocol { return \%all_protocol }
Subroutine all_protocol: not called CC=1 SCAR=6.9
Pod undocumented
11 0 ▶sub protocol { return \%protocol }
Subroutine protocol: not called CC=1 SCAR=6.9
Pod undocumented
12 0 ▶sub method { return \%method }
Subroutine method: not called CC=1 SCAR=6.9
Pod undocumented
13  
14 0 ▶sub support_method {
Subroutine support_method: not called CC=6 SCAR=37.4
Pod undocumented
15 0 my $class = shift;
16 0 my ( $method, $args ) = @_;
17 0 ▶ if ( $args
Branch: 1 branch
BranchTrueFalseTotal
if ($args and $args->{"content_type"} and $args->{"content_type"} =~ /form-data/) 0 0 0
Condition0%0/6
$args and $args->{"content_type"} and $args->{"content_type"} =~ /form-data/
!ll&&!rl&&r
000
$args and $args->{"content_type"}
!ll&&!rl&&r
000
MC/DC0%0/3
$args and $args->{"content_type"} and $args->{"content_type"} =~ /form-data/
$args $args->{"content_type"} $args->{"content_type"} =~ /form-data/
Truth table0%0/4
A and B and C
A= $argsB= $args->{"content_type"}C= $args->{"content_type"} =~ /form-data/
ABCresult
0XX0
10X0
1100
1111
18 && $args->{content_type}
19 && $args->{content_type} =~ 'form-data' )
20 {
21 0 return;
22 }
23  
24 0 ▶ if ( $ENV{http_proxy} && $ENV{http_proxy} =~ /@/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"http_proxy"} and $ENV{"http_proxy"} =~ /@/) 0 0 0
Condition0%0/3
$ENV{"http_proxy"} and $ENV{"http_proxy"} =~ /@/
!ll&&!rl&&r
000
MC/DC0%0/2
$ENV{"http_proxy"} and $ENV{"http_proxy"} =~ /@/
$ENV{"http_proxy"} $ENV{"http_proxy"} =~ /@/
Truth table0%0/3
A and B
A= $ENV{"http_proxy"}B= $ENV{"http_proxy"} =~ /@/
ABresult
0X0
100
111
25  
26 # HTTPTiny doesn't support proxy auth
27 0 return;
28 }
29  
30 0 return $class->SUPER::support_method(@_);
31 }
32  
33 5 my $inited;
34  
35 0 ▶sub init {
Subroutine init: not called CC=3 SCAR=24.8
Pod undocumented
36 0 ▶ return if $inited;
Branch: 1 branch
BranchTrueFalseTotal
if $inited 0 0 0
37 0 $inited = 1;
38 0 ▶ eval { require HTTP::Tiny } or return;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require HTTP::Tiny } } or return 0 0 0
39 0 undef $protocol{http};
40 0 ▶ $method{http_get} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
41 0 my ( $uri, $args ) = @_;
42 0 my $http = HTTP::Tiny->new;
43 0 add_headers( $http, $uri, $args );
44 0 ▶ $http->{proxy} = $ENV{http_proxy} if $ENV{http_proxy};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"http_proxy"} 0 0 0
45 0 my $res = $http->get($uri);
46  
47 0 ▶ if ( $res->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{'success'}) { } 0 0 0
48 0 return $res->{content};
49 }
50 else {
51 0 warn "failed to get $uri with HTTP::Tiny: " . $res;
52 0 return;
53 }
54 0 };
55  
56 0 ▶ $method{http_post} = sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
57 0 my ( $uri, $args ) = @_;
58 0 my $http = HTTP::Tiny->new;
59 0 ▶ $http->proxy( $ENV{http_proxy} ) if $ENV{http_proxy};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"http_proxy"} 0 0 0
60 0 add_headers( $http, $uri, $args );
61  
62 0 my $body;
63 0 ▶ if ( $args->{body_hash} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"body_hash"}) 0 0 0
64 0 for my $key ( keys %{$args->{body_hash}} ) {
65 # TODO uri escape key and value
66 0 my $val = $args->{body_hash}{$key};
67 0 ▶ $body .= $body ? "&$key=$val" : "$key=$val";
Branch: 1 branch
BranchTrueFalseTotal
$body ? : 0 0 0
68 }
69 }
70  
71 $http->{default_headers}{'Content-Type'} =
72 'application/x-www-form-urlencoded'
73 0 ▶ unless $http->{default_headers}{'Content-Type'};
Branch: 1 branch
BranchTrueFalseTotal
unless $http->{"default_headers"}{"Content-Type"} 0 0 0
74  
75 0 ▶ my $res = $http->request(
Branch: 1 branch
BranchTrueFalseTotal
defined $body ? : 0 0 0
76 'POST', $uri,
77 {
78 defined $body
79 ? ( content => $body )
80 : ()
81 }
82 );
83  
84 0 ▶ if ( $res->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{'success'}) { } 0 0 0
85 0 return $res->{content};
86 }
87 else {
88 0 warn "failed to post $uri with HTTP::Tiny: " . $res;
89 0 return;
90 }
91 0 };
92  
93 0 return 1;
94 }
95  
96 0 ▶sub add_headers {
Subroutine add_headers: not called CC=7 SCAR=40.3
Pod undocumented
97 0 my ( $http, $uri, $args ) = @_;
98 0 ▶ my $headers = $args->{headers} || {};
Condition0%0/2
$args->{'headers'} || {}
l!l
00
MC/DC0%0/1
$args->{'headers'} || {}
$args->{'headers'}
Truth table0%0/2
A
A= $args->{'headers'}
Aresult
11
00
99 0 for my $k ( keys %$headers ) {
100 0 $http->{default_headers}{$k} = $headers->{$k};
101 }
102  
103 0 ▶ if ( $args->{user} && defined $args->{password} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"user"} and defined $args->{"password"}) 0 0 0
Condition0%0/3
$args->{"user"} and defined $args->{"password"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"user"} and defined $args->{"password"}
$args->{"user"} defined $args->{"password"}
Truth table0%0/3
A and B
A= $args->{"user"}B= defined $args->{"password"}
ABresult
0X0
100
111
104 0 ▶ my $method = lc $args->{auth_method} || 'basic';
Condition0%0/2
lc $args->{'auth_method'} || 'basic'
l!l
00
MC/DC0%0/1
lc $args->{'auth_method'} || 'basic'
lc $args->{'auth_method'}
Truth table0%0/2
A
A= lc $args->{'auth_method'}
Aresult
11
00
105 0 ▶ if ( $method eq 'basic' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'basic') { } 0 0 0
106 0 require MIME::Base64;
107 0 $http->{default_headers}{"Authorization"} = 'Basic '
108 . MIME::Base64::encode_base64( "$args->{user}:$args->{password}",
109 '' );
110 }
111 else {
112 0 die "aborting: unsupported auth method: $method";
113 }
114 }
115 }
116  
117 5 ▶__PACKAGE__->_add_to_plient if $Plient::bundle_mode;
Branch: 1 branch
BranchTrueFalseTotal
if $Plient::bundle_mode 0 5 5
118  
119 5 1;
120  
121 __END__
« Plient/Handler/curl.pm ↑ Summary Plient/Handler/wget.pm »