Skip to content

[HttpClient] Implement ResetInterface for all http clients #43985

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
Nov 10, 2021
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
10 changes: 9 additions & 1 deletion src/Symfony/Component/HttpClient/CachingHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
use Symfony\Contracts\Service\ResetInterface;

/**
* Adds caching on top of an HTTP client.
Expand All @@ -30,7 +31,7 @@
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class CachingHttpClient implements HttpClientInterface
class CachingHttpClient implements HttpClientInterface, ResetInterface
{
use HttpClientTrait;

Expand Down Expand Up @@ -141,4 +142,11 @@ public function stream($responses, float $timeout = null): ResponseStreamInterfa
yield $this->client->stream($clientResponses, $timeout);
})());
}

public function reset()
{
if ($this->client instanceof ResetInterface) {
$this->client->reset();
}
}
}
8 changes: 8 additions & 0 deletions src/Symfony/Component/HttpClient/DecoratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
use Symfony\Contracts\Service\ResetInterface;

/**
* Eases with writing decorators.
Expand Down Expand Up @@ -55,4 +56,11 @@ public function withOptions(array $options): self

return $clone;
}

public function reset()
{
if ($this->client instanceof ResetInterface) {
$this->client->reset();
}
}
}
3 changes: 2 additions & 1 deletion src/Symfony/Component/HttpClient/EventSourceHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\Service\ResetInterface;

/**
* @author Antoine Bluchet <soyuka@gmail.com>
* @author Nicolas Grekas <p@tchwork.com>
*/
final class EventSourceHttpClient implements HttpClientInterface
final class EventSourceHttpClient implements HttpClientInterface, ResetInterface
{
use AsyncDecoratorTrait, HttpClientTrait {
AsyncDecoratorTrait::withOptions insteadof HttpClientTrait;
Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Component/HttpClient/HttplugClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\Service\ResetInterface;

if (!interface_exists(HttplugInterface::class)) {
throw new \LogicException('You cannot use "Symfony\Component\HttpClient\HttplugClient" as the "php-http/httplug" package is not installed. Try running "composer require php-http/httplug".');
Expand All @@ -56,7 +57,7 @@
*
* @author Nicolas Grekas <p@tchwork.com>
*/
final class HttplugClient implements HttplugInterface, HttpAsyncClient, RequestFactory, StreamFactory, UriFactory
final class HttplugClient implements HttplugInterface, HttpAsyncClient, RequestFactory, StreamFactory, UriFactory, ResetInterface
{
private $client;
private $responseFactory;
Expand Down Expand Up @@ -238,6 +239,13 @@ public function __destruct()
$this->wait();
}

public function reset()
{
if ($this->client instanceof ResetInterface) {
$this->client->reset();
}
}

private function sendPsr7Request(RequestInterface $request, bool $buffer = null): ResponseInterface
{
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@ public function __construct()
{
$this->id = random_int(\PHP_INT_MIN, \PHP_INT_MAX);
}

public function reset()
{
$this->responseCount = 0;
$this->dnsCache = [];
$this->hosts = [];
}
}
8 changes: 7 additions & 1 deletion src/Symfony/Component/HttpClient/MockHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
use Symfony\Contracts\Service\ResetInterface;

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

Expand Down Expand Up @@ -115,4 +116,9 @@ public function withOptions(array $options): self

return $clone;
}

public function reset()
{
$this->requestsCount = 0;
}
}
8 changes: 7 additions & 1 deletion src/Symfony/Component/HttpClient/NativeHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
use Symfony\Contracts\Service\ResetInterface;

/**
* A portable implementation of the HttpClientInterface contracts based on PHP stream wrappers.
Expand All @@ -30,7 +31,7 @@
*
* @author Nicolas Grekas <p@tchwork.com>
*/
final class NativeHttpClient implements HttpClientInterface, LoggerAwareInterface
final class NativeHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface
{
use HttpClientTrait;
use LoggerAwareTrait;
Expand Down Expand Up @@ -261,6 +262,11 @@ public function stream($responses, float $timeout = null): ResponseStreamInterfa
return new ResponseStream(NativeResponse::stream($responses, $timeout));
}

public function reset()
{
$this->multi->reset();
}

private static function getBodyAsString($body): string
{
if (\is_resource($body)) {
Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Component/HttpClient/NoPrivateNetworkHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
use Symfony\Contracts\Service\ResetInterface;

/**
* Decorator that blocks requests to private networks by default.
*
* @author Hallison Boaventura <hallisonboaventura@gmail.com>
*/
final class NoPrivateNetworkHttpClient implements HttpClientInterface, LoggerAwareInterface
final class NoPrivateNetworkHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface
{
use HttpClientTrait;

Expand Down Expand Up @@ -121,4 +122,11 @@ public function withOptions(array $options): self

return $clone;
}

public function reset()
{
if ($this->client instanceof ResetInterface) {
$this->client->reset();
}
}
}
10 changes: 9 additions & 1 deletion src/Symfony/Component/HttpClient/Psr18Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Symfony\Component\HttpClient\Response\StreamWrapper;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\Service\ResetInterface;

if (!interface_exists(RequestFactoryInterface::class)) {
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\Psr18Client" as the "psr/http-factory" package is not installed. Try running "composer require nyholm/psr7".');
Expand All @@ -49,7 +50,7 @@
*
* @author Nicolas Grekas <p@tchwork.com>
*/
final class Psr18Client implements ClientInterface, RequestFactoryInterface, StreamFactoryInterface, UriFactoryInterface
final class Psr18Client implements ClientInterface, RequestFactoryInterface, StreamFactoryInterface, UriFactoryInterface, ResetInterface
{
private $client;
private $responseFactory;
Expand Down Expand Up @@ -190,6 +191,13 @@ public function createUri(string $uri = ''): UriInterface

throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__));
}

public function reset()
{
if ($this->client instanceof ResetInterface) {
$this->client->reset();
}
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/HttpClient/RetryableHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\Service\ResetInterface;

/**
* Automatically retries failing HTTP requests.
*
* @author Jérémy Derussé <jeremy@derusse.com>
*/
class RetryableHttpClient implements HttpClientInterface
class RetryableHttpClient implements HttpClientInterface, ResetInterface
{
use AsyncDecoratorTrait;

Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,16 @@ public function __toString()

$this->assertSame('foo=bar', $response->getRequestOptions()['body']);
}

public function testResetsRequestCount()
{
$client = new MockHttpClient([new MockResponse()]);
$this->assertSame(0, $client->getRequestsCount());

$client->request('POST', '/url', ['body' => 'payload']);

$this->assertSame(1, $client->getRequestsCount());
$client->reset();
$this->assertSame(0, $client->getRequestsCount());
}
}