Skip to content

Commit c55667a

Browse files
authored
fix bug for always throwing exception when we pass a callable to throwUnlessStatus method [test included] (#48844)
* fix bug for always throwing exception when we pass a callable * added test for throwUnlessStatus method with callable input returning true
1 parent 0646f33 commit c55667a

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/Illuminate/Http/Client/Response.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,8 @@ public function throwIfStatus($statusCode)
338338
*/
339339
public function throwUnlessStatus($statusCode)
340340
{
341-
if (is_callable($statusCode) &&
342-
! $statusCode($this->status(), $this)) {
343-
return $this->throw();
341+
if (is_callable($statusCode)) {
342+
return $statusCode($this->status(), $this) ? $this : $this->throw();
344343
}
345344

346345
return $this->status() === $statusCode ? $this : $this->throw();

tests/Http/HttpClientTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,6 +2194,16 @@ public function testRequestExceptionIsThrownUnlessStatusCodeIsSatisfied()
21942194
}
21952195

21962196
$this->assertNull($exception);
2197+
2198+
$exception = null;
2199+
2200+
try {
2201+
$this->factory->get('http://foo.com/api/500')->throwUnlessStatus(fn ($status) => $status === 500);
2202+
} catch (RequestException $e) {
2203+
$exception = $e;
2204+
}
2205+
2206+
$this->assertNull($exception);
21972207
}
21982208

21992209
public function testRequestExceptionIsThrownIfIsClientError()

0 commit comments

Comments
 (0)