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
24 changes: 12 additions & 12 deletions src/Symfony/Component/HttpClient/Response/TraceableResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,24 @@ public function getHeaders(bool $throw = true): array

public function getContent(bool $throw = true): string
{
$this->content = $this->response->getContent(false);

if ($throw) {
$this->checkStatusCode($this->response->getStatusCode());
try {
return $this->content = $this->response->getContent(false);
} finally {
if ($throw) {
$this->checkStatusCode($this->response->getStatusCode());
}
}

return $this->content;
}

public function toArray(bool $throw = true): array
{
$this->content = $this->response->toArray(false);

if ($throw) {
$this->checkStatusCode($this->response->getStatusCode());
try {
return $this->content = $this->response->toArray(false);
} finally {
if ($throw) {
$this->checkStatusCode($this->response->getStatusCode());
}
}

return $this->content;
}

public function cancel(): void
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/TraceableHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\HttpClient\NativeHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpClient\TraceableHttpClient;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\Test\TestHttpServer;

Expand Down Expand Up @@ -100,4 +101,16 @@ public function testStream()
$this->assertGreaterThan(1, \count($chunks));
$this->assertSame('Symfony is awesome!', implode('', $chunks));
}

public function testToArrayChecksStatusCodeBeforeDecoding()
{
$this->expectException(ClientExceptionInterface::class);

$sut = new TraceableHttpClient(new MockHttpClient($responseFactory = function (): MockResponse {
return new MockResponse('Errored.', ['http_code' => 400]);
}));

$response = $sut->request('GET', 'https://example.com/foo/bar');
$response->toArray();
}
}