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

Skip to content

Commit f130731

Browse files
committed
[HttpClient] Fix MockResponse in case of header timeout
1 parent 90911f1 commit f130731

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ protected static function perform(ClientState $multi, array &$responses): void
203203
$chunk[1]->getStatusCode();
204204
$chunk[1]->getHeaders(false);
205205
self::readResponse($response, $chunk[0], $chunk[1], $offset);
206-
$multi->handlesActivity[$id][] = new FirstChunk();
206+
$multi->handlesActivity[$id][] = ($response->body[0] ?? null) instanceof ErrorChunk ? $response->body[0] : new FirstChunk();
207207
$buffer = $response->requestOptions['buffer'] ?? null;
208208

209209
if ($buffer instanceof \Closure && $response->content = $buffer($response->headers) ?: null) {

src/Symfony/Component/HttpClient/Tests/Response/MockResponseTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\HttpClient\Exception\JsonException;
16+
use Symfony\Component\HttpClient\Exception\TimeoutException;
1617
use Symfony\Component\HttpClient\Exception\TransportException;
1718
use Symfony\Component\HttpClient\Response\MockResponse;
1819

@@ -124,4 +125,34 @@ public function testCancelingAMockResponseNotIssuedByMockHttpClient()
124125

125126
$this->assertTrue($mockResponse->getInfo('canceled'));
126127
}
128+
129+
public function testTimeoutHeader()
130+
{
131+
$response = MockResponse::fromRequest('GET', 'http://symfony.com', [], new MockResponse(['']));
132+
133+
try {
134+
$response->getStatusCode();
135+
$this->fail(TimeoutException::class.' expected');
136+
} catch (TimeoutException $e) {
137+
}
138+
139+
$this->assertSame('Idle timeout reached for "http://symfony.com".', $response->getInfo('error'));
140+
}
141+
142+
public function testTimeoutBody()
143+
{
144+
$response = MockResponse::fromRequest('GET', 'http://symfony.com', [], new MockResponse(['content', '']));
145+
146+
try {
147+
$this->assertSame(200, $response->getStatusCode());
148+
} catch (TimeoutException $e) {
149+
$this->fail(TimeoutException::class.' was not expected');
150+
}
151+
152+
try {
153+
$response->getContent();
154+
$this->fail(TimeoutException::class.' expected');
155+
} catch (TimeoutException $e) {
156+
}
157+
}
127158
}

0 commit comments

Comments
 (0)