blib/lib/Langertha/Request/SyncHTTP.pm

filter: Statementstmt 100% 42 / 42 Branchbran 81.8% 18 / 22 Conditioncond 40.0% 4 / 10 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 83.3% 70 / 84 CC 16 SCAR 22.4
CC
16
Cov
86%
do_request
12.5
__ANON__
5.0
BEGIN
1.0
CRAP 9.4
« blib/lib/Langertha/Engine/AKI.pm ↑ Summary blib/lib/Langertha/HTTP/ConnectCheck.pm »
1 package Langertha::Request::SyncHTTP;
2 # ABSTRACT: Synchronous LWP-backed HTTP client satisfying the async do_request contract
3 36 our $VERSION = '0.505';
4 36 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 36 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 36 has user_agent => (
8 is => 'ro',
9 required => 1,
10 );
11  
12  
13 194 ▶sub do_request {
Subroutine do_request: called CC=12 SCAR=25.3
Pod documented
14 194 my ( $self, %args ) = @_;
15 194 my $request = $args{request};
16 194 my $on_header = $args{on_header};
17  
18 194 ▶ if ($on_header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_header) 41 153 194
19 41 my ( $chunk_handler, $header_seen, $callback_error );
20 81 ▶ my $response = $self->user_agent->request($request, sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
21 81 my ( $data, $header_response ) = @_;
22 81 my $ok = eval {
23 81 ▶ unless ($header_seen) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($header_seen) 20 61 81
24 20 $header_seen = 1;
25 20 $chunk_handler = $on_header->($header_response);
26 }
27 81 ▶ $chunk_handler->($data) if $chunk_handler;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk_handler 81 0 81
28 75 1;
29 };
30 81 ▶ return if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 75 6 81
31 # Remember the original exception (objects survive), then die again so
32 # LWP stops reading the body.
33 6 ▶ $callback_error = $@ || "streaming callback died\n";
Condition50%1/2
$@ || "streaming callback died\n"
l!l
60
MC/DC0%0/1
$@ || "streaming callback died\n"
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
34 6 die $callback_error;
35 41 });
36  
37 # LWP catches a die in the content callback (and a mid-body read failure)
38 # and records it as X-Died on an otherwise successful-looking response.
39 # Fail like Net::Async::HTTP does instead of resolving a truncated stream.
40 41 ▶ return Future->fail( $callback_error, http => $response, $request )
Branch: 1 branch
BranchTrueFalseTotal
if defined $callback_error 6 35 41
41 if defined $callback_error;
42 35 ▶ if ( defined( my $died = $response->header('X-Died') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $died = $response->header("X-Died"))) 1 34 35
43 1 ▶ $died .= "\n" unless $died =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $died =~ /\n\z/ 1 0 1
44 1 return Future->fail( $died, http => $response, $request );
45 }
46  
47 # LWP never runs the content callback for a non-success response (the
48 # body is accumulated on the response instead), nor for an internal error
49 # (connection refused, DNS, timeout) or an empty body. Net::Async::HTTP
50 # calls on_header for every response and hands it the body, so do the
51 # same here once the request is done.
52 34 my $ok = eval {
53 34 ▶ unless ($header_seen) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($header_seen) 21 13 34
54 21 $header_seen = 1;
55 21 $chunk_handler = $on_header->($response);
56 21 my $body = $response->content;
57 21 ▶ $chunk_handler->($body) if $chunk_handler && defined $body && length $body;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk_handler and defined $body and length $body 20 1 21
Condition50%3/6
$chunk_handler and defined $body and length $body
!ll&&!rl&&r
0120
$chunk_handler and defined $body
!ll&&!rl&&r
0021
MC/DC33%1/3
$chunk_handler and defined $body and length $body
$chunk_handler defined $body length $body
Truth table50%2/4
A and B and C
A= $chunk_handlerB= defined $bodyC= length $body
ABCresult
0XX0
10X0
1100
1111
58 }
59 34 ▶ $chunk_handler->(undef) if $chunk_handler; # the async end-of-body signal
Branch: 1 branch
BranchTrueFalseTotal
if $chunk_handler 34 0 34
60 34 1;
61 };
62 34 ▶ return Future->fail( $@ || "streaming callback died\n", http => $response, $request )
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 34 34
Condition0%0/2
$@ || "streaming callback died\n"
l!l
00
MC/DC0%0/1
$@ || "streaming callback died\n"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
63 unless $ok;
64 34 return Future->done($response);
65 }
66  
67 153 my $response = $self->user_agent->request($request);
68 153 return Future->done($response);
69 }
70  
71  
72 36 __PACKAGE__->meta->make_immutable;
73  
74  
75 36 1;
76  
77 __END__
« blib/lib/Langertha/Engine/AKI.pm ↑ Summary blib/lib/Langertha/HTTP/ConnectCheck.pm »