Skip to content

Commit

Permalink
Add a custom Request Matcher for Post Fields
Browse files Browse the repository at this point in the history
  • Loading branch information
allejo committed Apr 11, 2020
1 parent 59f253b commit ee41b5d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/RelaxedRequestMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,18 @@ public static function matchBody(Request $first, Request $second)

return $firstBody === $secondBody;
}

public static function matchPostFields(Request $first, Request $second)
{
$bodyScrubbers = Config::getReqPostFieldScrubbers();
$firstPostFields = $first->getPostFields();
$secondPostFields = $second->getPostFields();

foreach ($bodyScrubbers as $bodyScrubber) {
$firstPostFields = $bodyScrubber($firstPostFields);
$secondPostFields = $bodyScrubber($secondPostFields);
}

return $firstPostFields === $secondPostFields;
}
}
1 change: 1 addition & 0 deletions src/VCRCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static function enable(array $options)
->addRequestMatcher('host', array('allejo\VCR\RelaxedRequestMatcher', 'matchHost'))
->addRequestMatcher('query_string', array('allejo\VCR\RelaxedRequestMatcher', 'matchQueryString'))
->addRequestMatcher('body', array('allejo\VCR\RelaxedRequestMatcher', 'matchBody'))
->addRequestMatcher('post_fields', array('allejo\VCR\RelaxedRequestMatcher', 'matchPostFields'))
;

VCR::getEventDispatcher()
Expand Down
40 changes: 40 additions & 0 deletions tests/VCR/RelaxedRequestMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,44 @@ function ($body) {
$this->assertFalse(RequestMatcher::matchBody($actualRequest, $cleanRequest));
$this->assertTrue(RelaxedRequestMatcher::matchBody($actualRequest, $cleanRequest));
}

public function testRelaxedRequestMatcherPostFields()
{
$actualRequest = Request::fromArray(
array(
'method' => 'POST',
'url' => 'http://example.com/api/v2',
'post_fields' => [
'Something Public' => 'public',
'VerySecret' => 'Do not tell anyone this secret',
],
)
);

$cleanRequest = Request::fromArray(
array(
'method' => 'POST',
'url' => 'http://example.com/api/v2',
'post_fields' => [
'Something Public' => 'public',
'VerySecret' => 'REDACTED',
],
)
);

Config::configureOptions(array(
'request' => array(
'postFieldScrubbers' => array(
function (array $postFields) {
$postFields['VerySecret'] = 'REDACTED';

return $postFields;
},
),
),
));

$this->assertFalse(RequestMatcher::matchPostFields($actualRequest, $cleanRequest));
$this->assertTrue(RelaxedRequestMatcher::matchPostFields($actualRequest, $cleanRequest));
}
}

0 comments on commit ee41b5d

Please sign in to comment.