Skip to content

Commit d6e08cc

Browse files
authored
Merge pull request #17 from pottink/add-data-formatter
Add RemoveSensitiveQueryStringFormatter
2 parents d00bd99 + 71c65ea commit d6e08cc

6 files changed

Lines changed: 74087 additions & 69277 deletions

File tree

grumphp.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ grumphp:
66
psalm:
77
config: psalm.xml
88
show_info: true
9+
no_cache: true
910
phpunit: ~
1011
composer:
1112
metadata:
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phpro\HttpTools\Formatter;
6+
7+
use Http\Message\Formatter as HttpFormatter;
8+
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\ResponseInterface;
10+
11+
final class RemoveSensitiveQueryStringsFormatter implements HttpFormatter
12+
{
13+
private HttpFormatter $formatter;
14+
15+
/**
16+
* @var non-empty-list<string>
17+
*/
18+
private array $sensitiveKeys;
19+
20+
/**
21+
* @param non-empty-list<string> $sensitiveKeys
22+
*/
23+
public function __construct(
24+
HttpFormatter $formatter,
25+
array $sensitiveKeys
26+
) {
27+
$this->formatter = $formatter;
28+
$this->sensitiveKeys = $sensitiveKeys;
29+
}
30+
31+
public function formatRequest(RequestInterface $request): string
32+
{
33+
return $this->removeQueryStrings($request);
34+
}
35+
36+
public function formatResponse(ResponseInterface $response): string
37+
{
38+
return $this->formatter->formatResponse($response);
39+
}
40+
41+
private function removeQueryStrings(RequestInterface $request): string
42+
{
43+
$uri = $request->getUri();
44+
$query = $uri->getQuery();
45+
46+
$result = [];
47+
parse_str($query, $result);
48+
49+
foreach ($this->sensitiveKeys as $key) {
50+
if (!array_key_exists($key, $result)) {
51+
continue;
52+
}
53+
54+
$result[$key] = 'xxxx';
55+
}
56+
57+
return $this->formatter->formatRequest(
58+
$request->withUri(
59+
$uri->withQuery(http_build_query($result))
60+
)
61+
);
62+
}
63+
}

tests/Helper/Formatter/CallbackFormatter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Phpro\HttpTools\Tests\Helper\Formatter;
66

77
use Http\Message\Formatter as HttpFormatter;
8+
use Psr\Http\Message\MessageInterface;
89
use Psr\Http\Message\RequestInterface;
910
use Psr\Http\Message\ResponseInterface;
1011

tests/Unit/Formatter/RemoveSensitiveHeaderKeysFormatterTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ protected function setUp(): void
3333
public function it_can_remove_sensitive_keys_from_request(array $headers, array $expected): void
3434
{
3535
$request = $this->createRequest('GET', 'something');
36+
3637
foreach ($headers as $name => $value) {
3738
$request = $request->withAddedHeader($name, $value);
3839
}
40+
3941
$formatted = $this->formatter->formatRequest($request);
4042

4143
self::assertSame($this->formatHeaders($expected), $formatted);
@@ -48,15 +50,17 @@ public function it_can_remove_sensitive_keys_from_request(array $headers, array
4850
public function it_can_remove_sensitive_keys_from_response(array $headers, array $expected): void
4951
{
5052
$response = $this->createResponse(200);
53+
5154
foreach ($headers as $name => $value) {
5255
$response = $response->withAddedHeader($name, $value);
5356
}
57+
5458
$formatted = $this->formatter->formatResponse($response);
5559

5660
self::assertSame($this->formatHeaders($expected), $formatted);
5761
}
5862

59-
public function provideJsonExpectations()
63+
public function provideJsonExpectations(): iterable
6064
{
6165
yield 'sample1' => [
6266
[
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phpro\HttpTools\Tests\Unit\Formatter;
6+
7+
use Http\Message\Formatter\SimpleFormatter;
8+
use Phpro\HttpTools\Formatter\RemoveSensitiveQueryStringsFormatter;
9+
use Phpro\HttpTools\Test\UseHttpFactories;
10+
use PHPUnit\Framework\TestCase;
11+
12+
final class RemoveSensitiveQueryStringsFormatterTest extends TestCase
13+
{
14+
use UseHttpFactories;
15+
16+
private RemoveSensitiveQueryStringsFormatter $formatter;
17+
18+
protected function setUp(): void
19+
{
20+
$this->formatter = new RemoveSensitiveQueryStringsFormatter(
21+
new SimpleFormatter(),
22+
['apiKey', 'token']
23+
);
24+
}
25+
26+
/**
27+
* @test
28+
* @dataProvider provideJsonExpectations
29+
*/
30+
public function it_can_remove_sensitive_query_strings_from_request(
31+
string $actual,
32+
string $expected
33+
): void {
34+
$request = $this->createRequest('GET', $actual);
35+
$formatted = $this->formatter->formatRequest($request);
36+
37+
self::assertStringContainsString(
38+
$expected,
39+
$formatted
40+
);
41+
}
42+
43+
/**
44+
* @test
45+
*/
46+
public function it_can_format_a_response(): void
47+
{
48+
$response = $this->createResponse();
49+
50+
self::assertIsString(
51+
$this->formatter->formatResponse($response)
52+
);
53+
}
54+
55+
public function provideJsonExpectations(): iterable
56+
{
57+
yield 'regular' => [
58+
'https://testapi.com/api/v1/products?query=string',
59+
'https://testapi.com/api/v1/products?query=string',
60+
];
61+
yield 'apiKey' => [
62+
'https://testapi.com/api/v1/products?apiKey=ABCDEFGH123',
63+
'https://testapi.com/api/v1/products?apiKey=xxxx',
64+
];
65+
yield 'apiKeyAndToken' => [
66+
'https://testapi.com/api/v1/products?apiKey=ABCDEFGH123&token=eyJzdWIiOiIxMjM0NTY3ODkwIiwibm',
67+
'https://testapi.com/api/v1/products?apiKey=xxxx&token=xxxx',
68+
];
69+
}
70+
}

0 commit comments

Comments
 (0)