[HttpClient] always return the empty string when the response cannot have a body
This commit is contained in:
parent
2b0a5793d1
commit
f78e14332e
@ -112,11 +112,15 @@ trait ResponseTrait
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null === $content) {
|
if (null !== $content) {
|
||||||
throw new TransportException('Cannot get the content of the response twice: the request was issued with option "buffer" set to false.');
|
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) {
|
foreach (self::stream([$this]) as $chunk) {
|
||||||
|
@ -48,7 +48,7 @@ class MockHttpClientTest extends HttpClientTestCase
|
|||||||
return new MockHttpClient(function (string $method, string $url, array $options) use ($client) {
|
return new MockHttpClient(function (string $method, string $url, array $options) use ($client) {
|
||||||
try {
|
try {
|
||||||
// force the request to be completed so that we don't test side effects of the transport
|
// 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);
|
$content = $response->getContent(false);
|
||||||
|
|
||||||
return new MockResponse($content, $response->getInfo());
|
return new MockResponse($content, $response->getInfo());
|
||||||
|
@ -29,15 +29,20 @@ foreach ($_SERVER as $k => $v) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$json = json_encode($vars, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||||
|
|
||||||
switch ($vars['REQUEST_URI']) {
|
switch ($vars['REQUEST_URI']) {
|
||||||
default:
|
default:
|
||||||
exit;
|
exit;
|
||||||
|
|
||||||
|
case '/head':
|
||||||
|
header('Content-Length: '.strlen($json), true);
|
||||||
|
break;
|
||||||
|
|
||||||
case '/':
|
case '/':
|
||||||
case '/?a=a&b=b':
|
case '/?a=a&b=b':
|
||||||
case 'http://127.0.0.1:8057/':
|
case 'http://127.0.0.1:8057/':
|
||||||
case 'http://localhost:8057/':
|
case 'http://localhost:8057/':
|
||||||
header('Content-Type: application/json');
|
|
||||||
ob_start('ob_gzhandler');
|
ob_start('ob_gzhandler');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -85,6 +90,7 @@ switch ($vars['REQUEST_URI']) {
|
|||||||
case '/304':
|
case '/304':
|
||||||
header('Content-Length: 10', true, 304);
|
header('Content-Length: 10', true, 304);
|
||||||
echo '12345';
|
echo '12345';
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case '/307':
|
case '/307':
|
||||||
@ -143,4 +149,4 @@ switch ($vars['REQUEST_URI']) {
|
|||||||
|
|
||||||
header('Content-Type: application/json', true);
|
header('Content-Type: application/json', true);
|
||||||
|
|
||||||
echo json_encode($vars, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
echo $json;
|
||||||
|
@ -75,26 +75,24 @@ abstract class HttpClientTestCase extends TestCase
|
|||||||
public function testHeadRequest()
|
public function testHeadRequest()
|
||||||
{
|
{
|
||||||
$client = $this->getHttpClient(__FUNCTION__);
|
$client = $this->getHttpClient(__FUNCTION__);
|
||||||
$response = $client->request('HEAD', 'http://localhost:8057', [
|
$response = $client->request('HEAD', 'http://localhost:8057/head', [
|
||||||
'headers' => ['Foo' => 'baR'],
|
'headers' => ['Foo' => 'baR'],
|
||||||
'user_data' => $data = new \stdClass(),
|
'user_data' => $data = new \stdClass(),
|
||||||
|
'buffer' => false,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertSame([], $response->getInfo('response_headers'));
|
$this->assertSame([], $response->getInfo('response_headers'));
|
||||||
$this->assertSame($data, $response->getInfo()['user_data']);
|
|
||||||
$this->assertSame(200, $response->getStatusCode());
|
$this->assertSame(200, $response->getStatusCode());
|
||||||
|
|
||||||
$info = $response->getInfo();
|
$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('HTTP/1.1 200 OK', $info['response_headers'][0]);
|
||||||
$this->assertSame('Host: localhost:8057', $info['response_headers'][1]);
|
$this->assertSame('Host: localhost:8057', $info['response_headers'][1]);
|
||||||
$this->assertSame('http://localhost:8057/', $info['url']);
|
|
||||||
|
|
||||||
$headers = $response->getHeaders();
|
$headers = $response->getHeaders();
|
||||||
|
|
||||||
$this->assertSame('localhost:8057', $headers['host'][0]);
|
$this->assertSame('localhost:8057', $headers['host'][0]);
|
||||||
$this->assertSame(['application/json'], $headers['content-type']);
|
$this->assertSame(['application/json'], $headers['content-type']);
|
||||||
|
$this->assertTrue(0 < $headers['content-length'][0]);
|
||||||
|
|
||||||
$this->assertSame('', $response->getContent());
|
$this->assertSame('', $response->getContent());
|
||||||
}
|
}
|
||||||
@ -265,6 +263,7 @@ abstract class HttpClientTestCase extends TestCase
|
|||||||
$client = $this->getHttpClient(__FUNCTION__);
|
$client = $this->getHttpClient(__FUNCTION__);
|
||||||
$response = $client->request('GET', 'http://localhost:8057/304', [
|
$response = $client->request('GET', 'http://localhost:8057/304', [
|
||||||
'headers' => ['If-Match' => '"abc"'],
|
'headers' => ['If-Match' => '"abc"'],
|
||||||
|
'buffer' => false,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertSame(304, $response->getStatusCode());
|
$this->assertSame(304, $response->getStatusCode());
|
||||||
|
Reference in New Issue
Block a user