Skip to content

Commit 4dbd08b

Browse files
[HttpClient] always return the empty string when the response cannot have a body
1 parent 4296c86 commit 4dbd08b

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

Response/ResponseTrait.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,15 @@ public function getContent(bool $throw = true): string
112112
}
113113
}
114114

115-
if (null === $content) {
116-
throw new TransportException('Cannot get the content of the response twice: the request was issued with option "buffer" set to false.');
115+
if (null !== $content) {
116+
return $content;
117117
}
118118

119-
return $content;
119+
if ('HEAD' === $this->info['http_method'] || \in_array($this->info['http_code'], [204, 304], true)) {
120+
return '';
121+
}
122+
123+
throw new TransportException('Cannot get the content of the response twice: the request was issued with option "buffer" set to false.');
120124
}
121125

122126
foreach (self::stream([$this]) as $chunk) {

Tests/MockHttpClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected function getHttpClient(string $testCase): HttpClientInterface
4848
return new MockHttpClient(function (string $method, string $url, array $options) use ($client) {
4949
try {
5050
// force the request to be completed so that we don't test side effects of the transport
51-
$response = $client->request($method, $url, $options);
51+
$response = $client->request($method, $url, ['buffer' => false] + $options);
5252
$content = $response->getContent(false);
5353

5454
return new MockResponse($content, $response->getInfo());

0 commit comments

Comments
 (0)