8000 [HttpClient] Fix not calling the on progress callback when canceling a MockResponse by fancyweb · Pull Request #49796 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpClient] Fix not calling the on progress callback when canceling a MockResponse #49796

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
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
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpClient/Response/MockResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ public function cancel(): void
} catch (TransportException $e) {
// ignore errors when canceling
}

$onProgress = $this->requestOptions['on_progress'] ?? static function () {};
$dlSize = isset($this->headers['content-encoding']) || 'HEAD' === $this->info['http_method'] || \in_array($this->info['http_code'], [204, 304], true) ? 0 : (int) ($this->headers['content-length'][0] ?? 0);
$onProgress($this->offset, $dlSize, $this->info);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,4 +506,25 @@ public function testResetsRequestCount()
$client->reset();
$this->assertSame(0, $client->getRequestsCount());
}

public function testCancellingMockResponseExecutesOnProgressWithUpdatedInfo()
{
$client = new MockHttpClient(new MockResponse(['foo', 'bar', 'ccc']));
$canceled = false;
$response = $client->request('GET', 'https://example.com', [
'on_progress' => static function (int $dlNow, int $dlSize, array $info) use (&$canceled): void {
$canceled = $info['canceled'];
},
]);

foreach ($client->stream($response) as $response => $chunk) {
if ('bar' === $chunk->getContent()) {
$response->cancel();

break;
}
}

$this->assertTrue($canceled);
}
}
0