Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions lib/Test/LWP/Recorder.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use base qw(LWP::UserAgent);
use Digest::MD5 qw(md5_hex);
use File::Slurp;
use File::Spec;
use List::Util qw(reduce);
use HTTP::Status qw(:constants);
use HTTP::Response;

Expand All @@ -29,22 +28,21 @@ sub new {
return $self;
}

sub _filter_param {
my ( $self, $key, $value ) = @_;
my %filter = map { $_ => 1 } @{ $self->{_test_options}->{filter_params} };
return join q{=}, $key, $filter{$key} ? q{} : $value;
}

sub _filter_all_params {
my $self = shift;
my $param_string = shift;

my %filter = map { $_ => 1 } @{ $self->{'_test_options'}->{'filter_params'} };

## no critic (BuiltinFunctions::ProhibitStringySplit)
my %query = map { ( split q{=} )[ 0, 1 ] } split q{\&}, $param_string;
## use critic;
return %query
? reduce { $a . $self->_filter_param( $b, $query{$b} ) }
sort keys %query
: q{};

return join("&",
map { "$_=$query{$_}" }
grep { !$filter{$_} }
sort keys %query
);
}

sub _get_cache_key {
Expand Down
48 changes: 48 additions & 0 deletions t/filter_params.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use strict;
use warnings;

use Test::More tests => 7;

use Test::LWP::Recorder;

my $ua = Test::LWP::Recorder->new({
record => 1,
cache_dir => 't/LWPtmp',
filter_params => [ 'filterme' ],
});


# Same URL parameter strings should return the same result
{
my @pairs = (
[ "foo=a&bar=b", "foo=a&bar=b" ],
[ "foo=a&bar=b", "bar=b&foo=a" ],
[ "foo=a&bar=b", "bar=b&foo=a&filterme=c" ],
[ "foo=a&bar=b&filterme=c", "bar=b&foo=a&filterme=d" ],
);

for my $pair (@pairs) {
my @results = map { $ua->_filter_all_params($_) } @$pair;
ok(
$results[0] eq $results[1],
sprintf("Got matching param string for '%s' and '%s'", @$pair)
) or diag sprintf("Got '%s' for '%s',\nBut '%s' for '%s'", $results[0], $pair->[0], $results[1], $pair->[1]);
}
}

# Different URL parameter strings should return different result
{
my @pairs = (
[ "bar=a", "bar=b" ],
[ "foo=a&bar=b", "foo=c&bar=b" ],
[ "foo=a&bar=b", "foo=a&bar=c" ],
);

for my $pair (@pairs) {
my @results = map { $ua->_filter_all_params($_) } @$pair;
ok(
$results[0] ne $results[1],
sprintf("Got mis-matching param string for '%s' and '%s'", @$pair)
) or diag sprintf("Got '%s' for '%s' and '%s'!", $results[0], @$pair);
}
}