Skip to content

[HttpClient] Add a canceled state to the ResponseInterface #34044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpClient/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
4.4.0
-----

* added `canceled` to `ResponseInterface::getInfo()`
* added `HttpClient::createForBaseUri()`
* added `HttplugClient` with support for sync and async requests
* added `max_duration` option
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpClient/NativeHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public function request(string $method, string $url, array $options = []): Respo
'response_headers' => [],
'url' => $url,
'error' => null,
'canceled' => false,
'http_method' => $method,
'http_code' => 0,
'redirect_count' => 0,
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpClient/Response/MockResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function getInfo(string $type = null)
*/
public function cancel(): void
{
$this->info['canceled'] = true;
$this->info['error'] = 'Response has been canceled.';
$this->body = null;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/HttpClient/Response/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ trait ResponseTrait
'response_headers' => [],
'http_code' => 0,
'error' => null,
'canceled' => false,
];

/** @var resource */
Expand Down Expand Up @@ -178,6 +179,7 @@ public function toArray(bool $throw = true): array
*/
public function cancel(): void
{
$this->info['canceled'] = true;
$this->info['error'] = 'Response has been canceled.';
$this->close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ protected function getHttpClient(string $testCase): HttpClientInterface
case 'testOnProgressError':
case 'testReentrantBufferCallback':
case 'testThrowingBufferCallback':
case 'testInfoOnCanceledResponse':
$responses[] = new MockResponse($body, ['response_headers' => $headers]);
break;

Expand Down
19 changes: 10 additions & 9 deletions src/Symfony/Contracts/HttpClient/ResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,16 @@ public function cancel(): void;
* another, as the request/response progresses.
*
* The following info MUST be returned:
* - response_headers - an array modelled after the special $http_response_header variable
* - redirect_count - the number of redirects followed while executing the request
* - redirect_url - the resolved location of redirect responses, null otherwise
* - start_time - the time when the request was sent or 0.0 when it's pending
* - http_method - the HTTP verb of the last request
* - http_code - the last response code or 0 when it is not known yet
* - error - the error message when the transfer was aborted, null otherwise
* - user_data - the value of the "user_data" request option, null if not set
* - url - the last effective URL of the request
* - canceled (bool) - true if the response was canceled using ResponseInterface::cancel(), false otherwise
* - error (string|null) - the error message when the transfer was aborted, null otherwise
* - http_code (int) - the last response code or 0 when it is not known yet
* - http_method (string) - the HTTP verb of the last request
* - redirect_count (int) - the number of redirects followed while executing the request
* - redirect_url (string|null) - the resolved location of redirect responses, null otherwise
* - response_headers (array) - an array modelled after the special $http_response_header variable
* - start_time (float) - the time when the request was sent or 0.0 when it's pending
* - 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"
* attribute SHOULD list the peer certificates as an array of OpenSSL X.509 resources.
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,17 @@ public function testCancel()
$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()
{
$client = $this->getHttpClient(__FUNCTION__);
Expand Down