forked from symfony/symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RetryableHttpClientTest.php
288 lines (240 loc) · 9.64 KB
/
RetryableHttpClientTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?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\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\Exception\ServerException;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\NativeHttpClient;
use Symfony\Component\HttpClient\Response\AsyncContext;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpClient\Retry\GenericRetryStrategy;
use Symfony\Component\HttpClient\Retry\RetryStrategyInterface;
use Symfony\Component\HttpClient\RetryableHttpClient;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\Test\TestHttpServer;
class RetryableHttpClientTest extends TestCase
{
public static function tearDownAfterClass(): void
{
TestHttpServer::stop();
}
public function testRetryOnError()
{
$client = new RetryableHttpClient(
new MockHttpClient([
new MockResponse('', ['http_code' => 500]),
new MockResponse('', ['http_code' => 200]),
]),
new GenericRetryStrategy([500], 0),
1
);
$response = $client->request('GET', 'http://example.com/foo-bar');
self::assertSame(200, $response->getStatusCode());
}
public function testRetryRespectStrategy()
{
$client = new RetryableHttpClient(
new MockHttpClient([
new MockResponse('', ['http_code' => 500]),
new MockResponse('', ['http_code' => 500]),
new MockResponse('', ['http_code' => 200]),
]),
new GenericRetryStrategy([500], 0),
1
);
$response = $client->request('GET', 'http://example.com/foo-bar');
$this->expectException(ServerException::class);
$response->getHeaders();
}
public function testRetryWithBody()
{
$client = new RetryableHttpClient(
new MockHttpClient([
new MockResponse('abc', ['http_code' => 500]),
new MockResponse('def', ['http_code' => 200]),
]),
new class(GenericRetryStrategy::DEFAULT_RETRY_STATUS_CODES, 0) extends GenericRetryStrategy {
public function shouldRetry(AsyncContext $context, ?string $responseContent, ?TransportExceptionInterface $exception): ?bool
{
return 500 === $context->getStatusCode() && null === $responseContent ? null : 200 !== $context->getStatusCode();
}
},
2
);
$response = $client->request('GET', 'http://example.com/foo-bar');
self::assertSame(200, $response->getStatusCode());
self::assertSame('def', $response->getContent());
}
public function testRetryWithBodyKeepContent()
{
$client = new RetryableHttpClient(
new MockHttpClient([
new MockResponse('my bad', ['http_code' => 400]),
]),
new class([400], 0) extends GenericRetryStrategy {
public function shouldRetry(AsyncContext $context, ?string $responseContent, ?TransportExceptionInterface $exception): ?bool
{
if (null === $responseContent) {
return null;
}
return 'my bad' !== $responseContent;
}
},
1
);
$response = $client->request('GET', 'http://example.com/foo-bar');
self::assertSame(400, $response->getStatusCode());
self::assertSame('my bad', $response->getContent(false));
}
public function testRetryWithBodyInvalid()
{
$client = new RetryableHttpClient(
new MockHttpClient([
new MockResponse('', ['http_code' => 500]),
new MockResponse('', ['http_code' => 200]),
]),
new class(GenericRetryStrategy::DEFAULT_RETRY_STATUS_CODES, 0) extends GenericRetryStrategy {
public function shouldRetry(AsyncContext $context, ?string $responseContent, ?TransportExceptionInterface $exception): ?bool
{
return null;
}
},
1
);
$response = $client->request('GET', 'http://example.com/foo-bar');
$this->expectExceptionMessageMatches('/must not return null when called with a body/');
$response->getHeaders();
}
public function testStreamNoRetry()
{
$client = new RetryableHttpClient(
new MockHttpClient([
new MockResponse('', ['http_code' => 500]),
]),
new GenericRetryStrategy([500], 0),
0
);
$response = $client->request('GET', 'http://example.com/foo-bar');
foreach ($client->stream($response) as $chunk) {
if ($chunk->isFirst()) {
self::assertSame(500, $response->getStatusCode());
}
}
}
public function testRetryWithDnsIssue()
{
$client = new RetryableHttpClient(
new NativeHttpClient(),
new class(GenericRetryStrategy::DEFAULT_RETRY_STATUS_CODES, 0) extends GenericRetryStrategy {
public function shouldRetry(AsyncContext $context, ?string $responseContent, ?TransportExceptionInterface $exception): ?bool
{
$this->fail('should not be called');
}
},
2,
$logger = new TestLogger()
);
$response = $client->request('GET', 'http://does.not.exists/foo-bar');
try {
$response->getHeaders();
} catch (TransportExceptionInterface $e) {
$this->assertSame('Could not resolve host "does.not.exists".', $e->getMessage());
}
$this->assertCount(2, $logger->logs);
$this->assertSame('Try #{count} after {delay}ms: Could not resolve host "does.not.exists".', $logger->logs[0]);
}
public function testCancelOnTimeout()
{
$client = HttpClient::create();
if ($client instanceof NativeHttpClient) {
$this->markTestSkipped('NativeHttpClient cannot timeout before receiving headers');
}
$client = new RetryableHttpClient($client);
$response = $client->request('GET', 'https://example.com/');
foreach ($client->stream($response, 0) as $chunk) {
$this->assertTrue($chunk->isTimeout());
$response->cancel();
}
}
public function testRetryWithDelay()
{
$retryAfter = '0.46';
$client = new RetryableHttpClient(
new MockHttpClient([
new MockResponse('', [
'http_code' => 503,
'response_headers' => [
'retry-after' => $retryAfter,
],
]),
new MockResponse('', [
'http_code' => 200,
]),
]),
new GenericRetryStrategy(),
1,
$logger = new class() extends TestLogger {
public $context = [];
public function log($level, $message, array $context = []): void
{
$this->context = $context;
parent::log($level, $message, $context);
}
}
);
$client->request('GET', 'http://example.com/foo-bar')->getContent();
$delay = $logger->context['delay'] ?? null;
$this->assertArrayHasKey('delay', $logger->context);
$this->assertNotNull($delay);
$this->assertSame((int) ($retryAfter * 1000), $delay);
}
public function testRetryOnErrorAssertContent()
{
$client = new RetryableHttpClient(
new MockHttpClient([
new MockResponse('', ['http_code' => 500]),
new MockResponse('Test out content', ['http_code' => 200]),
]),
new GenericRetryStrategy([500], 0),
1
);
$response = $client->request('GET', 'http://example.com/foo-bar');
self::assertSame(200, $response->getStatusCode());
self::assertSame('Test out content', $response->getContent());
self::assertSame('Test out content', $response->getContent(), 'Content should be buffered');
}
public function testRetryOnTimeout()
{
$client = HttpClient::create();
TestHttpServer::start();
$strategy = new class() implements RetryStrategyInterface {
public $isCalled = false;
public function shouldRetry(AsyncContext $context, ?string $responseContent, ?TransportExceptionInterface $exception): ?bool
{
$this->isCalled = true;
return false;
}
public function getDelay(AsyncContext $context, ?string $responseContent, ?TransportExceptionInterface $exception): int
{
return 0;
}
};
$client = new RetryableHttpClient($client, $strategy);
$response = $client->request('GET', 'http://localhost:8057/timeout-header', ['timeout' => 0.1]);
try {
$response->getStatusCode();
$this->fail(TransportException::class.' expected');
} catch (TransportException $e) {
}
$this->assertTrue($strategy->isCalled, 'The HTTP retry strategy should be called');
}
}