Skip to content

Commit

Permalink
bug #34186 [HttpClient] always return the empty string when the respo…
Browse files Browse the repository at this point in the history
…nse 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
  • Loading branch information
nicolas-grekas committed Oct 30, 2019
2 parents 2b0a579 + f78e143 commit c424c5a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
10 changes: 7 additions & 3 deletions src/Symfony/Component/HttpClient/Response/ResponseTrait.php
Expand Up @@ -112,11 +112,15 @@ public function getContent(bool $throw = true): string
}
}

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

return $content;
if ('HEAD' === $this->info['http_method'] || \in_array($this->info['http_code'], [204, 304], true)) {
return '';
}

throw new TransportException('Cannot get the content of the response twice: the request was issued with option "buffer" set to false.');
}

foreach (self::stream([$this]) as $chunk) {
Expand Down
Expand Up @@ -48,7 +48,7 @@ protected function getHttpClient(string $testCase): HttpClientInterface
return new MockHttpClient(function (string $method, string $url, array $options) use ($client) {
try {
// force the request to be completed so that we don't test side effects of the transport
$response = $client->request($method, $url, $options);
$response = $client->request($method, $url, ['buffer' => false] + $options);
$content = $response->getContent(false);

return new MockResponse($content, $response->getInfo());
Expand Down
10 changes: 8 additions & 2 deletions src/Symfony/Contracts/HttpClient/Test/Fixtures/web/index.php
Expand Up @@ -29,15 +29,20 @@
}
}

$json = json_encode($vars, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

switch ($vars['REQUEST_URI']) {
default:
exit;

case '/head':
header('Content-Length: '.strlen($json), true);
break;

case '/':
case '/?a=a&b=b':
case 'http://127.0.0.1:8057/':
case 'http://localhost:8057/':
header('Content-Type: application/json');
ob_start('ob_gzhandler');
break;

Expand Down Expand Up @@ -85,6 +90,7 @@
case '/304':
header('Content-Length: 10', true, 304);
echo '12345';

return;

case '/307':
Expand Down Expand Up @@ -143,4 +149,4 @@

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

echo json_encode($vars, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
echo $json;
9 changes: 4 additions & 5 deletions src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php
Expand Up @@ -75,26 +75,24 @@ public function testGetRequest()
public function testHeadRequest()
{
$client = $this->getHttpClient(__FUNCTION__);
$response = $client->request('HEAD', 'http://localhost:8057', [
$response = $client->request('HEAD', 'http://localhost:8057/head', [
'headers' => ['Foo' => 'baR'],
'user_data' => $data = new \stdClass(),
'buffer' => false,
]);

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

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

$headers = $response->getHeaders();

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

$this->assertSame('', $response->getContent());
}
Expand Down Expand Up @@ -265,6 +263,7 @@ public function test304()
$client = $this->getHttpClient(__FUNCTION__);
$response = $client->request('GET', 'http://localhost:8057/304', [
'headers' => ['If-Match' => '"abc"'],
'buffer' => false,
]);

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

0 comments on commit c424c5a

Please sign in to comment.