Skip to content

Commit

Permalink
fix bug for always throwing exception when we pass a callable to thro…
Browse files Browse the repository at this point in the history
…wUnlessStatus 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
  • Loading branch information
mhfereydouni authored Oct 30, 2023
1 parent 0646f33 commit c55667a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/Illuminate/Http/Client/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,8 @@ public function throwIfStatus($statusCode)
*/
public function throwUnlessStatus($statusCode)
{
if (is_callable($statusCode) &&
! $statusCode($this->status(), $this)) {
return $this->throw();
if (is_callable($statusCode)) {
return $statusCode($this->status(), $this) ? $this : $this->throw();
}

return $this->status() === $statusCode ? $this : $this->throw();
Expand Down
10 changes: 10 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2194,6 +2194,16 @@ public function testRequestExceptionIsThrownUnlessStatusCodeIsSatisfied()
}

$this->assertNull($exception);

$exception = null;

try {
$this->factory->get('http://foo.com/api/500')->throwUnlessStatus(fn ($status) => $status === 500);
} catch (RequestException $e) {
$exception = $e;
}

$this->assertNull($exception);
}

public function testRequestExceptionIsThrownIfIsClientError()
Expand Down

0 comments on commit c55667a

Please sign in to comment.