Skip to content

Commit 5ec58fe

Browse files
author
Olivier MICHAUD
committed
Implement a replacement filter to cleanup private data in responses
1 parent 2ff502f commit 5ec58fe

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/Recorder/FilesystemRecorder.php

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

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

7+
use GuzzleHttp\Psr7\Message;
78
use GuzzleHttp\Psr7;
89
use Psr\Http\Message\ResponseInterface;
910
use Psr\Log\LoggerAwareInterface;
@@ -31,7 +32,12 @@ final class FilesystemRecorder implements RecorderInterface, PlayerInterface, Lo
3132
*/
3233
private $filesystem;
3334

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

@@ -44,6 +50,7 @@ public function __construct(string $directory, ?Filesystem $filesystem = null)
4450
}
4551

4652
$this->directory = realpath($directory).\DIRECTORY_SEPARATOR;
53+
$this->filters = $filters;
4754
}
4855

4956
public function replay(string $name): ?ResponseInterface
@@ -67,7 +74,8 @@ public function record(string $name, ResponseInterface $response): void
6774
$filename = "{$this->directory}$name.txt";
6875
$context = compact('name', 'filename');
6976

70-
$this->filesystem->dumpFile($filename, Psr7\str($response));
77+
$content = preg_replace(array_keys($this->filters), array_values($this->filters), Message::toString($response));
78+
$this->filesystem->dumpFile($filename, $content);
7179

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

tests/Recorder/FilesystemRecorderTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,28 @@ protected function tearDown(): void
9393
$this->filesystem->remove($this->workspace);
9494
umask($this->umask);
9595
}
96+
97+
public function testRecordWithFilter(): void
98+
{
99+
$original = new Response(200, ['X-Foo' => 'Bar', 'X-Bar' => 'private-token-065a1bb33f000032ab'], 'The content');
100+
101+
$recorder = new FilesystemRecorder($this->workspace, $this->filesystem, [
102+
'!private-token-[0-9a-z]+!' => 'private-token-xxxx',
103+
'!The content!' => 'The big content',
104+
]);
105+
$recorder->record('my_awesome_response', $original);
106+
107+
$this->assertFileExists(sprintf('%s%smy_awesome_response.txt', $this->workspace, \DIRECTORY_SEPARATOR));
108+
109+
$replayed = (new FilesystemRecorder($this->workspace))->replay('my_awesome_response');
110+
111+
$this->assertNotNull($replayed, 'Response should not be null');
112+
113+
$this->assertSame($original->getStatusCode(), $replayed->getStatusCode());
114+
$expectedHeaders = $original->getHeaders();
115+
$expectedHeaders['X-Bar'] = ['private-token-xxxx'];
116+
$this->assertSame($expectedHeaders, $replayed->getHeaders());
117+
$this->assertSame('The big content', (string) $replayed->getBody());
118+
}
119+
96120
}

0 commit comments

Comments
 (0)