Skip to content

Commit d3b2018

Browse files
author
Olivier MICHAUD
committed
Implement a replacement filter to cleanup private data in responses
1 parent 1202f7f commit d3b2018

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/Recorder/FilesystemRecorder.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Http\Client\Plugin\Vcr\Recorder;
66

7-
use GuzzleHttp\Psr7;
7+
use GuzzleHttp\Psr7\Message;
88
use Psr\Http\Message\ResponseInterface;
99
use Psr\Log\LoggerAwareInterface;
1010
use Psr\Log\LoggerAwareTrait;
@@ -31,7 +31,12 @@ final class FilesystemRecorder implements RecorderInterface, PlayerInterface, Lo
3131
*/
3232
private $filesystem;
3333

34-
public function __construct(string $directory, ?Filesystem $filesystem = null)
34+
/**
35+
* @var array
36+
*/
37+
private $filters;
38+
39+
public function __construct(string $directory, ?Filesystem $filesystem = null,array $filters=[])
3540
{
3641
$this->filesystem = $filesystem ?? new Filesystem();
3742

@@ -44,6 +49,7 @@ public function __construct(string $directory, ?Filesystem $filesystem = null)
4449
}
4550

4651
$this->directory = realpath($directory).\DIRECTORY_SEPARATOR;
52+
$this->filters = $filters;
4753
}
4854

4955
public function replay(string $name): ?ResponseInterface
@@ -59,15 +65,19 @@ public function replay(string $name): ?ResponseInterface
5965

6066
$this->log('Response replayed from {filename}', $context);
6167

62-
return Psr7\parse_response(file_get_contents($filename));
68+
return Message::parseResponse(file_get_contents($filename));
6369
}
6470

6571
public function record(string $name, ResponseInterface $response): void
6672
{
6773
$filename = "{$this->directory}$name.txt";
6874
$context = compact('name', 'filename');
6975

70-
$this->filesystem->dumpFile($filename, Psr7\str($response));
76+
$content = Message::toString($response);
77+
foreach ($this->filters as $regularExpression => $replacement) {
78+
$content = preg_replace($regularExpression, $replacement, $content);
79+
}
80+
$this->filesystem->dumpFile($filename, $content);
7181

7282
$this->log('Response for {name} stored into {filename}', $context);
7383
}

tests/Recorder/FilesystemRecorderTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,27 @@ public function testRecord(): void
5656
$this->assertSame($original->getHeaders(), $replayed->getHeaders());
5757
$this->assertSame((string) $original->getBody(), (string) $replayed->getBody());
5858
}
59+
60+
public function testRecordWithFilter(): void
61+
{
62+
$original = new Response(200, ['X-Foo' => 'Bar', 'X-Bar' => 'private-token-065a1bb33f000032ab'], 'The content');
63+
64+
$recorder = new FilesystemRecorder($this->workspace, $this->filesystem, [
65+
'!private-token-[0-9a-z]+!' => 'private-token-xxxx',
66+
'!The content!' => 'The big content',
67+
]);
68+
$recorder->record('my_awesome_response', $original);
69+
70+
$this->assertFileExists(sprintf('%s%smy_awesome_response.txt', $this->workspace, \DIRECTORY_SEPARATOR));
71+
72+
$replayed = (new FilesystemRecorder($this->workspace))->replay('my_awesome_response');
73+
74+
$this->assertNotNull($replayed, 'Response should not be null');
75+
76+
$this->assertSame($original->getStatusCode(), $replayed->getStatusCode());
77+
$expectedHeaders = $original->getHeaders();
78+
$expectedHeaders['X-Bar'] = ['private-token-xxxx'];
79+
$this->assertSame($expectedHeaders, $replayed->getHeaders());
80+
$this->assertSame('The big content', (string) $replayed->getBody());
81+
}
5982
}

0 commit comments

Comments
 (0)