Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e5b082a

Browse files
committed
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 | symfony/symfony-docs#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 ------- c402418 [HttpClient] add $response->cancel()
2 parents 28fbf16 + c402418 commit e5b082a

File tree

5 files changed

+26
-2
lines changed

5 files changed

+26
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"psr/container": "^1.0",
2727
"psr/link": "^1.0",
2828
"psr/log": "~1.0",
29-
"symfony/contracts": "^1.1.1",
29+
"symfony/contracts": "^1.1.3",
3030
"symfony/polyfill-ctype": "~1.8",
3131
"symfony/polyfill-intl-icu": "~1.0",
3232
"symfony/polyfill-intl-idn": "^1.10",

src/Symfony/Component/HttpClient/Response/ResponseTrait.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ public function toArray(bool $throw = true): array
169169
return $content;
170170
}
171171

172+
/**
173+
* {@inheritdoc}
174+
*/
175+
public function cancel(): void
176+
{
177+
$this->info['error'] = 'Response has been canceled.';
178+
$this->close();
179+
}
180+
172181
/**
173182
* Closes the response and all its network handles.
174183
*/

src/Symfony/Component/HttpClient/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"require": {
2222
"php": "^7.1.3",
2323
"psr/log": "^1.0",
24-
"symfony/http-client-contracts": "^1.1",
24+
"symfony/http-client-contracts": "^1.1.3",
2525
"symfony/polyfill-php73": "^1.11"
2626
},
2727
"require-dev": {

src/Symfony/Contracts/HttpClient/ResponseInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public function getContent(bool $throw = true): string;
7171
*/
7272
public function toArray(bool $throw = true): array;
7373

74+
/**
75+
* Cancels the response.
76+
*/
77+
public function cancel(): void;
78+
7479
/**
7580
* Returns info coming from the transport layer.
7681
*

src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,16 @@ public function testPostCallback()
495495
$this->assertSame(['foo' => '0123456789', 'REQUEST_METHOD' => 'POST'], $response->toArray());
496496
}
497497

498+
public function testCancel()
499+
{
500+
$client = $this->getHttpClient(__FUNCTION__);
501+
$response = $client->request('GET', 'http://localhost:8057/timeout-header');
502+
503+
$response->cancel();
504+
$this->expectException(TransportExceptionInterface::class);
505+
$response->getHeaders();
506+
}
507+
498508
public function testOnProgressCancel()
499509
{
500510
$client = $this->getHttpClient(__FUNCTION__);

0 commit comments

Comments
 (0)