Skip to content

[HttpClient] add MockHttpClient #30604

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 1 commit into from
Mar 19, 2019
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
17 changes: 10 additions & 7 deletions src/Symfony/Component/HttpClient/Chunk/ErrorChunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,14 @@
*/
class ErrorChunk implements ChunkInterface
{
protected $didThrow;

private $didThrow = false;
private $offset;
private $errorMessage;
private $error;

/**
* @param bool &$didThrow Allows monitoring when the $error has been thrown or not
*/
public function __construct(bool &$didThrow, int $offset, \Throwable $error = null)
public function __construct(int $offset, \Throwable $error = null)
{
$didThrow = false;
$this->didThrow = &$didThrow;
$this->offset = $offset;
$this->error = $error;
$this->errorMessage = null !== $error ? $error->getMessage() : 'Reading from the response stream reached the inactivity timeout.';
Expand Down Expand Up @@ -96,6 +91,14 @@ public function getError(): ?string
return $this->errorMessage;
}

/**
* @return bool Whether the wrapped error has been thrown or not
*/
public function didThrow(): bool
{
return $this->didThrow;
}

public function __destruct()
{
if (!$this->didThrow) {
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/HttpClient/HttpClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private static function prepareRequest(?string $method, ?string $url, array $opt

// Finalize normalization of options
$options['headers'] = $headers;
$options['http_version'] = (string) ($options['http_version'] ?? '');
$options['http_version'] = (string) ($options['http_version'] ?? '') ?: null;
$options['timeout'] = (float) ($options['timeout'] ?? ini_get('default_socket_timeout'));

return [$url, $options];
Expand All @@ -128,6 +128,8 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
*/
private static function mergeDefaultOptions(array $options, array $defaultOptions, bool $allowExtraOptions = false): array
{
unset($options['raw_headers'], $defaultOptions['raw_headers']);

$options['headers'] = self::normalizeHeaders($options['headers'] ?? []);

if ($defaultOptions['headers'] ?? false) {
Expand Down
85 changes: 85 additions & 0 deletions src/Symfony/Component/HttpClient/MockHttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpClient;

use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpClient\Response\ResponseStream;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;

/**
* A test-friendly HttpClient that doesn't make actual HTTP requests.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class MockHttpClient implements HttpClientInterface
{
use HttpClientTrait;

private $responseFactory;
private $baseUri;

/**
* @param callable|ResponseInterface|ResponseInterface[]|iterable $responseFactory
*/
public function __construct($responseFactory, string $baseUri = null)
{
if ($responseFactory instanceof ResponseInterface) {
$responseFactory = [$responseFactory];
}

if (!\is_callable($responseFactory) && !$responseFactory instanceof \Iterator) {
$responseFactory = (function () use ($responseFactory) {
yield from $responseFactory;
})();
}

$this->responseFactory = $responseFactory;
$this->baseUri = $baseUri;
}

/**
* {@inheritdoc}
*/
public function request(string $method, string $url, array $options = []): ResponseInterface
{
[$url, $options] = $this->prepareRequest($method, $url, $options, ['base_uri' => $this->baseUri], true);
$url = implode('', $url);

if (\is_callable($this->responseFactory)) {
$response = ($this->responseFactory)($method, $url, $options);
} elseif (!$this->responseFactory->valid()) {
throw new TransportException('The response factory iterator passed to MockHttpClient is empty.');
} else {
$response = $this->responseFactory->current();
$this->responseFactory->next();
}

return MockResponse::fromRequest($method, $url, $options, $response);
}

/**
* {@inheritdoc}
*/
public function stream($responses, float $timeout = null): ResponseStreamInterface
{
if ($responses instanceof ResponseInterface) {
$responses = [$responses];
} elseif (!\is_iterable($responses)) {
throw new \TypeError(sprintf('%s() expects parameter 1 to be an iterable of MockResponse objects, %s given.', __METHOD__, \is_object($responses) ? \get_class($responses) : \gettype($responses)));
}

return new ResponseStream(MockResponse::stream($responses, $timeout));
}
}
Loading