14
14
use PHPUnit \Framework \TestCase ;
15
15
use Symfony \Component \HttpClient \Exception \ServerException ;
16
16
use Symfony \Component \HttpClient \Exception \TimeoutException ;
17
+ use Symfony \Component \HttpClient \Exception \TransportException ;
17
18
use Symfony \Component \HttpClient \HttpClient ;
18
19
use Symfony \Component \HttpClient \MockHttpClient ;
19
20
use Symfony \Component \HttpClient \NativeHttpClient ;
20
21
use Symfony \Component \HttpClient \Response \AsyncContext ;
21
22
use Symfony \Component \HttpClient \Response \MockResponse ;
22
23
use Symfony \Component \HttpClient \Retry \GenericRetryStrategy ;
24
+ use Symfony \Component \HttpClient \Retry \RetryStrategyInterface ;
23
25
use Symfony \Component \HttpClient \RetryableHttpClient ;
24
26
use Symfony \Contracts \HttpClient \Exception \TransportExceptionInterface ;
25
27
use Symfony \Contracts \HttpClient \Test \TestHttpServer ;
@@ -248,13 +250,9 @@ public function testRetryOnErrorAssertContent()
248
250
}
249
251
250
252
/**
251
- * @testWith ["GET"]
252
- * ["POST"]
253
- * ["PUT"]
254
- * ["PATCH"]
255
- * ["DELETE"]
253
+ * Make sure we use the RetryableHttpClient on timeouts
256
254
*/
257
- public function testRetryOnHeaderTimeout ( string $ method )
255
+ public function testRetryOnTimeout ( )
258
256
{
259
257
$ client = HttpClient::create ();
260
258
@@ -264,15 +262,27 @@ public function testRetryOnHeaderTimeout(string $method)
264
262
265
263
TestHttpServer::start ();
266
264
267
- $ client = new RetryableHttpClient ($ client );
268
- $ response = $ client ->request ($ method , 'http://localhost:8057/timeout-header ' , ['timeout ' => 0.1 ]);
265
+ $ strategy = new class implements RetryStrategyInterface {
266
+ public $ isCalled = false ;
267
+ public function shouldRetry (AsyncContext $ context , ?string $ responseContent , ?TransportExceptionInterface $ exception ): ?bool
268
+ {
269
+ $ this ->isCalled = true ;
270
+ return false ;
271
+ }
272
+
273
+ public function getDelay (AsyncContext $ context , ?string $ responseContent , ?TransportExceptionInterface $ exception ): int
274
+ {
275
+ return 0 ;
276
+ }
277
+ };
278
+ $ client = new RetryableHttpClient ($ client , $ strategy );
279
+ $ response = $ client ->request ('GET ' , 'http://localhost:8057/timeout-header ' , ['timeout ' => 0.1 ]);
269
280
270
281
try {
271
282
$ response ->getStatusCode ();
272
- $ this ->fail (TimeoutException::class.' expected ' );
273
- } catch (TimeoutException $ e ) {
283
+ } catch (TransportException $ e ) {
274
284
}
275
285
276
- $ this ->assertSame ( ' Idle timeout reached for "http://localhost:8057/timeout-header". ' , $ response -> getInfo ( ' error ' ) );
286
+ $ this ->assertTrue ( $ strategy -> isCalled , ' The HTTP retry strategy should be called ' );
277
287
}
278
288
}
0 commit comments