Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f78e143

Browse filesBrowse files
[HttpClient] always return the empty string when the response cannot have a body
1 parent 2b0a579 commit f78e143
Copy full SHA for f78e143

File tree

Expand file treeCollapse file tree

4 files changed

+20
-11
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+20
-11
lines changed

‎src/Symfony/Component/HttpClient/Response/ResponseTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Response/ResponseTrait.php
+7-3Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Contracts/HttpClient/Test/Fixtures/web/index.php
+8-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php
+4-5Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.