Skip to content
This repository was archived by the owner on Jun 28, 2024. It is now read-only.

Commit 47f5135

Browse files
committed
fix: #19 - Fix detection of Retry-After header in ServerResponseException class and phpdoc
Fixes: #19
1 parent fdd22df commit 47f5135

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,6 @@ parameters:
9595
count: 1
9696
path: src/Response/DownstreamResponseContract.php
9797

98-
-
99-
message: "#^Property LaravelFCM\\\\Response\\\\Exceptions\\\\ServerResponseException\\:\\:\\$retryAfter \\(int\\) does not accept array\\<string\\>\\.$#"
100-
count: 1
101-
path: src/Response/Exceptions/ServerResponseException.php
102-
10398
-
10499
message: "#^Negated boolean expression is always true\\.$#"
105100
count: 1

src/Response/Exceptions/ServerResponseException.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
class ServerResponseException extends Exception
99
{
1010
/**
11-
* retry after.
11+
* The value of the first Retry-After header in the response.
1212
*
13-
* @var int
13+
* @see https://httpwg.org/specs/rfc7231.html#header.retry-after
14+
* @var int|string
1415
*/
1516
public $retryAfter;
1617

@@ -25,8 +26,9 @@ public function __construct(ResponseInterface $response)
2526
$responseHeader = $response->getHeaders();
2627
$responseBody = $response->getBody()->getContents();
2728

28-
if (array_keys($responseHeader, 'Retry-After')) {
29-
$this->retryAfter = $responseHeader['Retry-After'];
29+
if (array_key_exists('Retry-After', $responseHeader)) {
30+
$retryAfterValue = $responseHeader['Retry-After'][0];
31+
$this->retryAfter = is_numeric($retryAfterValue) ? (int) $retryAfterValue : $retryAfterValue;
3032
}
3133

3234
parent::__construct($responseBody, $code);

tests/ServerResponseExceptionTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace LaravelFCM\Tests;
4+
5+
use LaravelFCM\Response\Exceptions\ServerResponseException;
6+
7+
class ServerResponseExceptionTest extends FCMTestCase
8+
{
9+
10+
public function testHandleWithoutRetryAfter(): void
11+
{
12+
$response = new \GuzzleHttp\Psr7\Response(
13+
200,
14+
[],
15+
''
16+
);
17+
$sre = new ServerResponseException($response);
18+
$this->assertNull($sre->retryAfter);
19+
}
20+
21+
public function testHandleWithRetryAfter(): void
22+
{
23+
$response = new \GuzzleHttp\Psr7\Response(
24+
200,
25+
[
26+
// See: https://httpwg.org/specs/rfc7231.html#header.retry-after
27+
'Retry-After' => 120,
28+
],
29+
''
30+
);
31+
$sre = new ServerResponseException($response);
32+
$this->assertNotNull($sre->retryAfter);
33+
$this->assertIsNumeric($sre->retryAfter);
34+
$this->assertSame(120, $sre->retryAfter);
35+
}
36+
37+
public function testHandleWithRetryAfterMultiple(): void
38+
{
39+
$response = new \GuzzleHttp\Psr7\Response(
40+
200,
41+
[
42+
// See: https://httpwg.org/specs/rfc7231.html#header.retry-after
43+
'Retry-After' => [120, 'Fri, 31 Dec 1999 23:59:59 GMT', 420],
44+
],
45+
''
46+
);
47+
$sre = new ServerResponseException($response);
48+
$this->assertNotNull($sre->retryAfter);
49+
$this->assertIsNumeric($sre->retryAfter);
50+
$this->assertSame(120, $sre->retryAfter);
51+
}
52+
}

0 commit comments

Comments
 (0)