Skip to content

Commit 62e1f0f

Browse files
committed
Merge branch '5.1' into 5.x
2 parents 281d6be + 2a57516 commit 62e1f0f

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

Response/TraceableResponse.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ public function getContent(bool $throw = true): string
9595
if ($this->event && $this->event->isStarted()) {
9696
$this->event->stop();
9797
}
98+
99+
if ($throw) {
100+
$this->checkStatusCode($this->response->getStatusCode());
101+
}
98102
}
99103
}
100104

@@ -116,6 +120,10 @@ public function toArray(bool $throw = true): array
116120
if ($this->event && $this->event->isStarted()) {
117121
$this->event->stop();
118122
}
123+
124+
if ($throw) {
125+
$this->checkStatusCode($this->response->getStatusCode());
126+
}
119127
}
120128
}
121129

Tests/TraceableHttpClientTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\HttpClient\Response\MockResponse;
1919
use Symfony\Component\HttpClient\TraceableHttpClient;
2020
use Symfony\Component\Stopwatch\Stopwatch;
21+
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
2122
use Symfony\Contracts\HttpClient\HttpClientInterface;
2223
use Symfony\Contracts\HttpClient\Test\TestHttpServer;
2324

@@ -118,6 +119,18 @@ public function testStream()
118119
$this->assertSame('Symfony is awesome!', implode('', $chunks));
119120
}
120121

122+
public function testToArrayChecksStatusCodeBeforeDecoding()
123+
{
124+
$this->expectException(ClientExceptionInterface::class);
125+
126+
$sut = new TraceableHttpClient(new MockHttpClient($responseFactory = function (): MockResponse {
127+
return new MockResponse('Errored.', ['http_code' => 400]);
128+
}));
129+
130+
$response = $sut->request('GET', 'https://example.com/foo/bar');
131+
$response->toArray();
132+
}
133+
121134
public function testStopwatch()
122135
{
123136
$sw = new Stopwatch(true);

0 commit comments

Comments
 (0)