Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d635d3b

Browse filesBrowse files
committed
Fix exception triggered with AsyncResponse
1 parent d0ded92 commit d635d3b
Copy full SHA for d635d3b

File tree

3 files changed

+39
-5
lines changed
Filter options

3 files changed

+39
-5
lines changed

‎src/Symfony/Component/HttpClient/Response/AsyncResponse.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Response/AsyncResponse.php
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ public static function stream(iterable $responses, float $timeout = null, string
206206
foreach ($client->stream($wrappedResponses, $timeout) as $response => $chunk) {
207207
$r = $asyncMap[$response];
208208

209+
if (null === $chunk->getError() && $chunk->isFirst()) {
210+
// Ensure no exception is thrown on destruct for the wrapped response
211+
$r->response->getStatusCode();
212+
}
213+
209214
if (!$r->passthru) {
210215
if (null !== $chunk->getError() || $chunk->isLast()) {
211216
unset($asyncMap[$response]);
@@ -219,11 +224,6 @@ public static function stream(iterable $responses, float $timeout = null, string
219224
continue;
220225
}
221226

222-
if (null === $chunk->getError() && $chunk->isFirst()) {
223-
// Ensure no exception is thrown on destruct for the wrapped response
224-
$r->response->getStatusCode();
225-
}
226-
227227
foreach (self::passthru($r->client, $r, $chunk, $asyncMap) as $chunk) {
228228
yield $r => $chunk;
229229
}

‎src/Symfony/Component/HttpClient/Response/CommonResponseTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Response/CommonResponseTrait.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public function getContent(bool $throw = true): string
5454
foreach (self::stream([$this]) as $chunk) {
5555
if (!$chunk->isLast()) {
5656
$content .= $chunk->getContent();
57+
} elseif (null === $content) {
58+
$content = '';
5759
}
5860
}
5961

‎src/Symfony/Component/HttpClient/Tests/AsyncDecoratorTraitTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Tests/AsyncDecoratorTraitTest.php
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,38 @@ public function testRetry404()
6262
$this->assertSame(200, $response->getStatusCode());
6363
}
6464

65+
public function testRetry404WithoutThrow()
66+
{
67+
$client = $this->getHttpClient(__FUNCTION__, function (ChunkInterface $chunk, AsyncContext $context) {
68+
$this->assertTrue($chunk->isFirst());
69+
$this->assertSame(404, $context->getStatusCode());
70+
$context->getResponse()->cancel();
71+
$context->replaceRequest('GET', 'http://localhost:8057/404');
72+
$context->passthru();
73+
});
74+
75+
$response = $client->request('GET', 'http://localhost:8057/404');
76+
// This should not trigger exception
77+
$response->getContent(false);
78+
}
79+
80+
public function testRetry404WithThrow()
81+
{
82+
$client = $this->getHttpClient(__FUNCTION__, function (ChunkInterface $chunk, AsyncContext $context) {
83+
$this->assertTrue($chunk->isFirst());
84+
$this->assertSame(404, $context->getStatusCode());
85+
$context->getResponse()->cancel();
86+
$context->replaceRequest('GET', 'http://localhost:8057/404');
87+
$context->passthru();
88+
});
89+
90+
$response = $client->request('GET', 'http://localhost:8057/404');
91+
92+
$this->expectException('Symfony\Component\HttpClient\Exception\ClientException');
93+
// This should trigger exception
94+
$response->getContent(true);
95+
}
96+
6597
public function testRetryTransportError()
6698
{
6799
$client = $this->getHttpClient(__FUNCTION__, function (ChunkInterface $chunk, AsyncContext $context) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.