[HttpClient] Add a canceled state to the ResponseInterface
This commit is contained in:
parent
16d528504c
commit
bbb8ed11ff
@ -4,6 +4,7 @@ CHANGELOG
|
|||||||
4.4.0
|
4.4.0
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
* added `canceled` to `ResponseInterface::getInfo()`
|
||||||
* added `HttpClient::createForBaseUri()`
|
* added `HttpClient::createForBaseUri()`
|
||||||
* added `HttplugClient` with support for sync and async requests
|
* added `HttplugClient` with support for sync and async requests
|
||||||
* added `max_duration` option
|
* added `max_duration` option
|
||||||
|
@ -96,6 +96,7 @@ final class NativeHttpClient implements HttpClientInterface, LoggerAwareInterfac
|
|||||||
'response_headers' => [],
|
'response_headers' => [],
|
||||||
'url' => $url,
|
'url' => $url,
|
||||||
'error' => null,
|
'error' => null,
|
||||||
|
'canceled' => false,
|
||||||
'http_method' => $method,
|
'http_method' => $method,
|
||||||
'http_code' => 0,
|
'http_code' => 0,
|
||||||
'redirect_count' => 0,
|
'redirect_count' => 0,
|
||||||
|
@ -84,6 +84,7 @@ class MockResponse implements ResponseInterface
|
|||||||
*/
|
*/
|
||||||
public function cancel(): void
|
public function cancel(): void
|
||||||
{
|
{
|
||||||
|
$this->info['canceled'] = true;
|
||||||
$this->info['error'] = 'Response has been canceled.';
|
$this->info['error'] = 'Response has been canceled.';
|
||||||
$this->body = null;
|
$this->body = null;
|
||||||
}
|
}
|
||||||
|
@ -52,6 +52,7 @@ trait ResponseTrait
|
|||||||
'response_headers' => [],
|
'response_headers' => [],
|
||||||
'http_code' => 0,
|
'http_code' => 0,
|
||||||
'error' => null,
|
'error' => null,
|
||||||
|
'canceled' => false,
|
||||||
];
|
];
|
||||||
|
|
||||||
/** @var resource */
|
/** @var resource */
|
||||||
@ -178,6 +179,7 @@ trait ResponseTrait
|
|||||||
*/
|
*/
|
||||||
public function cancel(): void
|
public function cancel(): void
|
||||||
{
|
{
|
||||||
|
$this->info['canceled'] = true;
|
||||||
$this->info['error'] = 'Response has been canceled.';
|
$this->info['error'] = 'Response has been canceled.';
|
||||||
$this->close();
|
$this->close();
|
||||||
}
|
}
|
||||||
|
@ -105,6 +105,7 @@ class MockHttpClientTest extends HttpClientTestCase
|
|||||||
case 'testOnProgressError':
|
case 'testOnProgressError':
|
||||||
case 'testReentrantBufferCallback':
|
case 'testReentrantBufferCallback':
|
||||||
case 'testThrowingBufferCallback':
|
case 'testThrowingBufferCallback':
|
||||||
|
case 'testInfoOnCanceledResponse':
|
||||||
$responses[] = new MockResponse($body, ['response_headers' => $headers]);
|
$responses[] = new MockResponse($body, ['response_headers' => $headers]);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -88,15 +88,16 @@ interface ResponseInterface
|
|||||||
* another, as the request/response progresses.
|
* another, as the request/response progresses.
|
||||||
*
|
*
|
||||||
* The following info MUST be returned:
|
* The following info MUST be returned:
|
||||||
* - response_headers - an array modelled after the special $http_response_header variable
|
* - canceled (bool) - true if the response was canceled using ResponseInterface::cancel(), false otherwise
|
||||||
* - redirect_count - the number of redirects followed while executing the request
|
* - error (string|null) - the error message when the transfer was aborted, null otherwise
|
||||||
* - redirect_url - the resolved location of redirect responses, null otherwise
|
* - http_code (int) - the last response code or 0 when it is not known yet
|
||||||
* - start_time - the time when the request was sent or 0.0 when it's pending
|
* - http_method (string) - the HTTP verb of the last request
|
||||||
* - http_method - the HTTP verb of the last request
|
* - redirect_count (int) - the number of redirects followed while executing the request
|
||||||
* - http_code - the last response code or 0 when it is not known yet
|
* - redirect_url (string|null) - the resolved location of redirect responses, null otherwise
|
||||||
* - error - the error message when the transfer was aborted, null otherwise
|
* - response_headers (array) - an array modelled after the special $http_response_header variable
|
||||||
* - user_data - the value of the "user_data" request option, null if not set
|
* - start_time (float) - the time when the request was sent or 0.0 when it's pending
|
||||||
* - url - the last effective URL of the request
|
* - url (string) - the last effective URL of the request
|
||||||
|
* - user_data (mixed|null) - the value of the "user_data" request option, null if not set
|
||||||
*
|
*
|
||||||
* When the "capture_peer_cert_chain" option is true, the "peer_certificate_chain"
|
* When the "capture_peer_cert_chain" option is true, the "peer_certificate_chain"
|
||||||
* attribute SHOULD list the peer certificates as an array of OpenSSL X.509 resources.
|
* attribute SHOULD list the peer certificates as an array of OpenSSL X.509 resources.
|
||||||
|
@ -515,6 +515,17 @@ abstract class HttpClientTestCase extends TestCase
|
|||||||
$response->getHeaders();
|
$response->getHeaders();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testInfoOnCanceledResponse()
|
||||||
|
{
|
||||||
|
$client = $this->getHttpClient(__FUNCTION__);
|
||||||
|
|
||||||
|
$response = $client->request('GET', 'http://localhost:8057/timeout-header');
|
||||||
|
|
||||||
|
$this->assertFalse($response->getInfo('canceled'));
|
||||||
|
$response->cancel();
|
||||||
|
$this->assertTrue($response->getInfo('canceled'));
|
||||||
|
}
|
||||||
|
|
||||||
public function testCancelInStream()
|
public function testCancelInStream()
|
||||||
{
|
{
|
||||||
$client = $this->getHttpClient(__FUNCTION__);
|
$client = $this->getHttpClient(__FUNCTION__);
|
||||||
|
Reference in New Issue
Block a user