bug #31850 [HttpClient] add $response->cancel() (nicolas-grekas)

This PR was merged into the 4.3 branch.

Discussion
----------

[HttpClient] add $response->cancel()

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | https://github.com/symfony/symfony-docs/pull/11668

An alternative to #31845 and #31842.
Same as  #31831 but considered as a bug fix (at the Contracts level), thus for 4.3.
I think we're early enough since 4.3/1.1 to do it.
That will save us some headaches in the short term.

Commits
-------

c402418723 [HttpClient] add $response->cancel()
This commit is contained in:
Fabien Potencier 2019-06-05 15:19:12 +02:00
commit e5b082acee
5 changed files with 26 additions and 2 deletions

View File

@ -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",

View File

@ -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.
*/

View File

@ -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": {

View File

@ -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.
*

View File

@ -495,6 +495,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__);