-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSchemaAwareHttpClientTest.php
More file actions
55 lines (44 loc) · 1.85 KB
/
SchemaAwareHttpClientTest.php
File metadata and controls
55 lines (44 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
declare(strict_types=1);
namespace Macpaw\SchemaContextBundle\Tests\HttpClient;
use Macpaw\SchemaContextBundle\HttpClient\SchemaAwareHttpClient;
use Macpaw\SchemaContextBundle\Service\SchemaResolver;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
class SchemaAwareHttpClientTest extends TestCase
{
public function testItInjectsSchemaIntoBaggageHeader(): void
{
$expectedSchema = 'tenant_42';
$mockResponse = new MockResponse('OK');
$schemaRequestHeader = 'X-Schema';
$mockClient = $this->createMock(HttpClientInterface::class);
$mockClient
->expects($this->once())
->method('request')
->with(
'GET',
'https://api.example.com/test',
$this->callback(function (array $options) use ($expectedSchema, $schemaRequestHeader) {
$headers = $options['headers'] ?? [];
$baggage = $headers['baggage'] ?? null;
if (is_array($baggage)) {
$baggage = implode(',', $baggage);
}
return is_string($baggage) && str_contains($baggage, $schemaRequestHeader . '=' . $expectedSchema);
})
)
->willReturn($mockResponse);
$schemaResolver = $this->createMock(SchemaResolver::class);
$schemaResolver->method('getSchema')->willReturn($expectedSchema);
$client = new SchemaAwareHttpClient(
$mockClient,
$schemaResolver,
$schemaRequestHeader,
);
$response = $client->request('GET', 'https://api.example.com/test');
self::assertInstanceOf(ResponseInterface::class, $response);
}
}