blib/lib/HTTP/Request/Diff.pm

filter: Statementstmt 94.0% 175 / 186 Branchbran 69.8% 74 / 106 Conditioncond 62.5% 40 / 64 MC/DCmcdc 29.4% 10 / 34 Subroutinesub 100% 19 / 19 Podpod 40.0% 2 / 5 totaltotal 77.2% 320 / 414 CC 84 SCAR 43.7
CC
84
Cov
81%
diff
120.9
as_table
8.4
fetch_value
8.0
CRAP 78.7
↑ Summary
1 package HTTP::Request::Diff;
2 5 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use Moo 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 our $VERSION = '0.07';
6  
7 5 ▶use experimental 'signatures'; # actually, they are stable but stable.pm doesn't know
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use stable 'postderef';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Algorithm::Diff;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Carp 'croak', 'cluck';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use List::Util 'pairs', 'uniq', 'max';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use CGI::Tiny::Multipart 'parse_multipart_form_data';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use URI::QueryParam; # old versions of URI don't load the functionality automatically
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 require overload; # for checking whether inputs are overloaded
17  
18 =encoding utf-8
19  
20 =head1 NAME
21  
22 HTTP::Request::Diff - create diffs between HTTP requests
23  
24 =head1 SYNOPSIS
25  
26 use HTTP::Request::Common 'GET';
27  
28 my $diff = HTTP::Request::Diff->new(
29 reference => GET('https://example.com/?foo=bar' ),
30 #actual => $req2,
31 skip_headers => \@skip,
32 ignore_headers => \@skip2,
33 mode => 'exact', # default is 'semantic'
34 );
35  
36 my @differences = $diff->diff( GET('https://example.com/' ));
37 say Dumper $differences[0];
38 # {
39 # 'kind' => 'value',
40 # 'type' => 'query.foo',
41 # 'reference' => [
42 # 'bar'
43 # ],
44 # 'actual' => [
45 # undef
46 # ]
47 # }
48 #
49  
50 say $diff->as_table(@differences);
51 # +-----------+-----------+-----------+
52 # | Type | Reference | Actual |
53 # +-----------+-----------+-----------+
54 # | query.foo | bar | <missing> |
55 # +-----------+-----------+-----------+
56  
57  
58 =head1 METHODS
59  
60 =head2 C<< ->new >>
61  
62 my $diff = HTTP::Request::Diff->new(
63 mode => 'semantic',
64 );
65  
66 =head3 Options
67  
68 =over 4
69  
70 =item * C<mode>
71  
72 mode => 'strict',
73  
74 The comparison mode. The default is semantic comparison, which considers some
75 differences insignificant:
76  
77 =over 4
78  
79 =item * The order of HTTP headers
80  
81 =item * The boundary strings of multipart POST requests
82  
83 =item * The order of query parameters
84  
85 =item * The order of form parameters
86  
87 =item * A C<Content-Length: 0> header is equivalent to a missing C<Content-Length> header
88  
89 =back
90  
91 C<strict> mode wants the requests to be as identical as possible.
92 C<lax> mode considers query parameters in the POST body as equivalent.
93  
94 =cut
95  
96 # lax -> parameters may be query or post-parameters
97 # semantic -> many things in requests are equivalent
98 # strict -> requests must be string-identical
99 5 has 'mode' => (
100 is => 'ro',
101 default => 'semantic',
102 );
103  
104 =item * C<reference>
105  
106 (optional) The reference request to compare against. Alternatively pass in
107 the request in the call to C<< ->diff >>.
108  
109 =cut
110  
111 5 has 'reference' => (
112 is => 'ro',
113 );
114  
115 =item * C<skip_headers>
116  
117 skip_headers => ['X-Proxied-For'],
118  
119 List of headers to skip when comparing. Missing headers are not significant.
120  
121 =cut
122  
123 has 'skip_headers' => (
124 is => 'ro',
125 23 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
126 5 );
127  
128 =item * C<ignore_headers>
129  
130 ignore_headers => ['Accept-Encoding'],
131  
132 List of headers to ignore when comparing. Missing headers are significant.
133  
134 =cut
135  
136 has 'ignore_headers' => (
137 is => 'ro',
138 21 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
139 5 );
140  
141 =item * C<canonicalize>
142  
143 Callback to canonicalize a request. The request will be passed in unmodified
144 either as a string or a L<HTTP::Request>.
145  
146 =cut
147  
148 5 has 'canonicalize' => (
149 is => 'ro',
150 );
151  
152 =item * C<compare>
153  
154 Arrayref of things to compare.
155  
156 =cut
157  
158 has 'compare' => (
159 is => 'ro',
160 24 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
161 return [
162 24 request => 'method',
163 uri => 'host',
164 uri => 'port',
165 uri => 'path',
166 ];
167 },
168 5 );
169  
170 =item * C<warn_on_newlines>
171  
172 (optional) If we should output warnings when we receive C< \n > delimited input
173 instead of C< \r\n >. This mostly happens when input is read from text files
174 for regression test.
175  
176 Default is true.
177  
178 =back
179  
180 =cut
181  
182 5 has 'warn_on_newlines' => (
183 is => 'rw',
184 default => 1
185 );
186  
187 288 ▶sub fetch_value($self, $req, $item, $req_params=undef) {
Subroutine fetch_value: called CC=8 SCAR=20.8
Pod undocumented
188 288 my $obj;
189 288 ▶ if( $item->key eq 'request' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($item->key eq 'request') { } 92 196 288
elsif ($item->key eq 'headers') { } 28 168 196
elsif ($item->key eq 'query') { } 16 152 168
elsif ($item->key eq 'uri') { } 144 8 152
elsif ($item->key eq 'form') { } 8 0 8
190 92 my $v = $item->value;
191 92 return $req->$v;
192  
193 } elsif( $item->key eq 'headers' ) {
194 28 return $req->headers->header( $item->value );
195  
196 } elsif( $item->key eq 'query' ) {
197 16 return [ $req->uri->query_param( $item->value )];
198  
199 } elsif( $item->key eq 'uri' ) {
200 144 my $u = $req->uri;
201 144 ▶ if( my $c = $u->can( $item->value )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $u->can($item->value)) { } 76 68 144
202 76 return $c->($u)
203 } else {
204 return
205 68 }
206  
207 } elsif( $item->key eq 'form' ) {
208 8 return $req_params->{ $item->value };
209  
210 } else {
211 0 croak sprintf "Unknown key '%s'", $item->key;
212 }
213  
214 }
215  
216 4 ▶sub get_form_parameters( $self, $req ) {
Subroutine get_form_parameters: called CC=4 SCAR=14.2
Pod undocumented
217 4 my(undef, $boundary) = $req->headers->content_type;
218 4 my $str = $req->content;
219 4 $boundary =~ s!^boundary=!!;
220  
221 4 my %res;
222 4 my $res = parse_multipart_form_data( \$str, length($str), $boundary);
223 4 ▶ if( ! $res ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 4 4
224 0 croak "Malformed form data";
225 }
226 4 for my $p ($res->@*) {
227 8 ▶ $res{ $p->{name} } //= [];
Condition50%1/2
$res{$p->{"name"}} //= []
l!l
08
MC/DC0%0/1
$res{$p->{"name"}} //= []
$res{$p->{"name"}}
Truth table50%1/2
A
A= $res{$p->{"name"}}
Aresult
11
00
228 8 push $res{ $p->{name}}->@*, $p->{content};
229 };
230 4 return \%res;
231 }
232  
233 48 ▶sub get_request_header_names( $self, $req ) {
Subroutine get_request_header_names: called CC=3 SCAR=11.0
Pod undocumented
234 48 ▶ if( $req =~ /\n/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req =~ /\n/u) { } 41 7 48
235 41 ▶ my( $header ) = $req =~ m/^(.*?)\r?\n\r?\n/ms
Branch: 1 branch
BranchTrueFalseTotal
my($header) = $req =~ /^(.*?)\r?\n\r?\n/msu or croak("No header in request <$req>") 41 0 41
236 or croak "No header in request <$req>";
237 41 my @headers = ($header =~ /^([A-Za-z][A-Za-z\d-]+):/mg);
238 41 return @headers;
239 } else {
240 return
241 7 }
242 }
243  
244 =head2 C<< ->diff >>
245  
246 my @diff = $diff->diff( $reference, $actual, %options );
247 my @diff = $diff->diff( $actual, %options );
248  
249 Performs the diff and returns an array of hashrefs with differences.
250  
251 =cut
252  
253 24 ▶sub diff( $self, $actual_or_reference, $actual=undef, %options ) {
Subroutine diff: called CC=65 SCAR=47.9
Pod documented
254 24 ▶ $options{ warn_on_newlines } //= $self->warn_on_newlines;
Condition33%1/3
$options{"warn_on_newlines"} //= $self->warn_on_newlines
l!l&&r!l&&!r
0240
MC/DC0%0/2
$options{"warn_on_newlines"} //= $self->warn_on_newlines
$options{"warn_on_newlines"} $self->warn_on_newlines
Truth table33%1/3
A //= B
A= $options{"warn_on_newlines"}B= $self->warn_on_newlines
ABresult
1X1
011
000
255 24 ▶ $options{ mode } //= $self->mode;
Condition33%1/3
$options{"mode"} //= $self->mode
l!l&&r!l&&!r
0240
MC/DC0%0/2
$options{"mode"} //= $self->mode
$options{"mode"} $self->mode
Truth table33%1/3
A //= B
A= $options{"mode"}B= $self->mode
ABresult
1X1
011
000
256  
257 # Downconvert things to strings, unless we have strings already
258 # reparse into HTTP::Request for easy structural checks
259  
260 24 my $ref;
261 24 ▶ if( $actual ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($actual) { } 7 17 24
elsif ($actual_or_reference) { } 17 0 17
262 7 ▶ $ref = $actual_or_reference
Branch: 1 branch
BranchTrueFalseTotal
$ref = $actual_or_reference or croak("Need a reference request") 7 0 7
263 or croak "Need a reference request";
264 } elsif( $actual_or_reference ) {
265 17 ▶ $ref = $self->reference
Branch: 1 branch
BranchTrueFalseTotal
$ref = $self->reference or croak("Need a reference request") 17 0 17
266 or croak "Need a reference request";
267 17 ▶ $actual = $actual_or_reference // $self->actual
Branch: 1 branch
BranchTrueFalseTotal
$actual = $actual_or_reference // $self->actual or croak("Need an actual request to diff") 17 0 17
Condition33%1/3
$actual_or_reference // $self->actual
l!l&&r!l&&!r
1700
MC/DC0%0/2
$actual_or_reference // $self->actual
$actual_or_reference $self->actual
Truth table33%1/3
A // B
A= $actual_or_referenceB= $self->actual
ABresult
1X1
011
000
268 or croak "Need an actual request to diff";
269 } else {
270 0 ▶ $ref = $self->reference
Branch: 1 branch
BranchTrueFalseTotal
$ref = $self->reference or croak("Need a reference request") 0 0 0
271 or croak "Need a reference request";
272 0 ▶ $actual = $self->actual
Branch: 1 branch
BranchTrueFalseTotal
$actual = $self->actual or croak("Need an actual request to diff") 0 0 0
273 or croak "Need an actual request to diff";
274 }
275  
276 24 ▶ if( my $c = $self->canonicalize ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $self->canonicalize) 0 24 24
277 0 ▶ $ref = $c->( $ref )
Branch: 1 branch
BranchTrueFalseTotal
$ref = &$c($ref) or croak("Request canonicalizer returned no request") 0 0 0
278 or croak "Request canonicalizer returned no request";
279 0 ▶ $actual = $c->( $actual )
Branch: 1 branch
BranchTrueFalseTotal
$actual = &$c($actual) or croak("Request canonicalizer returned no request") 0 0 0
280 or croak "Request canonicalizer returned no request";
281 };
282  
283 # maybe cache that in our builder?!
284 24 my %ignore_diff = map {; "headers.$_" => 1 } $self->ignore_headers->@*;
285  
286 # maybe cache that in our builder?!
287 24 my %skip_header = map { $_ => 1 } $self->skip_headers->@*;
288  
289 24 for ($ref, $actual) {
290 48 ▶ if( ref $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) 14 34 48
291 14 ▶ if( $_->can( 'as_string' )) {
Branch: 3 branches
BranchTrueFalseTotal
if ($_->can('as_string')) { } 14 0 14
elsif ($_->can('to_string')) { } 0 0 0
elsif (overload::Method($_, '""')) { } 0 0 0
292 14 $_ = $_->as_string("\r\n");
293  
294 } elsif( $_->can('to_string' )) {
295 0 $_ = $_->to_string("\r\n");
296  
297 } elsif( overload::Method($_, '""')) {
298 0 $_ = "$_";
299 } else {
300 0 croak "Don't know how to convert $_ to a string";
301 }
302 }
303 };
304  
305 24 ▶ if( $options{ warn_on_newlines }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"warn_on_newlines"}) 24 0 24
306 24 ▶ cluck 'Reference input has bare newlines in header, not crlf'
Branch: 1 branch
BranchTrueFalseTotal
if $ref =~ /\A(.*?)[\r]?\n[\r]?\n/u and $1 =~ /[^\r]\n/u 0 24 24
Condition66%2/3
$ref =~ /\A(.*?)[\r]?\n[\r]?\n/u and $1 =~ /[^\r]\n/u
!ll&&!rl&&r
13110
MC/DC0%0/2
$ref =~ /\A(.*?)[\r]?\n[\r]?\n/u and $1 =~ /[^\r]\n/u
$ref =~ /\A(.*?)[\r]?\n[\r]?\n/u $1 =~ /[^\r]\n/u
Truth table66%2/3
A and B
A= $ref =~ /\A(.*?)[\r]?\n[\r]?\n/uB= $1 =~ /[^\r]\n/u
ABresult
0X0
100
111
307 if $ref =~ /\A(.*?)[\r]?\n[\r]?\n/ and $1 =~ /[^\r]\n/;
308 24 ▶ cluck 'Actual input has bare newlines in header, not crlf'
Branch: 1 branch
BranchTrueFalseTotal
if $actual =~ /\A(.*?)[\r]?\n[\r]?\n/u and $1 =~ /[^\r]\n/u 0 24 24
Condition66%2/3
$actual =~ /\A(.*?)[\r]?\n[\r]?\n/u and $1 =~ /[^\r]\n/u
!ll&&!rl&&r
13110
MC/DC0%0/2
$actual =~ /\A(.*?)[\r]?\n[\r]?\n/u and $1 =~ /[^\r]\n/u
$actual =~ /\A(.*?)[\r]?\n[\r]?\n/u $1 =~ /[^\r]\n/u
Truth table66%2/3
A and B
A= $actual =~ /\A(.*?)[\r]?\n[\r]?\n/uB= $1 =~ /[^\r]\n/u
ABresult
0X0
100
111
309 if $actual =~ /\A(.*?)[\r]?\n[\r]?\n/ and $1 =~ /[^\r]\n/;
310 };
311  
312 24 my $r_ref = HTTP::Request->parse( $ref );
313 24 my $r_actual = HTTP::Request->parse( $actual );
314  
315 24 my @diff;
316  
317 # get query parameter separator, and check these (strict)
318 24 ▶ if( $options{ mode } eq 'strict' and my $q = $r_ref->uri->query ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"mode"} eq "strict" and my $q = $r_ref->uri->query) 1 23 24
Condition100%3/3
$options{"mode"} eq "strict" and my $q = $r_ref->uri->query
!ll&&!rl&&r
2211
MC/DC100%2/2
$options{"mode"} eq "strict" and my $q = $r_ref->uri->query
$options{"mode"} eq "strict" my $q = $r_ref->uri->query
Truth table100%3/3
A and B
A= $options{"mode"} eq "strict"B= my $q = $r_ref->uri->query
ABresult
0X0
100
111
319 1 ▶ if( $q =~ /([&;])/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q =~ /([&;])/u) 1 0 1
320 1 my $query_separator = $1;
321 1 ▶ if( my $q2 = $r_actual->uri->query ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $q2 = $r_actual->uri->query) 1 0 1
322 1 ▶ if( $q2 =~ /([&;])/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q2 =~ /([&;])/u) 1 0 1
323 1 ▶ if( $1 ne $query_separator ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 ne $query_separator) 1 0 1
324 1 push @diff, {
325 reference => $q,
326 actual => $q2,
327 type => 'meta.query_separator',
328 kind => 'value',
329 };
330 }
331 }
332 }
333 }
334 };
335  
336 24 my @ref_header_order = $self->get_request_header_names( $ref );
337 24 my @actual_header_order = $self->get_request_header_names( $actual );
338  
339 14 my @headers = map {; ("headers", $_) }
340 24 grep { ! $skip_header{ $_ } }
341 uniq( @ref_header_order,
342 @actual_header_order
343 );
344  
345 24 my @query_params = map {; ("query", $_) }
346 uniq( $r_ref->uri->query_param,
347 $r_actual->uri->query_param,
348 );
349 24 my @form_params;
350 24 my ($ref_params, $actual_params);
351 24 ▶ if( $self->mode eq 'semantic'
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "semantic" or $self->mode eq "lax") 22 2 24
Condition66%2/3
$self->mode eq "semantic" or $self->mode eq "lax"
l!l&&r!l&&!r
2202
MC/DC50%1/2
$self->mode eq "semantic" or $self->mode eq "lax"
$self->mode eq "semantic" $self->mode eq "lax"
Truth table66%2/3
A or B
A= $self->mode eq "semantic"B= $self->mode eq "lax"
ABresult
1X1
011
000
352 or $self->mode eq 'lax' ) {
353  
354 22 ▶ if( $r_ref->headers->content_type eq 'multipart/form-data'
Branch: 2 branches
BranchTrueFalseTotal
if ($r_ref->headers->content_type eq 'multipart/form-data' and $r_actual->headers->content_type eq 'multipart/form-data') { } 2 20 22
elsif ($r_actual->headers->content_type eq 'application/x-www-form-urlencoded' and $r_actual->headers->content_type eq 'application/x-www-form-urlencoded') { } 1 38 39
Condition66%2/3
$r_ref->headers->content_type eq "multipart/form-data" and $r_actual->headers->content_type eq "multipart/form-data"
!ll&&!rl&&r
2002
MC/DC50%1/2
$r_ref->headers->content_type eq "multipart/form-data" and $r_actual->headers->content_type eq "multipart/form-data"
$r_ref->headers->content_type eq "multipart/form-data" $r_actual->headers->content_type eq "multipart/form-data"
Truth table66%2/3
A and B
A= $r_ref->headers->content_type eq "multipart/form-data"B= $r_actual->headers->content_type eq "multipart/form-data"
ABresult
0X0
100
111
355 and $r_actual->headers->content_type eq 'multipart/form-data'
356 ) {
357 # We've checked the content type already, we can ignore the boundary
358 # value for semantic checks
359 2 $ignore_diff{ 'headers.Content-Type' } = 1;
360  
361 # The content length will likely also differ, as we use different
362 # sizes for the boundary
363 2 $ignore_diff{ 'headers.Content-Length' } = 1;
364  
365 2 $ref_params = $self->get_form_parameters( $r_ref );
366 2 $actual_params = $self->get_form_parameters( $r_actual );
367  
368 2 @form_params = map {; ("form", $_) }
369 uniq( keys( $ref_params->%* ),
370 keys( $actual_params->%*),
371 );
372  
373 ▶ } elsif( $r_actual->headers->content_type eq 'application/x-www-form-urlencoded'
Condition66%2/3
$r_actual->headers->content_type eq "application/x-www-form-urlencoded" and $r_actual->headers->content_type eq "application/x-www-form-urlencoded"
!ll&&!rl&&r
1901
MC/DC50%1/2
$r_actual->headers->content_type eq "application/x-www-form-urlencoded" and $r_actual->headers->content_type eq "application/x-www-form-urlencoded"
$r_actual->headers->content_type eq "application/x-www-form-urlencoded" $r_actual->headers->content_type eq "application/x-www-form-urlencoded"
Truth table66%2/3
A and B
A= $r_actual->headers->content_type eq "application/x-www-form-urlencoded"B= $r_actual->headers->content_type eq "application/x-www-form-urlencoded"
ABresult
0X0
100
111
374 and $r_actual->headers->content_type eq 'application/x-www-form-urlencoded'
375 ) {
376 # We've checked the content type already, we can ignore the boundary
377 # value for semantic checks
378 1 $ignore_diff{ 'headers.Content-Type' } = 1;
379  
380 # Handle %20 vs +
381 1 my $force_percent_encoding = ($r_ref->headers->content_length != $r_actual->headers->content_length);
382  
383 1 ▶ if( $force_percent_encoding ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force_percent_encoding) 1 0 1
384 1 for my $req ($r_ref, $r_actual) {
385 2 my $body = $req->content();
386 2 ▶ if( $body =~ s!\+!%20!g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ s/\+/%20/gu) 0 2 2
387 0 $ignore_diff{ 'header.Content-Length' } = 1;
388 0 $req->content( $body );
389 }
390 };
391 };
392  
393 }
394 };
395 24 my @check = ($self->compare->@*, @headers, @query_params, @form_params);
396  
397 24 ▶ if( !@form_params ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@form_params) 22 2 24
398 22 push @check, 'request' => 'content';
399 };
400  
401 24 ▶ if( $self->mode eq 'strict' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq "strict") 2 22 24
402 2 push @check, 'request' => 'header_order';
403 }
404  
405 # also, we should check for cookies
406  
407 24 for my $p (pairs @check) {
408  
409 146 my $ref_v;
410 my $actual_v;
411  
412 146 ▶ if( $p->value eq 'header_order' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->value eq 'header_order') { } 2 144 146
413 2 $ref_v = \@ref_header_order;
414 2 $actual_v = \@actual_header_order;
415  
416 } else {
417 144 $ref_v = $self->fetch_value( $r_ref, $p, $ref_params );
418 144 $actual_v = $self->fetch_value( $r_actual, $p, $actual_params );
419 }
420  
421 146 my $type = sprintf( '%s.%s', @$p );
422  
423 146 ▶ if( (defined $ref_v xor defined $actual_v)) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $ref_v xor defined $actual_v) { } 4 142 146
elsif (ref $ref_v) { } 14 128 142
elsif (not defined $ref_v || defined $actual_v) { } 34 94 128
elsif ($ref_v ne $actual_v) { } 12 82 94
Condition100%4/4
defined $ref_v xor defined $actual_v
l&&rl&&!r!l&&r!l&&!r
1082234
MC/DC100%2/2
defined $ref_v xor defined $actual_v
defined $ref_v defined $actual_v
Truth table100%4/4
A xor B
A= defined $ref_vB= defined $actual_v
ABresult
110
101
011
000
424 # One is missing
425  
426 # semantic/lax: If Content-Length is missing, it is equivalent
427 # to Content-Length: 0
428  
429 4 ▶ if( ($self->mode eq 'lax' or $self->mode eq 'semantic')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mode eq 'lax' || $self->mode eq 'semantic' and $type eq 'headers.Content-Length' and ($ref_v // 0) == 0 and ($actual_v // 0) == 0) { } 0 4 4
Condition56%9/16
$self->mode eq "lax" || $self->mode eq "semantic" and $type eq "headers.Content-Length" and ($ref_v // 0) == 0 and ($actual_v // 0) == 0
!ll&&!rl&&r
310
$self->mode eq "lax" || $self->mode eq "semantic" and $type eq "headers.Content-Length" and ($ref_v // 0) == 0
!ll&&!rl&&r
301
$self->mode eq "lax" || $self->mode eq "semantic" and $type eq "headers.Content-Length"
!ll&&!rl&&r
031
$self->mode eq "lax" or $self->mode eq "semantic"
l!l&&r!l&&!r
040
$ref_v // 0
l!l
01
$actual_v // 0
l!l
10
MC/DC0%0/5
$self->mode eq "lax" || $self->mode eq "semantic" and $type eq "headers.Content-Length" and ($ref_v // 0) == 0 and ($actual_v // 0) == 0
$self->mode eq "lax" $self->mode eq "semantic" $type eq "headers.Content-Length" $ref_v $actual_v
Truth table11%1/9
A or B and C and D and E
A= $self->mode eq "lax"B= $self->mode eq "semantic"C= $type eq "headers.Content-Length"D= $ref_vE= $actual_v
ABCDEresult
00XXX0
1X0XX0
010XX0
1X10X0
0110X0
1X1100
011100
1X1111
011111
430 and $type eq 'headers.Content-Length'
431 and ($ref_v // 0 )== 0 and ($actual_v // 0) == 0) {
432 # ignore
433 } else {
434  
435 4 push @diff, {
436 reference => $ref_v,
437 actual => $actual_v,
438 type => $type,
439 kind => 'missing',
440 };
441 };
442  
443 } elsif( ref $ref_v ) {
444 # Here we have a list of values, let's check if the lists
445 # of values match
446 14 my $diff = Algorithm::Diff->new( $ref_v, $actual_v );
447 14 my $diff_type;
448 my @ref;
449 14 my @act;
450  
451 14 ▶ while( $diff->Next() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff->Next) 18 14 32
452 18 ▶ if( $diff->Same() ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($diff->Same) { } 8 10 18
elsif (not $diff->Items(2)) { } 4 6 10
elsif (not $diff->Items(1)) { } 3 3 6
453 8 push @ref, $diff->Items(1);
454 8 push @act, $diff->Items(2);
455  
456 } elsif( !$diff->Items(2) ) {
457 4 push @ref, $diff->Items(1);
458 4 push @act, (undef) x scalar($diff->Items(1));
459 4 ▶ $diff_type //= 'missing';
Condition50%1/2
$diff_type //= "missing"
l!l
04
MC/DC0%0/1
$diff_type //= "missing"
$diff_type
Truth table50%1/2
A
A= $diff_type
Aresult
11
00
460  
461 } elsif( !$diff->Items(1) ) {
462 3 push @ref, (undef) x scalar($diff->Items(2));
463 3 push @act, $diff->Items(2);
464 3 ▶ $diff_type //= 'missing';
Condition100%2/2
$diff_type //= "missing"
l!l
21
MC/DC100%1/1
$diff_type //= "missing"
$diff_type
Truth table100%2/2
A
A= $diff_type
Aresult
11
00
465  
466 } else {
467 3 my $count = max( scalar $diff->Items(1), scalar $diff->Items(2));
468 3 push @ref, $diff->Items(1);
469 3 push @ref, (undef) x (scalar($diff->Items(2)) - $count);
470 3 push @act, $diff->Items(2);
471 3 push @act, (undef) x (scalar($diff->Items(1)) - $count);
472  
473 3 $diff_type = 'value';
474 }
475 };
476  
477 14 ▶ if( $diff_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff_type) 8 6 14
478 # Do we really want to downconvert to strings?!
479 #my $ref_diff = join "\n", @ref;
480 #my $actual_diff = join "\n", @act;
481 8 my $ref_diff = \@ref;
482 8 my $actual_diff = \@act;
483 8 push @diff, {
484 reference => $ref_diff,
485 actual => $actual_diff,
486 type => sprintf( '%s.%s', @$p ),
487 kind => $diff_type,
488 };
489 };
490  
491 ▶ } elsif( !defined $ref_v and !defined $actual_v ) {
Condition66%2/3
defined $ref_v || defined $actual_v
l!l&&r!l&&!r
94034
MC/DC50%1/2
defined $ref_v || defined $actual_v
defined $ref_v defined $actual_v
Truth table66%2/3
A || B
A= defined $ref_vB= defined $actual_v
ABresult
1X1
011
000
492 # neither value exists
493  
494 } elsif( $ref_v ne $actual_v ) {
495 12 my $type = sprintf( '%s.%s', @$p );
496 12 ▶ if( ! $ignore_diff{ $type }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ignore_diff{$type}) 9 3 12
497 9 push @diff, {
498 reference => $ref_v,
499 actual => $actual_v,
500 type => $type,
501 kind => 'value',
502 };
503 }
504 };
505 }
506 # parameters switching between body and query string
507 # if( $ref->headers->content_type eq '' ) {
508 # compare form values
509 # } else {
510 # compare request body
511 # }
512  
513 24 return @diff;
514 }
515  
516 =head2 C<< ->as_table( @diff ) >>
517  
518 my @diff = $diff->diff( $request1, $request2 );
519 print $diff->as_table( @diff );
520 # +-----------------+-----------+--------+
521 # | Type | Reference | Actual |
522 # +-----------------+-----------+--------+
523 # | request.content | Ümloud | Umloud |
524 # +-----------------+-----------+--------+
525  
526 Renders a diff as a table, using L<Term::Table>.
527  
528 =cut
529  
530 4 ▶sub as_table($self,@diff) {
Subroutine as_table: called CC=8 SCAR=21.3
Pod documented
531 4 require Term::Table;
532  
533 4 ▶ if( @diff ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@diff) 4 0 4
534 my $t = Term::Table->new(
535 allow_overflow => 1,
536 header => ['Type', 'Reference', 'Actual'],
537 rows => [
538 4 map {[ $_->{type},
539 1 ▶ ref $_->{reference} ? join "\n", map { $_ // '<missing>' } $_->{reference}->@* : $_->{reference} // '<missing>',
Branch: 1 branch
BranchTrueFalseTotal
ref $_->{'actual'} ? : 1 3 4
Condition50%2/4
$_ // "<missing>"
l!l
10
$_->{"reference"} // "<missing>"
l!l
30
MC/DC0%0/1
$_ // "<missing>"
$_
MC/DC0%0/1
$_->{"reference"} // "<missing>"
$_->{"reference"}
Truth table50%1/2
A
A= $_
Aresult
11
00
Truth table50%1/2
A
A= $_->{"reference"}
Aresult
11
00
540 4 ▶ ref $_->{actual} ? join "\n", map { $_ // '<missing>' } $_->{actual}->@* : $_->{actual} // '<missing>',
Branch: 1 branch
BranchTrueFalseTotal
ref $_->{'reference'} ? : 1 3 4
Condition75%3/4
$_ // "<missing>"
l!l
01
$_->{"actual"} // "<missing>"
l!l
21
MC/DC0%0/1
$_ // "<missing>"
$_
MC/DC100%1/1
$_->{"actual"} // "<missing>"
$_->{"actual"}
Truth table50%1/2
A
A= $_
Aresult
11
00
Truth table100%2/2
A
A= $_->{"actual"}
Aresult
11
00
541 ]} @diff
542 ],
543 );
544 4 return join "\n", $t->render;
545 };
546 }
547  
548 5 1;
549  
550 __END__
↑ Summary