Skip to content

Commit c5d9c95

Browse files
author
Rokas Mikalkėnas
committed
[HttpClient] Implement ResetInterface for all http clients
1 parent 29ac690 commit c5d9c95

11 files changed

+81
-8
lines changed

CachingHttpClient.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Contracts\HttpClient\HttpClientInterface;
2121
use Symfony\Contracts\HttpClient\ResponseInterface;
2222
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
23+
use Symfony\Contracts\Service\ResetInterface;
2324

2425
/**
2526
* Adds caching on top of an HTTP client.
@@ -30,7 +31,7 @@
3031
*
3132
* @author Nicolas Grekas <[email protected]>
3233
*/
33-
class CachingHttpClient implements HttpClientInterface
34+
class CachingHttpClient implements HttpClientInterface, ResetInterface
3435
{
3536
use HttpClientTrait;
3637

@@ -141,4 +142,11 @@ public function stream($responses, float $timeout = null): ResponseStreamInterfa
141142
yield $this->client->stream($clientResponses, $timeout);
142143
})());
143144
}
145+
146+
public function reset()
147+
{
148+
if ($this->client instanceof ResetInterface) {
149+
$this->client->reset();
150+
}
151+
}
144152
}

DecoratorTrait.php

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Contracts\HttpClient\HttpClientInterface;
1515
use Symfony\Contracts\HttpClient\ResponseInterface;
1616
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
17+
use Symfony\Contracts\Service\ResetInterface;
1718

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

5657
return $clone;
5758
}
59+
60+
public function reset()
61+
{
62+
if ($this->client instanceof ResetInterface) {
63+
$this->client->reset();
64+
}
65+
}
5866
}

EventSourceHttpClient.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2020
use Symfony\Contracts\HttpClient\HttpClientInterface;
2121
use Symfony\Contracts\HttpClient\ResponseInterface;
22+
use Symfony\Contracts\Service\ResetInterface;
2223

2324
/**
2425
* @author Antoine Bluchet <[email protected]>
2526
* @author Nicolas Grekas <[email protected]>
2627
*/
27-
final class EventSourceHttpClient implements HttpClientInterface
28+
final class EventSourceHttpClient implements HttpClientInterface, ResetInterface
2829
{
2930
use AsyncDecoratorTrait, HttpClientTrait {
3031
AsyncDecoratorTrait::withOptions insteadof HttpClientTrait;

HttplugClient.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
4040
use Symfony\Contracts\HttpClient\HttpClientInterface;
4141
use Symfony\Contracts\HttpClient\ResponseInterface;
42+
use Symfony\Contracts\Service\ResetInterface;
4243

4344
if (!interface_exists(HttplugInterface::class)) {
4445
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".');
@@ -56,7 +57,7 @@
5657
*
5758
* @author Nicolas Grekas <[email protected]>
5859
*/
59-
final class HttplugClient implements HttplugInterface, HttpAsyncClient, RequestFactory, StreamFactory, UriFactory
60+
final class HttplugClient implements HttplugInterface, HttpAsyncClient, RequestFactory, StreamFactory, UriFactory, ResetInterface
6061
{
6162
private $client;
6263
private $responseFactory;
@@ -238,6 +239,13 @@ public function __destruct()
238239
$this->wait();
239240
}
240241

242+
public function reset()
243+
{
244+
if ($this->client instanceof ResetInterface) {
245+
$this->client->reset();
246+
}
247+
}
248+
241249
private function sendPsr7Request(RequestInterface $request, bool $buffer = null): ResponseInterface
242250
{
243251
try {

Internal/NativeClientState.php

+7
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,11 @@ public function __construct()
3737
{
3838
$this->id = random_int(\PHP_INT_MIN, \PHP_INT_MAX);
3939
}
40+
41+
public function reset()
42+
{
43+
$this->responseCount = 0;
44+
$this->dnsCache = [];
45+
$this->hosts = [];
46+
}
4047
}

MockHttpClient.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
use Symfony\Contracts\HttpClient\HttpClientInterface;
1818
use Symfony\Contracts\HttpClient\ResponseInterface;
1919
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
20+
use Symfony\Contracts\Service\ResetInterface;
2021

2122
/**
2223
* A test-friendly HttpClient that doesn't make actual HTTP requests.
2324
*
2425
* @author Nicolas Grekas <[email protected]>
2526
*/
26-
class MockHttpClient implements HttpClientInterface
27+
class MockHttpClient implements HttpClientInterface, ResetInterface
2728
{
2829
use HttpClientTrait;
2930

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

116117
return $clone;
117118
}
119+
120+
public function reset()
121+
{
122+
$this->requestsCount = 0;
123+
}
118124
}

NativeHttpClient.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Contracts\HttpClient\HttpClientInterface;
2222
use Symfony\Contracts\HttpClient\ResponseInterface;
2323
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
24+
use Symfony\Contracts\Service\ResetInterface;
2425

2526
/**
2627
* A portable implementation of the HttpClientInterface contracts based on PHP stream wrappers.
@@ -30,7 +31,7 @@
3031
*
3132
* @author Nicolas Grekas <[email protected]>
3233
*/
33-
final class NativeHttpClient implements HttpClientInterface, LoggerAwareInterface
34+
final class NativeHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface
3435
{
3536
use HttpClientTrait;
3637
use LoggerAwareTrait;
@@ -261,6 +262,11 @@ public function stream($responses, float $timeout = null): ResponseStreamInterfa
261262
return new ResponseStream(NativeResponse::stream($responses, $timeout));
262263
}
263264

265+
public function reset()
266+
{
267+
$this->multi->reset();
268+
}
269+
264270
private static function getBodyAsString($body): string
265271
{
266272
if (\is_resource($body)) {

NoPrivateNetworkHttpClient.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
use Symfony\Contracts\HttpClient\HttpClientInterface;
2020
use Symfony\Contracts\HttpClient\ResponseInterface;
2121
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
22+
use Symfony\Contracts\Service\ResetInterface;
2223

2324
/**
2425
* Decorator that blocks requests to private networks by default.
2526
*
2627
* @author Hallison Boaventura <[email protected]>
2728
*/
28-
final class NoPrivateNetworkHttpClient implements HttpClientInterface, LoggerAwareInterface
29+
final class NoPrivateNetworkHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface
2930
{
3031
use HttpClientTrait;
3132

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

122123
return $clone;
123124
}
125+
126+
public function reset()
127+
{
128+
if ($this->client instanceof ResetInterface) {
129+
$this->client->reset();
130+
}
131+
}
124132
}

Psr18Client.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use Symfony\Component\HttpClient\Response\StreamWrapper;
3232
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
3333
use Symfony\Contracts\HttpClient\HttpClientInterface;
34+
use Symfony\Contracts\Service\ResetInterface;
3435

3536
if (!interface_exists(RequestFactoryInterface::class)) {
3637
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".');
@@ -49,7 +50,7 @@
4950
*
5051
* @author Nicolas Grekas <[email protected]>
5152
*/
52-
final class Psr18Client implements ClientInterface, RequestFactoryInterface, StreamFactoryInterface, UriFactoryInterface
53+
final class Psr18Client implements ClientInterface, RequestFactoryInterface, StreamFactoryInterface, UriFactoryInterface, ResetInterface
5354
{
5455
private $client;
5556
private $responseFactory;
@@ -190,6 +191,13 @@ public function createUri(string $uri = ''): UriInterface
190191

191192
throw new \LogicException(sprintf('You cannot use "%s()" as the "nyholm/psr7" package is not installed. Try running "composer require nyholm/psr7".', __METHOD__));
192193
}
194+
195+
public function reset()
196+
{
197+
if ($this->client instanceof ResetInterface) {
198+
$this->client->reset();
199+
}
200+
}
193201
}
194202

195203
/**

RetryableHttpClient.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2222
use Symfony\Contracts\HttpClient\HttpClientInterface;
2323
use Symfony\Contracts\HttpClient\ResponseInterface;
24+
use Symfony\Contracts\Service\ResetInterface;
2425

2526
/**
2627
* Automatically retries failing HTTP requests.
2728
*
2829
* @author Jérémy Derussé <[email protected]>
2930
*/
30-
class RetryableHttpClient implements HttpClientInterface
31+
class RetryableHttpClient implements HttpClientInterface, ResetInterface
3132
{
3233
use AsyncDecoratorTrait;
3334

Tests/MockHttpClientTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,16 @@ public function __toString()
418418

419419
$this->assertSame('foo=bar', $response->getRequestOptions()['body']);
420420
}
421+
422+
public function testResetsRequestCount()
423+
{
424+
$client = new MockHttpClient([new MockResponse()]);
425+
$this->assertSame(0, $client->getRequestsCount());
426+
427+
$client->request('POST', '/url', ['body' => 'payload']);
428+
429+
$this->assertSame(1, $client->getRequestsCount());
430+
$client->reset();
431+
$this->assertSame(0, $client->getRequestsCount());
432+
}
421433
}

0 commit comments

Comments
 (0)