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

Skip to content

Commit 2d6c309

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

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,11 @@ public function getRequestMethod(): string
9090
return $this->requestMethod;
9191
}
9292

93-
/**
94-
* {@inheritdoc}
95-
*/
9693
public function getInfo(?string $type = null)
9794
{
9895
return null !== $type ? $this->info[$type] ?? null : $this->info;
9996
}
10097

101-
/**
102-
* {@inheritdoc}
103-
*/
10498
public function cancel(): void
10599
{
106100
$this->info['canceled'] = true;
@@ -116,9 +110,6 @@ public function cancel(): void
116110
$onProgress($this->offset, $dlSize, $this->info);
117111
}
118112

119-
/**
120-
* {@inheritdoc}
121-
*/
122113
protected function close(): void
123114
{
124115
$this->inflate = null;
@@ -159,9 +150,6 @@ public static function fromRequest(string $method, string $url, array $options,
159150
return $response;
160151
}
161152

162-
/**
163-
* {@inheritdoc}
164-
*/
165153
protected static function schedule(self $response, array &$runningResponses): void
166154
{
167155
if (!$response->id) {
@@ -203,7 +191,7 @@ protected static function perform(ClientState $multi, array &$responses): void
203191
$chunk[1]->getStatusCode();
204192
$chunk[1]->getHeaders(false);
205193
self::readResponse($response, $chunk[0], $chunk[1], $offset);
206-
$multi->handlesActivity[$id][] = new FirstChunk();
194+
$multi->handlesActivity[$id][] = ($response->body[0] ?? null) instanceof ErrorChunk ? $response->body[0] : new FirstChunk();
207195
$buffer = $response->requestOptions['buffer'] ?? null;
208196

209197
if ($buffer instanceof \Closure && $response->content = $buffer($response->headers) ?: null) {
@@ -223,9 +211,6 @@ protected static function perform(ClientState $multi, array &$responses): void
223211
}
224212
}
225213

226-
/**
227-
* {@inheritdoc}
228-
*/
229214
protected static function select(ClientState $multi, float $timeout): int
230215
{
231216
return 42;

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)