-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from Clearfacts/CLEARFACTS-10616
[CLEARFACTS-10616] symfony http client decorator for correlation id
- Loading branch information
Showing
3 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Datalog\Correlation\Http; | ||
|
||
use Datalog\Correlation\Correlation; | ||
use Symfony\Component\HttpClient\DecoratorTrait; | ||
use Symfony\Component\HttpClient\HttpClient; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
class CorrelationHttpClientDecorator implements HttpClientInterface | ||
{ | ||
use DecoratorTrait; | ||
|
||
private Correlation $correlation; | ||
|
||
public function __construct( | ||
Correlation $correlation, | ||
?HttpClientInterface $client = null, | ||
) { | ||
$this->correlation = $correlation; | ||
$this->client = $client ?? HttpClient::create(); | ||
} | ||
|
||
public function request(string $method, string $url, array $options = []): ResponseInterface | ||
{ | ||
if (!isset($options['headers']) || !array_key_exists('correlation_id', $options['headers'])) { | ||
$options['headers']['correlation_id'] = $this->correlation->getId(); | ||
} | ||
|
||
return $this->client->request($method, $url, $options); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
tests/Correlation/Http/CorrelationHttpClientDecoratorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Tests\Datalog\Correlation\Http; | ||
|
||
use Datalog\Correlation\Correlation; | ||
use Datalog\Correlation\Http\CorrelationHttpClientDecorator; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpClient\MockHttpClient; | ||
use Symfony\Component\HttpClient\Response\MockResponse; | ||
|
||
class CorrelationHttpClientDecoratorTest extends TestCase | ||
{ | ||
public function testAddsHeader(): void | ||
{ | ||
Correlation::setId('test-id'); | ||
|
||
$client = new MockHttpClient(function ($method, $url, $options) { | ||
$this->assertSame(['correlation_id: test-id'], $options['normalized_headers']['correlation_id'] ?? null); | ||
|
||
return new MockResponse(); | ||
}); | ||
|
||
$decorator = new CorrelationHttpClientDecorator(new Correlation(), $client); | ||
|
||
$decorator->request('GET', 'http://example.com'); | ||
} | ||
} |