3838import static org .junit .jupiter .api .Assertions .*;
3939
4040public class TestGlobalFetch extends TestBase {
41+ private static final List <String > HTTP_METHODS = asList ("GET" , "PUT" , "POST" , "OPTIONS" , "HEAD" , "PATCH" );
42+
4143 @ Test
4244 void shouldHaveJavaInDefaultUesrAgent () throws ExecutionException , InterruptedException {
4345 APIRequestContext request = playwright .request ().newContext (new APIRequest .NewContextOptions ());
@@ -359,7 +361,7 @@ void shouldThrowAnErrorWhenMaxRedirectsIsExceeded() {
359361 server .setRedirect ("/b/c/redirect4" , "/simple.json" );
360362
361363 APIRequestContext request = playwright .request ().newContext ();
362- for (String method : new String [] { "GET" , "PUT" , "POST" , "OPTIONS" , "HEAD" , "PATCH" } ) {
364+ for (String method : HTTP_METHODS ) {
363365 for (int maxRedirects = 1 ; maxRedirects < 4 ; maxRedirects ++) {
364366 int currMaxRedirects = maxRedirects ;
365367 PlaywrightException exception = assertThrows (PlaywrightException .class ,
@@ -372,32 +374,68 @@ void shouldThrowAnErrorWhenMaxRedirectsIsExceeded() {
372374 }
373375
374376 @ Test
375- void shouldThrowAnErrorWhenMaxRedirectsOnContextIsExceeded () {
377+ void shouldUseMaxRedirectsFromFetchWhenProvidedOverridingNewContext () {
376378 server .setRedirect ("/a/redirect1" , "/b/c/redirect2" );
377379 server .setRedirect ("/b/c/redirect2" , "/b/c/redirect3" );
378380 server .setRedirect ("/b/c/redirect3" , "/b/c/redirect4" );
379381 server .setRedirect ("/b/c/redirect4" , "/simple.json" );
380382
381- for (String method : new String [] {"GET" , "PUT" , "POST" , "OPTIONS" , "HEAD" , "PATCH" }) {
382- for (int maxRedirects = 1 ; maxRedirects < 4 ; maxRedirects ++) {
383+ APIRequestContext request = playwright .request ().newContext (new NewContextOptions ().setMaxRedirects (1 ));
384+ for (String method : HTTP_METHODS ) {
385+ APIResponse response = request .fetch (server .PREFIX + "/a/redirect1" ,
386+ RequestOptions .create ().setMethod (method ).setMaxRedirects (4 ));
387+ assertEquals (200 , response .status ());
388+ }
389+ request .dispose ();
390+ }
391+
392+ @ Test
393+ void shouldFollowRedirectsUpToMaxRedirectsLimitSetInNewContext () {
394+ server .setRedirect ("/a/redirect1" , "/b/c/redirect2" );
395+ server .setRedirect ("/b/c/redirect2" , "/b/c/redirect3" );
396+ server .setRedirect ("/b/c/redirect3" , "/b/c/redirect4" );
397+ server .setRedirect ("/b/c/redirect4" , "/simple.json" );
398+
399+ for (String method : HTTP_METHODS ) {
400+ for (int maxRedirects = 1 ; maxRedirects <= 4 ; maxRedirects ++) {
383401 int currMaxRedirects = maxRedirects ;
384402 APIRequestContext request = playwright .request ().newContext (new NewContextOptions ().setMaxRedirects (currMaxRedirects ));
385- PlaywrightException exception = assertThrows (PlaywrightException .class ,
386- () -> request .fetch (server .PREFIX + "/a/redirect1" ,
387- RequestOptions .create ().setMethod (method )));
388- assertTrue (exception .getMessage ().contains ("Max redirect count exceeded" ), exception .getMessage ());
403+ if (maxRedirects < 4 ) {
404+ PlaywrightException exception = assertThrows (PlaywrightException .class ,
405+ () -> request .fetch (server .PREFIX + "/a/redirect1" ,
406+ RequestOptions .create ().setMethod (method )));
407+ assertTrue (exception .getMessage ().contains ("Max redirect count exceeded" ), exception .getMessage ());
408+ } else {
409+ APIResponse response = request .fetch (server .PREFIX + "/a/redirect1" , RequestOptions .create ().setMethod (method ));
410+ assertEquals (200 , response .status ());
411+ }
389412 request .dispose ();
390413 }
391414 }
392415 }
393416
417+ @ Test
418+ void shouldNotFollowRedirectsWhenMaxRedirectsIsSetTo0InNewContext () {
419+ server .setRedirect ("/a/redirect1" , "/b/c/redirect2" );
420+ server .setRedirect ("/b/c/redirect2" , "/simple.json" );
421+
422+ APIRequestContext request = playwright .request ().newContext (new NewContextOptions ().setMaxRedirects (0 ));
423+ for (String method : HTTP_METHODS ) {
424+ APIResponse response = request .fetch (server .PREFIX + "/a/redirect1" ,
425+ RequestOptions .create ().setMethod (method ));
426+ assertEquals ("/b/c/redirect2" , response .headers ().get ("location" ));
427+ assertEquals (302 , response .status ());
428+ }
429+ request .dispose ();
430+ }
431+
394432 @ Test
395433 void shouldNotFollowRedirectsWhenMaxRedirectsIsSetTo0 () {
396434 server .setRedirect ("/a/redirect1" , "/b/c/redirect2" );
397435 server .setRedirect ("/b/c/redirect2" , "/simple.json" );
398436
399437 APIRequestContext request = playwright .request ().newContext ();
400- for (String method : new String [] { "GET" , "PUT" , "POST" , "OPTIONS" , "HEAD" , "PATCH" } ) {
438+ for (String method : HTTP_METHODS ) {
401439 APIResponse response = request .fetch (server .PREFIX + "/a/redirect1" ,
402440 RequestOptions .create ().setMethod (method ).setMaxRedirects (0 ));
403441 assertEquals ("/b/c/redirect2" , response .headers ().get ("location" ));
@@ -412,7 +450,7 @@ void shouldThrowAnErrorWhenMaxRedirectsIsLessThan0() {
412450 server .setRedirect ("/b/c/redirect2" , "/simple.json" );
413451
414452 APIRequestContext request = playwright .request ().newContext ();
415- for (String method : new String [] { "GET" , "PUT" , "POST" , "OPTIONS" , "HEAD" , "PATCH" } ) {
453+ for (String method : HTTP_METHODS ) {
416454 PlaywrightException exception = assertThrows (PlaywrightException .class ,
417455 () -> request .fetch (server .PREFIX + "/a/redirect1" ,
418456 RequestOptions .create ().setMethod (method ).setMaxRedirects (-1 )));
0 commit comments