|
| 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