Skip to content

[HttpClient] Fix MockResponse in case of header timeout #53625

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

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 1 addition & 19 deletions src/Symfony/Component/HttpClient/Response/MockResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,11 @@ public function getRequestMethod(): string
return $this->requestMethod;
}

/**
* {@inheritdoc}
*/
public function getInfo(?string $type = null)
{
return null !== $type ? $this->info[$type] ?? null : $this->info;
}

/**
* {@inheritdoc}
*/
public function cancel(): void
{
$this->info['canceled'] = true;
Expand All @@ -116,9 +110,6 @@ public function cancel(): void
$onProgress($this->offset, $dlSize, $this->info);
}

/**
* {@inheritdoc}
*/
protected function close(): void
{
$this->inflate = null;
Expand Down Expand Up @@ -159,9 +150,6 @@ public static function fromRequest(string $method, string $url, array $options,
return $response;
}

/**
* {@inheritdoc}
*/
protected static function schedule(self $response, array &$runningResponses): void
{
if (!$response->id) {
Expand All @@ -177,9 +165,6 @@ protected static function schedule(self $response, array &$runningResponses): vo
$runningResponses[0][1][$response->id] = $response;
}

/**
* {@inheritdoc}
*/
protected static function perform(ClientState $multi, array &$responses): void
{
foreach ($responses as $response) {
Expand All @@ -203,7 +188,7 @@ protected static function perform(ClientState $multi, array &$responses): void
$chunk[1]->getStatusCode();
$chunk[1]->getHeaders(false);
self::readResponse($response, $chunk[0], $chunk[1], $offset);
$multi->handlesActivity[$id][] = new FirstChunk();
$multi->handlesActivity[$id][] = ($response->body[0] ?? null) instanceof ErrorChunk ? $response->body[0] : new FirstChunk();
$buffer = $response->requestOptions['buffer'] ?? null;

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

/**
* {@inheritdoc}
*/
protected static function select(ClientState $multi, float $timeout): int
{
return 42;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\Exception\JsonException;
use Symfony\Component\HttpClient\Exception\TimeoutException;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\Response\MockResponse;

Expand Down Expand Up @@ -124,4 +125,34 @@ public function testCancelingAMockResponseNotIssuedByMockHttpClient()

$this->assertTrue($mockResponse->getInfo('canceled'));
}

public function testTimeoutHeader()
{
$response = MockResponse::fromRequest('GET', 'http://symfony.com', [], new MockResponse(['']));

try {
$response->getStatusCode();
$this->fail(TimeoutException::class.' expected');
} catch (TimeoutException $e) {
}

$this->assertSame('Idle timeout reached for "http://symfony.com".', $response->getInfo('error'));
}

public function testTimeoutBody()
{
$response = MockResponse::fromRequest('GET', 'http://symfony.com', [], new MockResponse(['content', '']));

try {
$this->assertSame(200, $response->getStatusCode());
} catch (TimeoutException $e) {
$this->fail(TimeoutException::class.' was not expected');
}

try {
$response->getContent();
$this->fail(TimeoutException::class.' expected');
} catch (TimeoutException $e) {
}
}
}