Skip to content

Commit

Permalink
Skip middleware when request option key is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLevti committed Nov 11, 2022
1 parent 837a068 commit b245742
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ The format is based on [Keep a Changelog][1], and this project adheres to

## [Unreleased]

## [2.1.0] - 2022-11-11

### Changed

- Skip middleware if request option key is not set.

## [2.0.0] - 2022-11-05

### Added
Expand Down Expand Up @@ -61,7 +67,8 @@ The format is based on [Keep a Changelog][1], and this project adheres to
[1]: https://keepachangelog.com/en/1.1.0/
[2]: https://semver.org/spec/v2.0.0.html

[Unreleased]: https://github.com/Poor-Plebs/guzzle-retry-after-middleware/compare/2.0.0...HEAD
[Unreleased]: https://github.com/Poor-Plebs/guzzle-retry-after-middleware/compare/2.1.0...HEAD
[2.1.0]: https://github.com/Poor-Plebs/guzzle-retry-after-middleware/releases/2.1.0
[2.0.0]: https://github.com/Poor-Plebs/guzzle-retry-after-middleware/releases/2.0.0
[1.1.1]: https://github.com/Poor-Plebs/guzzle-retry-after-middleware/releases/1.1.1
[1.1.0]: https://github.com/Poor-Plebs/guzzle-retry-after-middleware/releases/1.1.0
Expand Down
14 changes: 6 additions & 8 deletions src/RetryAfterMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,17 @@ public function __construct(protected CacheInterface $cache)
public function __invoke(callable $handler): callable
{
return function (RequestInterface $request, array $options = []) use ($handler): PromiseInterface {
if (!isset($options[self::REQUEST_OPTION])) {
throw new MissingRetryAfterCacheKeyException(sprintf(
'Required qequest option %s has not been provided.',
self::REQUEST_OPTION,
), $request);
if (!array_key_exists(self::REQUEST_OPTION, $options)) {
return $handler($request, $options);
}

$key = $options[self::REQUEST_OPTION];
if (!is_string($key)) {
if (!is_string($key) || $key === '') {
$type = gettype($key);
throw new MissingRetryAfterCacheKeyException(sprintf(
'Request option %s must be of type string, %s given.',
'Request option %s must be a non empty string, %s given.',
self::REQUEST_OPTION,
gettype($key),
$type === 'string' ? 'empty string' : $type,
), $request);
}

Expand Down
31 changes: 28 additions & 3 deletions tests/RetryAfterMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,41 @@ public function it_fails_during_retry_after_seconds_period($mockHttpHandler): vo
$this->assertSame('{"ok":true,"result":{}}', (string)$response->getBody());
}

/**
* @test
* @covers \PoorPlebs\GuzzleRetryAfterMiddleware\RetryAfterMiddleware
*/
public function it_passes_when_cache_key_is_missing(): void
{
$handlerStack = HandlerStack::create(new MockHandler([
new Response(200, [], '{"ok":true,"result":{}}'),
]));
$handlerStack->unshift(
new RetryAfterMiddleware(new Repository(new ArrayStore())),
'retry_after',
);
$client = new Client([
'base_uri' => 'https://sometest.com/',
'handler' => $handlerStack,
]);

/** @var \Psr\Http\Message\ResponseInterface $response */
$response = $client->postAsync('sendMessage')->wait();

$this->assertSame('{"ok":true,"result":{}}', (string)$response->getBody());
}

/**
* @test
* @covers \PoorPlebs\GuzzleRetryAfterMiddleware\RetryAfterMiddleware
* @covers \PoorPlebs\GuzzleRetryAfterMiddleware\MissingRetryAfterCacheKeyException
*/
public function it_throws_an_exception_when_cache_key_is_missing(): void
public function it_throws_an_exception_when_cache_key_is_empty_string(): void
{
$this->expectException(MissingRetryAfterCacheKeyException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage(
'Required qequest option ' . RetryAfterMiddleware::REQUEST_OPTION . ' has not been provided.'
'Request option ' . RetryAfterMiddleware::REQUEST_OPTION . ' must be a non empty string, empty string given.'
);

$handlerStack = HandlerStack::create(new MockHandler([
Expand All @@ -269,6 +293,7 @@ public function it_throws_an_exception_when_cache_key_is_missing(): void
$client = new Client([
'base_uri' => 'https://sometest.com/',
'handler' => $handlerStack,
RetryAfterMiddleware::REQUEST_OPTION => '',
]);

$client->postAsync('sendMessage')->wait();
Expand All @@ -284,7 +309,7 @@ public function it_throws_an_exception_when_cache_key_is_not_a_string(): void
$this->expectException(MissingRetryAfterCacheKeyException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage(
'Request option ' . RetryAfterMiddleware::REQUEST_OPTION . ' must be of type string, integer given.'
'Request option ' . RetryAfterMiddleware::REQUEST_OPTION . ' must be a non empty string, integer given.'
);

$handlerStack = HandlerStack::create(new MockHandler([
Expand Down

0 comments on commit b245742

Please sign in to comment.