Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/OpenConext/EngineBlock/Http/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function read($path, array $parameters = [], array $headers = [])
{
$resource = ResourcePathFormatter::format($path, $parameters);
$response = $this->httpClient->request('GET', $resource, [
'exceptions' => false,
'http_errors' => false,
'headers' => $headers
]);
$statusCode = $response->getStatusCode();
Expand Down Expand Up @@ -93,7 +93,7 @@ public function post($data, $path, $parameters = [], array $headers = [], bool $
{
$resource = ResourcePathFormatter::format($path, $parameters);
$response = $this->httpClient->request('POST', $resource, [
'exceptions' => false,
'http_errors' => false,
'body' => $data,
'headers' => $headers,
'verify' => $verify,
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/OpenConext/EngineBlock/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use OpenConext\EngineBlock\Http\Exception\AccessDeniedException;
use OpenConext\EngineBlock\Http\Exception\MalformedResponseException;
use OpenConext\EngineBlock\Http\Exception\UnreadableResourceException;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -196,4 +198,36 @@ public function an_access_denied_exception_is_thrown_if_the_response_status_code

$client->post('post-and-give-me/403', 'Post body');
}

#[Group('EngineBlock')]
#[Group('Http')]
#[Test]
public function an_unreadable_resource_exception_is_thrown_if_the_response_status_code_is_400_when_reading()
{
$mockHandler = new MockHandler([
new Response(400, [], '{"error":"Bad Request"}')
]);
$guzzle = new Client(['handler' => HandlerStack::create($mockHandler)]);
$client = new HttpClient($guzzle);

$this->expectException(UnreadableResourceException::class);

$client->read('/some-resource');
}

#[Group('EngineBlock')]
#[Group('Http')]
#[Test]
public function an_unreadable_resource_exception_is_thrown_if_the_response_status_code_is_400_when_posting()
{
$mockHandler = new MockHandler([
new Response(400, [], '{"error":"Bad Request"}')
]);
$guzzle = new Client(['handler' => HandlerStack::create($mockHandler)]);
$client = new HttpClient($guzzle);

$this->expectException(UnreadableResourceException::class);

$client->post('{"id":1}', '/some-resource');
}
}