|
14 | 14 |
|
15 | 15 | namespace Gitlab\Tests\HttpClient; |
16 | 16 |
|
| 17 | +use Generator; |
17 | 18 | use Gitlab\HttpClient\Builder; |
| 19 | +use GuzzleHttp\Psr7\Response; |
18 | 20 | use Http\Client\Common\HttpMethodsClientInterface; |
19 | 21 | use Http\Client\Common\Plugin; |
20 | 22 | use PHPUnit\Framework\Attributes\Before; |
| 23 | +use PHPUnit\Framework\Attributes\DataProvider; |
21 | 24 | use PHPUnit\Framework\TestCase; |
| 25 | +use Psr\Cache\CacheItemInterface; |
| 26 | +use Psr\Cache\CacheItemPoolInterface; |
22 | 27 | use Psr\Http\Client\ClientInterface; |
23 | 28 | use Psr\Http\Message\RequestFactoryInterface; |
24 | 29 | use Psr\Http\Message\StreamFactoryInterface; |
@@ -64,6 +69,46 @@ public function testRemovePluginShouldInvalidateHttpClient(): void |
64 | 69 | $this->assertNotSame($client, $this->subject->getHttpClient()); |
65 | 70 | } |
66 | 71 |
|
| 72 | + #[DataProvider('credentialHeaderProvider')] |
| 73 | + public function testCacheKeyShouldIncludeCredentialHeaders(string $header): void |
| 74 | + { |
| 75 | + $keys = []; |
| 76 | + |
| 77 | + $cachePool = $this->createMock(CacheItemPoolInterface::class); |
| 78 | + $cachePool |
| 79 | + ->expects($this->exactly(2)) |
| 80 | + ->method('getItem') |
| 81 | + ->willReturnCallback(function (string $key) use (&$keys): CacheItemInterface { |
| 82 | + $keys[] = $key; |
| 83 | + |
| 84 | + $cacheItem = $this->createMock(CacheItemInterface::class); |
| 85 | + $cacheItem->method('expiresAfter')->willReturnSelf(); |
| 86 | + |
| 87 | + return $cacheItem; |
| 88 | + }); |
| 89 | + |
| 90 | + $httpClient = $this->createMock(ClientInterface::class); |
| 91 | + $httpClient |
| 92 | + ->method('sendRequest') |
| 93 | + ->willReturnCallback(static function (): Response { |
| 94 | + return new Response(200, ['Cache-Control' => 'max-age=60, private']); |
| 95 | + }); |
| 96 | + |
| 97 | + $builder = new Builder($httpClient); |
| 98 | + $builder->addCache($cachePool); |
| 99 | + $builder->getHttpClient()->get('https://gitlab.example.com/api/v4/user', [$header => 'first']); |
| 100 | + $builder->getHttpClient()->get('https://gitlab.example.com/api/v4/user', [$header => 'second']); |
| 101 | + |
| 102 | + $this->assertNotSame($keys[0], $keys[1]); |
| 103 | + } |
| 104 | + |
| 105 | + public static function credentialHeaderProvider(): Generator |
| 106 | + { |
| 107 | + yield 'PRIVATE-TOKEN' => ['PRIVATE-TOKEN']; |
| 108 | + yield 'JOB-TOKEN' => ['JOB-TOKEN']; |
| 109 | + yield 'SUDO' => ['SUDO']; |
| 110 | + } |
| 111 | + |
67 | 112 | public function testHttpClientShouldBeAnHttpMethodsClient(): void |
68 | 113 | { |
69 | 114 | $this->assertInstanceOf(HttpMethodsClientInterface::class, $this->subject->getHttpClient()); |
|
0 commit comments