Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Illuminate/Http/Client/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,19 @@ public function throwIf($condition)
return value($condition, $this) ? $this->throw(func_get_args()[1] ?? null) : $this;
}

/**
* Throw an exception if a server or client error occurred and the given condition evaluates to false.
*
* @param \Closure|bool $condition
* @return $this
*
* @throws \Illuminate\Http\Client\RequestException
*/
public function throwUnless($condition)
{
return $this->throwIf(! $condition);
}

/**
* Throw an exception if the response status code matches the given code.
*
Expand Down
29 changes: 29 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3188,6 +3188,35 @@ public function testRequestExceptionIsNotThrownIfConditionIsNotSatisfied()
$this->assertSame('{"result":{"foo":"bar"}}', $response->body());
}

public function testRequestExceptionIsThrownWhenUnlessConditionIsNotSatisfied()
{
$this->factory->fake([
'*' => $this->factory::response('', 400),
]);

$exception = null;

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

$this->assertNotNull($exception);
$this->assertInstanceOf(RequestException::class, $exception);
}

public function testRequestExceptionIsNotThrownWhenUnlessConditionIsSatisfied()
{
$this->factory->fake([
'*' => $this->factory::response(['result' => ['foo' => 'bar']], 400),
]);

$response = $this->factory->get('http://foo.com/api')->throwUnless(true);

$this->assertSame('{"result":{"foo":"bar"}}', $response->body());
}

public function testRequestExceptionIsThrowIfConditionClosureIsSatisfied()
{
$this->factory->fake([
Expand Down
Loading