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