From c402418723b965edd60790a2c69e93ca6e46083a Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 3 Jun 2019 19:56:51 +0200 Subject: [PATCH] [HttpClient] add $response->cancel() --- composer.json | 2 +- .../Component/HttpClient/Response/ResponseTrait.php | 9 +++++++++ src/Symfony/Component/HttpClient/composer.json | 2 +- src/Symfony/Contracts/HttpClient/ResponseInterface.php | 5 +++++ .../Contracts/HttpClient/Test/HttpClientTestCase.php | 10 ++++++++++ 5 files changed, 26 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 4824ffbb24..572f5f842d 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "psr/container": "^1.0", "psr/link": "^1.0", "psr/log": "~1.0", - "symfony/contracts": "^1.1.1", + "symfony/contracts": "^1.1.3", "symfony/polyfill-ctype": "~1.8", "symfony/polyfill-intl-icu": "~1.0", "symfony/polyfill-intl-idn": "^1.10", diff --git a/src/Symfony/Component/HttpClient/Response/ResponseTrait.php b/src/Symfony/Component/HttpClient/Response/ResponseTrait.php index 79cc40d5d8..e1777c3e12 100644 --- a/src/Symfony/Component/HttpClient/Response/ResponseTrait.php +++ b/src/Symfony/Component/HttpClient/Response/ResponseTrait.php @@ -169,6 +169,15 @@ trait ResponseTrait return $content; } + /** + * {@inheritdoc} + */ + public function cancel(): void + { + $this->info['error'] = 'Response has been canceled.'; + $this->close(); + } + /** * Closes the response and all its network handles. */ diff --git a/src/Symfony/Component/HttpClient/composer.json b/src/Symfony/Component/HttpClient/composer.json index 4ffe7bfd82..bda296a9d9 100644 --- a/src/Symfony/Component/HttpClient/composer.json +++ b/src/Symfony/Component/HttpClient/composer.json @@ -21,7 +21,7 @@ "require": { "php": "^7.1.3", "psr/log": "^1.0", - "symfony/http-client-contracts": "^1.1", + "symfony/http-client-contracts": "^1.1.3", "symfony/polyfill-php73": "^1.11" }, "require-dev": { diff --git a/src/Symfony/Contracts/HttpClient/ResponseInterface.php b/src/Symfony/Contracts/HttpClient/ResponseInterface.php index 6751184b87..a7e01be84c 100644 --- a/src/Symfony/Contracts/HttpClient/ResponseInterface.php +++ b/src/Symfony/Contracts/HttpClient/ResponseInterface.php @@ -71,6 +71,11 @@ interface ResponseInterface */ public function toArray(bool $throw = true): array; + /** + * Cancels the response. + */ + public function cancel(): void; + /** * Returns info coming from the transport layer. * diff --git a/src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php b/src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php index b898ba55c6..a5ea76f256 100644 --- a/src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php +++ b/src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php @@ -481,6 +481,16 @@ abstract class HttpClientTestCase extends TestCase $this->assertSame(['foo' => '0123456789', 'REQUEST_METHOD' => 'POST'], $response->toArray()); } + public function testCancel() + { + $client = $this->getHttpClient(__FUNCTION__); + $response = $client->request('GET', 'http://localhost:8057/timeout-header'); + + $response->cancel(); + $this->expectException(TransportExceptionInterface::class); + $response->getHeaders(); + } + public function testOnProgressCancel() { $client = $this->getHttpClient(__FUNCTION__);