S3/ResponseParser.pm

filter: Statementstmt 96.8% 61 / 63 Branchbran 75.0% 12 / 16 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 90.0% 90 / 100 CC 11 SCAR 10.1
CC
11
Cov
91%
__ANON__
5.2
__ANON__
3.0
_parse_errors
3.0
CRAP 2.8
« S3/Roles/Request.pm ↑ Summary S3/Request/SetFileContents.pm »
1  
2 package AWS::S3::ResponseParser;
3  
4 6 ▶use constant DEBUG => $ENV{AWS_S3_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use XML::LibXML::XPathContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Log::Any qw( $LOG );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 has 'expect_nothing' => (
11 is => 'ro',
12 isa => 'Bool',
13 required => 1,
14 default => 0,
15 27 ▶ trigger => sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
16 27 my ( $self, $expect_nothing) = @_;
17 27 ▶ if ( $expect_nothing ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expect_nothing) 6 21 27
18 6 my $code = $self->response->code;
19 6 ▶ if ( $code =~ m{^2\d\d} && !$self->response->content ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /^2\d\d/ and not $self->response->content) { } 4 2 6
Condition66%2/3
$code =~ /^2\d\d/ and not $self->response->content
!ll&&!rl&&r
204
MC/DC50%1/2
$code =~ /^2\d\d/ and not $self->response->content
$code =~ /^2\d\d/ not $self->response->content
Truth table66%2/3
A and not(B)
A= $code =~ /^2\d\d/B= not $self->response->content
ABresult
0X0
100
111
20 4 return; # not sure what jdrago wanted this to do originally
21 }
22 else {
23 2 ▶ if ( $self->_parse_errors() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_parse_errors) { } 2 0 2
24 # die $self->friendly_error();
25 }
26 else {
27 0 return;
28 }
29 }
30 }
31 }
32 6 );
33  
34 6 has 'response' => (
35 is => 'ro',
36 isa => 'HTTP::Response',
37 required => 1,
38 );
39  
40 6 has 'type' => (
41 is => 'ro',
42 isa => 'Str',
43 required => 1,
44 );
45  
46 has 'libxml' => (
47 is => 'ro',
48 isa => 'XML::LibXML',
49 required => 1,
50 27 ▶ default => sub { return XML::LibXML->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
51 6 );
52  
53 6 has 'error_code' => (
54 is => 'rw',
55 isa => 'Str',
56 required => 0,
57 );
58  
59 6 has 'error_message' => (
60 is => 'rw',
61 isa => 'Str',
62 required => 0,
63 );
64  
65 has 'xml' => (
66 is => 'ro',
67 isa => 'XML::LibXML::Document',
68 required => 0,
69 lazy => 1,
70 clearer => '_clear_xml',
71 15 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
72 15 my $self = shift;
73  
74 15 my $src = $self->response->content;
75 15 print STDERR ">>> AWS Response:\n", $src, "\n" if DEBUG;
76  
77 15 ▶ return unless $src =~ m/^[[:space:]]*</s;
Branch: 1 branch
BranchTrueFalseTotal
unless $src =~ /^[[:space:]]*</s 0 15 15
78 15 return $self->libxml->parse_string( $src );
79 }
80 6 );
81  
82 has 'xpc' => (
83 is => 'ro',
84 isa => 'XML::LibXML::XPathContext',
85 required => 0,
86 lazy => 1,
87 clearer => '_clear_xpc',
88 6 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
89 6 my $self = shift;
90 6 my $doc = $self->xml;
91 6 ▶ return unless $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc 0 6 6
92  
93 6 my $xpc = XML::LibXML::XPathContext->new( $doc );
94 6 $xpc->registerNs( 's3', 'http://s3.amazonaws.com/doc/2006-03-01/' );
95  
96 6 return $xpc;
97 }
98 6 );
99  
100 has 'friendly_error' => (
101 is => 'ro',
102 isa => 'Maybe[Str]',
103 lazy => 1,
104 required => 0,
105 10 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
106 10 my $s = shift;
107  
108 10 ▶ return unless $s->error_code || $s->error_message;
Branch: 1 branch
BranchTrueFalseTotal
unless $s->error_code or $s->error_message 8 2 10
Condition66%2/3
$s->error_code or $s->error_message
l!l&&r!l&&!r
208
MC/DC50%1/2
$s->error_code or $s->error_message
$s->error_code $s->error_message
Truth table66%2/3
A or B
A= $s->error_codeB= $s->error_message
ABresult
1X1
011
000
109 2 $s->type . " call had errors: [" . $s->error_code . "] " . $s->error_message;
110 }
111 6 );
112  
113 2 ▶sub _parse_errors {
Subroutine _parse_errors: called CC=3 SCAR=11.0
114 2 my $self = shift;
115  
116 2 my $src = $self->response->content;
117  
118 # Do not try to parse non-xml:
119 2 ▶ unless ( $src =~ m/^[[:space:]]*</s ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($src =~ /^[[:space:]]*</s) 1 1 2
120 1 ( my $code = $src ) =~ s/^[[:space:]]*\([0-9]*\).*$/$1/s;
121 1 $LOG->error('Error response from AWS', {code => $code, msg => $src});
122 1 $self->error_code( $code );
123 1 $self->error_message( $src );
124 1 return 1;
125 } # end unless()
126  
127 ## Originally at this point the re-setting of xpc would happen
128 ## Does not seem to be needed but it may be a problem area
129 ## Feel free to delete - Evan Carroll 2012/06/14
130 #### $s->_clear_xpc;
131  
132 1 ▶ if ( $self->xpc->findnodes( "//Error" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->xpc->findnodes("//Error")) 1 0 1
133 1 my $code = $self->xpc->findvalue( "//Error/Code" );
134 1 my $msg = $self->xpc->findvalue( "//Error/Message" );
135 1 $LOG->error('Error response from AWS', {code => $code, msg => $msg});
136 1 $self->error_code( $code );
137 1 $self->error_message( $msg );
138 1 return 1;
139 }
140  
141 0 return 0;
142 }
143  
144 6 __PACKAGE__->meta->make_immutable;
« S3/Roles/Request.pm ↑ Summary S3/Request/SetFileContents.pm »