Skip to content

Commit 4cd1b7e

Browse files
committed
[HttpClient] Fix not calling the on progress callback when canceling a MockResponse
1 parent a2db7ef commit 4cd1b7e

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

Response/MockResponse.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ public function cancel(): void
110110
} catch (TransportException $e) {
111111
// ignore errors when canceling
112112
}
113+
114+
$onProgress = $this->requestOptions['on_progress'] ?? static function () {};
115+
$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);
116+
$onProgress($this->offset, $dlSize, $this->info);
113117
}
114118

115119
/**

Tests/MockHttpClientTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,4 +506,25 @@ public function testResetsRequestCount()
506506
$client->reset();
507507
$this->assertSame(0, $client->getRequestsCount());
508508
}
509+
510+
public function testCancellingMockResponseExecutesOnProgressWithUpdatedInfo()
511+
{
512+
$client = new MockHttpClient(new MockResponse(['foo', 'bar', 'ccc']));
513+
$canceled = false;
514+
$response = $client->request('GET', 'https://example.com', [
515+
'on_progress' => static function (int $dlNow, int $dlSize, array $info) use (&$canceled): void {
516+
$canceled = $info['canceled'];
517+
},
518+
]);
519+
520+
foreach ($client->stream($response) as $response => $chunk) {
521+
if ('bar' === $chunk->getContent()) {
522+
$response->cancel();
523+
524+
break;
525+
}
526+
}
527+
528+
$this->assertTrue($canceled);
529+
}
509530
}

0 commit comments

Comments
 (0)