Proxy/CompareParams.pm

filter: Statementstmt 98.2% 113 / 115 Branchbran 92.3% 24 / 26 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 15 / 15 Podpod 80.0% 4 / 5 totaltotal 93.5% 160 / 171 CC 17 SCAR 9.2
CC
17
Cov
95%
cmp_request_params
4.0
convert_har_params_to_hash
3.1
generate_comparison_sub
3.0
CRAP 2.5
« Proxy.pm ↑ Summary
1 package Browsermob::Proxy::CompareParams;
2 2 $Browsermob::Proxy::CompareParams::VERSION = '0.16';
3 # ABSTRACT: Look for a request with the specified matching request params
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 require Exporter;
9 2 our @ISA = qw/Exporter/;
10 2 our @EXPORT = qw/cmp_request_params/;
11 2 our @EXPORT_OK = qw/convert_har_params_to_hash
12 replace_placeholder_values
13 collect_query_param_keys/;
14  
15  
16  
17 sub cmp_request_params {
18 31 ▶ my ($got, $expected, $user_cmp) = @_;
Subroutine cmp_request_params: called CC=4 SCAR=13.9
Pod documented
19 31 my $got_hash = convert_har_params_to_hash($got);
20 31 my $compare = generate_comparison_sub($user_cmp);
21  
22 # Start by assuming that we can't find any of our expected keys
23 31 my @least_missing = keys %{ $expected };
24 31 my $closest = {};
25  
26 my @matched = grep {
27 75 my $actual_params = $_;
28  
29 # The @missing array will contain the expected keys that
30 # either do not exist in actual params, or they do exist but
31 # the values aren't the same.
32 my @missing = grep {
33 137 my $key = $_;
34 # Negative asserts ( "!missing", "!not_equal:to_this" )
35 # need to be handled differently
36 137 ▶ if ( _is_negative_assert($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_negative_assert($key)) { } 39 98 137
37 39 _assert_negative_kv($key, $expected->{$key}, $actual_params, $compare);
38 }
39 else {
40 98 _assert_positive_kv($key, $expected->{$key}, $actual_params, $compare);
41 }
42 75 } keys %{ $expected };
43  
44 # We need to keep track of the closest match we've found so
45 # far so we can tell the caller about it when we're done
46 75 ▶ if (scalar @missing < scalar @least_missing) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @missing < scalar @least_missing) 25 50 75
47 25 @least_missing = @missing;
48  
49 $closest = { map {
50 25 $_ =~ s/^!//;
51 13 $_ => $actual_params->{$_}
52 } @least_missing };
53 }
54  
55 # @missing will be empty for a successful request/assert
56 # match.
57 75 ! ( scalar @missing )
58 31 } @{ $got_hash };
59  
60 31 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 8 23 31
61 # In list context, provide the closest match for context on
62 # the caller's side
63 my $missing = { map {
64 8 $_ => $expected->{$_}
65 } @least_missing };
66 8 return (scalar @matched, $missing, $closest);
67 }
68 else {
69 23 return scalar @matched;
70 }
71 }
72  
73 sub _is_negative_assert {
74 137 ▶ my ($key) = @_;
Subroutine _is_negative_assert: called CC=1 SCAR=0.0
75  
76 137 return $key =~ /^!/;
77 }
78  
79 sub _assert_negative_kv {
80 39 ▶ my ($key, $expected, $actual_params, $compare) = @_;
Subroutine _assert_negative_kv: called CC=2 SCAR=6.9
81  
82 # Negative asserts come in two flavors: either the key must not
83 # exist at all, or the key must exist, but its value cannot match
84 # the expected.
85  
86 39 ▶ if ($expected eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($expected eq '') { } 23 16 39
87 23 return _assert_missing_key( $key, $actual_params );
88 }
89 else {
90 16 return _assert_different_value( $key, $expected, $actual_params, $compare );
91 }
92 }
93  
94 sub _assert_different_value {
95 16 ▶ my ($key, $expected, $actual_params, $compare) = @_;
Subroutine _assert_different_value: called CC=2 SCAR=6.9
96 16 my $actual_key = $key;
97 16 $actual_key =~ s/^!//;
98  
99 16 ▶ if ( exists $actual_params->{$actual_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $actual_params->{$actual_key}) { } 9 7 16
100 # At this point, we know the key exists, and we just want to
101 # make sure we _dont_ match our assertion. Which is to say,
102 # the exact opposite of a positive kv assertion.
103 9 return ! _assert_positive_kv( $actual_key, $expected, $actual_params, $compare);
104 }
105 else {
106 # An assert like "!missing: not this" requires that the key
107 # exists and is not equal to the value. If the key does not
108 # even exist, that is bad; we assert that it must exist.
109 7 return 'needs to exist';
110 }
111 }
112  
113 sub _assert_missing_key {
114 23 ▶ my ($key, $actual_params) = @_;
Subroutine _assert_missing_key: called CC=2 SCAR=6.9
115 # The key looks like "!query", but the actual key we are
116 # interested in is "query".
117 23 my $actual_key = $key;
118 23 $actual_key =~ s/^!//;
119  
120 23 ▶ if (exists $actual_params->{$actual_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $actual_params->{$actual_key}) { } 9 14 23
121 # We're asserting that the key is not present. Since we've
122 # found it, that's bad; the grep up in cmp_request_params
123 # expects truthy values to indicate something bad.
124 9 return 'found';
125 }
126 else {
127 # The key isn't in the actual params, so we're good! False
128 # values indicate that everything is okay.
129 14 return '';
130 }
131 }
132  
133 sub _assert_positive_kv {
134 107 ▶ my ($key, $expected, $actual_params, $compare) = @_;
Subroutine _assert_positive_kv: called CC=3 SCAR=11.0
135  
136 # Start off assuming that the expected key is missing from the
137 # actual params.
138 107 my $ret = 'missing';
139  
140 # The expected key must exist in the actual params...
141 107 ▶ if ( exists $actual_params->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $actual_params->{$key}) 56 51 107
142 56 my $got = $actual_params->{$key};
143 # and the expected key's value must match the actual param's
144 # key's value.
145 56 ▶ if ( $compare->( $got, $expected ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$compare($got, $expected)) 42 14 56
146 42 $ret = '';
147 }
148 }
149  
150 # Otherwise, we've initialized $ret as missing so we're good to go.
151 107 return $ret;
152 }
153  
154  
155 sub convert_har_params_to_hash {
156 36 ▶ my ($har_or_requests) = @_;
Subroutine convert_har_params_to_hash: called CC=3 SCAR=11.4
Pod documented
157  
158 36 my $requests;
159 36 ▶ if (ref($har_or_requests) eq 'HASH' && exists $har_or_requests->{log}->{entries}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $har_or_requests eq 'HASH' and exists $har_or_requests->{'log'}{'entries'}) { } 0 36 36
Condition33%1/3
ref $har_or_requests eq "HASH" and exists $har_or_requests->{"log"}{"entries"}
!ll&&!rl&&r
3600
MC/DC0%0/2
ref $har_or_requests eq "HASH" and exists $har_or_requests->{"log"}{"entries"}
ref $har_or_requests eq "HASH" exists $har_or_requests->{"log"}{"entries"}
Truth table33%1/3
A and B
A= ref $har_or_requests eq "HASH"B= exists $har_or_requests->{"log"}{"entries"}
ABresult
0X0
100
111
160 0 $requests = $har_or_requests->{log}->{entries};
161 }
162 else {
163 36 $requests = $har_or_requests;
164 }
165  
166 my $hash = [
167 map {
168 85 my $params = $_->{request}->{queryString};
169 my $pairs = { map {
170 85 $_->{name} => $_->{value}
171 139 } @$params };
172  
173 85 $pairs
174 36 } @{ $requests }
175 ];
176  
177 36 return $hash;
178 }
179  
180 sub generate_comparison_sub {
181 31 ▶ my ($user_comparison) = @_;
Subroutine generate_comparison_sub: called CC=3 SCAR=11.1
Pod undocumented
182 31 ▶ my $string_equality = sub { $_[0] eq $_[1] };
Subroutine __ANON__: called CC=1 SCAR=0.0
183  
184 31 ▶ if (! defined $user_comparison) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $user_comparison) 27 4 31
185 27 return $string_equality;
186 }
187  
188 4 my $ref = ref($user_comparison);
189 4 ▶ if ($ref ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref ne "CODE") 0 4 4
190 0 croak 'We expected your custom comparison to be a CODEREF, not a ' . $ref . '!';
191 }
192  
193 return sub {
194 5 ▶ my ($got, $expected) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.1
195  
196 5 ▶ return $string_equality->($got, $expected) || $user_comparison->($got, $expected);
Condition66%2/3
&$string_equality($got, $expected) || &$user_comparison($got, $expected)
l!l&&r!l&&!r
041
MC/DC50%1/2
&$string_equality($got, $expected) || &$user_comparison($got, $expected)
&$string_equality($got, $expected) &$user_comparison($got, $expected)
Truth table66%2/3
A || B
A= &$string_equality($got, $expected)B= &$user_comparison($got, $expected)
ABresult
1X1
011
000
197 4 };
198  
199 }
200  
201  
202 sub replace_placeholder_values {
203 4 ▶ my ($requests, $assert) = @_;
Subroutine replace_placeholder_values: called CC=3 SCAR=11.0
Pod documented
204  
205 my $mutated = { map {
206 7 my ($key, $value) = ($_, $assert->{$_});
207 7 ▶ if ($value !~ /^ *: */) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $value =~ /^ *: */) { } 4 3 7
208 4 $key => $value
209 }
210 else {
211 3 my $replacement_key = $value;
212 3 $replacement_key =~ s/^ *: *//;
213  
214 3 my $actual_keys = collect_query_param_keys($requests);
215 my $found_existing_key = scalar(
216 3 grep { $_ eq $replacement_key } @{ $actual_keys }
217 );
218 3 ▶ if ($found_existing_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found_existing_key) { } 2 1 3
219 2 $key => $assert->{$replacement_key};
220 }
221 else {
222 1 $key => $value
223 }
224 }
225  
226 4 } keys %{ $assert } };
227  
228 4 return $mutated;
229 }
230  
231  
232 sub collect_query_param_keys {
233 4 ▶ my ($requests) = @_;
Subroutine collect_query_param_keys: called CC=2 SCAR=6.9
Pod documented
234  
235 4 my $kv_params = convert_har_params_to_hash($requests);
236  
237 4 my $keys = {};
238 4 foreach my $param_pairs (@{ $kv_params }) {
239 8 map { $keys->{$_}++ } keys %{ $param_pairs };
240 }
241  
242 4 return [ sort keys %{ $keys } ];
243 }
244  
245 2 1;
246  
247 __END__
248  
249 =pod
250  
251 =encoding UTF-8
252  
253 =head1 NAME
254  
255 Browsermob::Proxy::CompareParams - Look for a request with the specified matching request params
256  
257 =head1 VERSION
258  
259 version 0.16
260  
261 =head1 SYNOPSIS
262  
263 # create a har with traffic
264 my $ua = LWP::UserAgent->new;
265 my $proxy = Browsermob::Server->new->create_proxy;
266 $ua->proxy($proxy->ua_proxy);
267 $ua->get('http://www.perl.org/?query=string');
268 my $har = $proxy->har;
269  
270 # ask the har if any requests have the following query params
271 my $request_found = cmp_request_params($har, { query => 'string' });
272 if ($request_found) {
273 print 'A request was found with ?query=string in it';
274 }
275  
276 =head1 DESCRIPTION
277  
278 Our primary use of Browsermob::Proxy is for checking analytics
279 requests. They're transferred primarily in the form of request
280 parameters, so it behooves us to make it easy to check if our HAR has
281 any requests that match a set of our expected request params.
282  
283 By default, we only export the one function: L</cmp_request_params>.
284  
285 =head1 METHODS
286  
287 =head2 cmp_request_params ( $har, $expected_params )
288  
289 Pass in a $har object genereated by L</Browsermob::Proxy>, as well as
290 a hashref of key/value pairs of the request params that you want to
291 find. In scalar context, this method will return the number of
292 requests that can be found with all of the expected_params key/value
293 pairs. If no requests are found, it returns that number: 0. So, you
294 can use the return value to check whether or not any matching requests
295 were found.
296  
297 # look for a request matching ?expected=params&go=here
298 my $found = cmp_request_params($har, { expected => 'params', go => 'here' });
299 say 'We found it!' if $found;
300  
301 In list context, the sub will return the boolean status as before, a
302 hashref with the unmatched pieces from the closest request, and a
303 hashref of the actual values from the closest request.
304  
305 my ($bool, $missing, $closest) = cmp_request_params($har, $expected);
306 if ( ! $bool ) {
307 say 'We cannot find these expected params: ';
308 print Dumper $missing;
309  
310 say 'The closest request's respective params were: ';
311 print Dumper $closest;
312 }
313  
314 =head2 convert_har_params_to_hash
315  
316 This isn't exported by default; we wouldn't expect that you'd need to
317 use it. But, if you're interested: the har format is a bit unwieldy to
318 work with. The requests come in an array of objects. Each object in
319 the array is a hash with a request key which points to an object with
320 a queryString key. The queryString object is an array of hashes with
321 name and value keys, the values of which are the actual query
322 params. Here's an example of one request:
323  
324 [0] {
325 ...
326 request {
327 ...
328 queryString [
329 [0] {
330 name "query",
331 value "string"
332 },
333 [1] {
334 name "query2",
335 value "string2"
336 },
337 ],
338 url "http://127.0.0.1/b/ss?query=string&query2=string2"
339 },
340 ...
341 }
342  
343 This function would transform that request into an array of hash
344 objects where the keys are the param names and the values are the
345 param values:
346  
347 \ [
348 [0] {
349 query "string"
350 query2 "string2"
351 }
352 ]
353  
354 =head1 FUNCTIONS
355  
356 =head2 replace_placeholder_values
357  
358 Takes two arguments: a HAR or the C<->{log}->{entries}> of a HAR, and
359 an assert hashref. If the assert has a value that starts with a colon
360 C<:>, and that value exists as a key in any of the HAR's actual query
361 parameter pairs, we'll replace the asserted value with the matching
362 assert's key.
363  
364 An example may help make this clear: say you assert the following
365 hashref
366  
367 $assert = {
368 query => 'param',
369 query2 => ':query'
370 };
371  
372 and your HAR records a request to a URL with the following params:
373 C</endpoint?query=param&query2=param>. We'll return you a new
374 C<$assert>:
375  
376 $assert = {
377 query => 'param',
378 query2 => 'param'
379 };
380  
381 =head2 collect_query_param_keys
382  
383 Given a HAR, or a the entries array of a HAR, we'll return a list of
384 all of the keys that were used in any of the query parameters. So if
385 your HAR contains a call to C</endpoint?example1&example2> and another
386 call to C</endpoint?example2&example3>, we'll return C<[ qw/ example1
387 example2 example3 ]>.
388  
389 =head1 SEE ALSO
390  
391 Please see those modules/websites for more information related to this module.
392  
393 =over 4
394  
395 =item *
396  
397 L<Browsermob::Proxy|Browsermob::Proxy>
398  
399 =back
400  
401 =head1 BUGS
402  
403 Please report any bugs or feature requests on the bugtracker website
404 https://github.com/gempesaw/Browsermob-Proxy/issues
405  
406 When submitting a bug or request, please include a test-file or a
407 patch to an existing test-file that illustrates the bug or desired
408 feature.
409  
410 =head1 AUTHOR
411  
412 Daniel Gempesaw <gempesaw@gmail.com>
413  
414 =head1 COPYRIGHT AND LICENSE
415  
416 This software is copyright (c) 2014 by Daniel Gempesaw.
417  
418 This is free software; you can redistribute it and/or modify it under
419 the same terms as the Perl 5 programming language system itself.
420  
421 =cut
« Proxy.pm ↑ Summary