Skip to content

Commit 3eeb6e8

Browse files
micoliOlivier MICHAUD
andauthored
Implement a replacement filter to cleanup private data in responses (#8)
Co-authored-by: Olivier MICHAUD <[email protected]>
1 parent 5c3e952 commit 3eeb6e8

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/Recorder/FilesystemRecorder.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -67,7 +73,8 @@ public function record(string $name, ResponseInterface $response): void
6773
$filename = "{$this->directory}$name.txt";
6874
$context = compact('name', 'filename');
6975

70-
$this->filesystem->dumpFile($filename, Psr7\str($response));
76+
$content = preg_replace(array_keys($this->filters), array_values($this->filters), Psr7\str($response));
77+
$this->filesystem->dumpFile($filename, $content);
7178

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

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)