-
-
Notifications
You must be signed in to change notification settings - Fork 162
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 #355 from mesilov/354-add-x-request-id-support
add x-request-id support
- Loading branch information
Showing
7 changed files
with
166 additions
and
21 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
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
50 changes: 50 additions & 0 deletions
50
src/Infrastructure/HttpClient/RequestId/DefaultRequestIdGenerator.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,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bitrix24\SDK\Infrastructure\HttpClient\RequestId; | ||
|
||
use Symfony\Component\Uid\Uuid; | ||
|
||
class DefaultRequestIdGenerator implements RequestIdGeneratorInterface | ||
{ | ||
private const DEFAULT_REQUEST_ID_FIELD_NAME = 'X-Request-ID'; | ||
private const KEY_NAME_VARIANTS = [ | ||
'REQUEST_ID', | ||
'HTTP_X_REQUEST_ID', | ||
'UNIQUE_ID' | ||
]; | ||
|
||
private function generate(): string | ||
{ | ||
return Uuid::v7()->toRfc4122(); | ||
} | ||
|
||
private function findExists(): ?string | ||
{ | ||
$candidate = null; | ||
foreach(self::KEY_NAME_VARIANTS as $key) | ||
{ | ||
if(!empty($_SERVER[$key])) | ||
{ | ||
$candidate = $_SERVER[$key]; | ||
break; | ||
} | ||
} | ||
return $candidate; | ||
} | ||
|
||
public function getRequestId(): string | ||
{ | ||
$reqId = $this->findExists(); | ||
if ($reqId === null) { | ||
$reqId = $this->generate(); | ||
} | ||
return $reqId; | ||
} | ||
|
||
public function getHeaderFieldName(): string | ||
{ | ||
return self::DEFAULT_REQUEST_ID_FIELD_NAME; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Infrastructure/HttpClient/RequestId/RequestIdGeneratorInterface.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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bitrix24\SDK\Infrastructure\HttpClient\RequestId; | ||
|
||
interface RequestIdGeneratorInterface | ||
{ | ||
public function getRequestId(): string; | ||
|
||
public function getHeaderFieldName(): string; | ||
} |
44 changes: 44 additions & 0 deletions
44
tests/Unit/Infrastructure/HttpClient/RequestId/DefaultRequestIdGeneratorTest.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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bitrix24\SDK\Tests\Unit\Infrastructure\HttpClient\RequestId; | ||
|
||
use Bitrix24\SDK\Infrastructure\HttpClient\RequestId\DefaultRequestIdGenerator; | ||
use Generator; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
class DefaultRequestIdGeneratorTest extends TestCase | ||
{ | ||
/** | ||
* @param $requestIdKey | ||
* @param $requestId | ||
* @return void | ||
* @dataProvider requestIdKeyDataProvider | ||
* @covers \Bitrix24\SDK\Infrastructure\HttpClient\RequestId\DefaultRequestIdGenerator::getRequestId | ||
*/ | ||
public function testExistsRequestId($requestIdKey, $requestId): void | ||
{ | ||
$_SERVER[$requestIdKey] = $requestId; | ||
$gen = new DefaultRequestIdGenerator(); | ||
$this->assertEquals($requestId, $gen->getRequestId()); | ||
unset($_SERVER[$requestIdKey]); | ||
} | ||
|
||
public function requestIdKeyDataProvider(): Generator | ||
{ | ||
yield 'REQUEST_ID' => [ | ||
'REQUEST_ID', | ||
Uuid::v7()->toRfc4122() | ||
]; | ||
yield 'HTTP_X_REQUEST_ID' => [ | ||
'HTTP_X_REQUEST_ID', | ||
Uuid::v7()->toRfc4122() | ||
]; | ||
yield 'UNIQUE_ID' => [ | ||
'UNIQUE_ID', | ||
Uuid::v7()->toRfc4122() | ||
]; | ||
} | ||
} |
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