Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c4f2d20

Browse files
committed
[HttpClient] Check satus code before decoding in TraceableResponse::toArray()
1 parent a29af81 commit c4f2d20

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

src/Symfony/Component/HttpClient/Response/TraceableResponse.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Symfony\Component\HttpClient\Exception\ServerException;
1717
use Symfony\Component\HttpClient\TraceableHttpClient;
1818
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
19+
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
20+
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
1921
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
2022
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
2123
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
@@ -52,24 +54,24 @@ public function getHeaders(bool $throw = true): array
5254

5355
public function getContent(bool $throw = true): string
5456
{
55-
$this->content = $this->response->getContent(false);
56-
57-
if ($throw) {
58-
$this->checkStatusCode($this->response->getStatusCode());
57+
try {
58+
return $this->content = $this->response->getContent(false);
59+
} finally {
60+
if ($throw) {
61+
$this->checkStatusCode($this->response->getStatusCode());
62+
}
5963
}
60-
61-
return $this->content;
6264
}
6365

6466
public function toArray(bool $throw = true): array
6567
{
66-
$this->content = $this->response->toArray(false);
67-
68-
if ($throw) {
69-
$this->checkStatusCode($this->response->getStatusCode());
68+
try {
69+
return $this->content = $this->response->toArray(false);
70+
} finally {
71+
if ($throw) {
72+
$this->checkStatusCode($this->response->getStatusCode());
73+
}
7074
}
71-
72-
return $this->content;
7375
}
7476

7577
public function cancel(): void

src/Symfony/Component/HttpClient/Tests/TraceableHttpClientTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpClient\NativeHttpClient;
1717
use Symfony\Component\HttpClient\Response\MockResponse;
1818
use Symfony\Component\HttpClient\TraceableHttpClient;
19+
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
1920
use Symfony\Contracts\HttpClient\HttpClientInterface;
2021
use Symfony\Contracts\HttpClient\Test\TestHttpServer;
2122

@@ -100,4 +101,16 @@ public function testStream()
100101
$this->assertGreaterThan(1, \count($chunks));
101102
$this->assertSame('Symfony is awesome!', implode('', $chunks));
102103
}
104+
105+
public function testToArrayChecksStatusCodeBeforeDecoding()
106+
{
107+
$this->expectException(ClientExceptionInterface::class);
108+
109+
$sut = new TraceableHttpClient(new MockHttpClient($responseFactory = function (): MockResponse {
110+
return new MockResponse('Errored.', ['http_code' => 400]);
111+
}));
112+
113+
$response = $sut->request('GET', 'https://example.com/foo/bar');
114+
$response->toArray();
115+
}
103116
}

0 commit comments

Comments
 (0)