8000 bug #34186 [HttpClient] always return the empty string when the respo… · symfony/symfony@c424c5a · GitHub
[go: up one dir, main page]

Skip to content

Commit c424c5a

Browse files
bug #34186 [HttpClient] always return the empty string when the response cannot have a body (nicolas-grekas)
This PR was merged into the 4.3 branch. Discussion ---------- [HttpClient] always return the empty string when the response cannot have a body | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - This will fix tests on 4.4 also. Commits ------- f78e143 [HttpClient] always return the empty string when the response cannot have a body
2 parents 2b0a579 + f78e143 commit c424c5a

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

src/Symfony/Component/HttpClient/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) {

src/Symfony/Component/HttpClient/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());

src/Symfony/Contracts/HttpClient/Test/Fixtures/web/index.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,20 @@
2929
}
3030
}
3131

32+
$json = json_encode($vars, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
33+
3234
switch ($vars['REQUEST_URI']) {
3335
default:
3436
exit;
3537

38+
case '/head':
39+
header('Content-Length: '.strlen($json), true);
40+
break;
41+
3642
case '/':
3743
case '/?a=a&b=b':
3844
case 'http://127.0.0.1:8057/':
3945
case 'http://localhost:8057/':
40-
header('Content-Type: application/json');
4146
ob_start('ob_gzhandler');
4247
break;
4348

@@ -85,6 +90,7 @@
8590
case '/304':
8691
header('Content-Length: 10', true, 304);
8792
echo '12345';
93+
8894
return;
8995

9096
case '/307':
@@ -143,4 +149,4 @@
143149

144150
header('Content-Type: application/json', true);
145151

146-
echo json_encode($vars, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
152+
echo $json;

src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,24 @@ public function testGetRequest()
7575
public function testHeadRequest()
7676
{
7777
$client = $this->getHttpClient(__FUNCTION__);
78-
$response = $client->request('HEAD', 'http://localhost:8057', [
78+
$response = $client->request('HEAD', 'http://localhost:8057/head', [
7979
'headers' => ['Foo' => 'baR'],
8080
'user_data' => $data = new \stdClass(),
81+
'buffer' => false,
8182
]);
8283

8384
$this->assertSame([], $response->getInfo('response_headers'));
84-
$this->assertSame($data, $response->getInfo()['user_data']);
8585
$this->assertSame(200, $response->getStatusCode());
8686

8787
$info = $response->getInfo();
88-
$this->assertNull($info['error']);
89-
$this->assertSame(0, $info['redirect_count']);
9088
$this->assertSame('HTTP/1.1 200 OK', $info['response_headers'][0]);
9189
$this->assertSame('Host: localhost:8057', $info['response_headers'][1]);
92-
$this->assertSame('http://localhost:8057/', $info['url']);
9390

9491
$headers = $response->getHeaders();
9592

9693
$this->assertSame('localhost:8057', $headers['host'][0]);
9794
$this->assertSame(['application/json'], $headers['content-type']);
95+
$this->assertTrue(0 < $headers['content-length'][0]);
9896

9997
$this->assertSame('', $response->getContent());
10098
}
@@ -265,6 +263,7 @@ public function test304()
265263
$client = $this->getHttpClient(__FUNCTION__);
266264
$response = $client->request('GET', 'http://localhost:8057/304', [
267265
'headers' => ['If-Match' => '"abc"'],
266+
'buffer' => false,
268267
]);
269268

270269
$this->assertSame(304, $response->getStatusCode());

0 commit comments

Comments
 (0)
0