This repository has been archived by the owner on Feb 10, 2023. It is now read-only.
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 #24 from lepiaf/add-requester-header
add header requester name via middleware
- Loading branch information
Showing
8 changed files
with
90 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
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,33 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace lepiaf\SapientBundle\GuzzleHttp\Middleware; | ||
|
||
use lepiaf\SapientBundle\Service\PublicKeyGetter; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
class RequesterHeaderMiddleware | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $requesterName; | ||
|
||
/** | ||
* @param string $requesterName | ||
*/ | ||
public function __construct(string $requesterName) | ||
{ | ||
$this->requesterName = $requesterName; | ||
} | ||
|
||
public function __invoke(callable $handler): callable | ||
{ | ||
return function(RequestInterface $request, array $options) use ($handler) { | ||
return $handler( | ||
$request->withHeader(PublicKeyGetter::HEADER_REQUESTER, $this->requesterName), | ||
$options | ||
); | ||
}; | ||
} | ||
} |
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,6 @@ | ||
services: | ||
lepiaf\SapientBundle\GuzzleHttp\Middleware\RequesterHeaderMiddleware: | ||
arguments: | ||
- '%sapient.guzzle_middleware.requester_name%' | ||
tags: | ||
- { name: 'csa_guzzle.middleware', alias: 'requester_name' } |
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
31 changes: 31 additions & 0 deletions
31
tests/GuzzleHttp/Middleware/RequesterHeaderMiddlewareTest.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,31 @@ | ||
<?php | ||
|
||
namespace Tests\lepiaf\SapientBundle\EventSubscriber; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Handler\MockHandler; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Psr7\Response; | ||
use lepiaf\SapientBundle\GuzzleHttp\Middleware\RequesterHeaderMiddleware; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
class RequesterHeaderMiddlewareTest extends TestCase | ||
{ | ||
public function testRequestWithHeader() | ||
{ | ||
$mockHandler = new MockHandler([ | ||
function (RequestInterface $request) { | ||
$this->assertTrue($request->hasHeader('Sapient-Requester')); | ||
$this->assertSame(['client-bob'], $request->getHeader('Sapient-Requester')); | ||
|
||
return new Response(200); | ||
}, | ||
]); | ||
$handler = HandlerStack::create($mockHandler); | ||
$handler->push(new RequesterHeaderMiddleware('client-bob')); | ||
$client = new Client(['handler' => $handler]); | ||
|
||
$client->request('GET', 'http://api.example.com/api/ping'); | ||
} | ||
} |