diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/Router.java b/playwright/src/main/java/com/microsoft/playwright/impl/Router.java index 58240138..1e08e156 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/Router.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/Router.java @@ -38,19 +38,16 @@ class Router { this.times = times; } - boolean handle(RouteImpl route) { - if (!matcher.test(route.request().url())) { - return false; - } - if (times != null) { - --times; - } + void handle(RouteImpl route) { handler.accept(route); - return true; } - boolean isDone() { - return times != null && times <= 0; + boolean decrementRemainingCallCount() { + if (times == null) { + return false; + } + --times; + return times <= 0; } } @@ -73,14 +70,16 @@ class Router { HandleResult result = HandleResult.NoMatchingHandler; for (Iterator it = routes.iterator(); it.hasNext();) { RouteInfo info = it.next(); - if (info.handle(route)) { - result = HandleResult.FoundMatchingHandler; - if (info.isDone()) { - it.remove(); - } - if (route.isHandled()) { - break; - } + if (!info.matcher.test(route.request().url())) { + continue; + } + if (info.decrementRemainingCallCount()) { + it.remove(); + } + result = HandleResult.FoundMatchingHandler; + info.handle(route); + if (route.isHandled()) { + break; } } return result; diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageRequestFallback.java b/playwright/src/test/java/com/microsoft/playwright/TestPageRequestFallback.java index c03b54ec..e3d280a2 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageRequestFallback.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageRequestFallback.java @@ -19,6 +19,7 @@ package com.microsoft.playwright; import org.junit.jupiter.api.Test; import java.io.OutputStreamWriter; +import java.io.Writer; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -127,13 +128,16 @@ public class TestPageRequestFallback extends TestBase { @Test void shouldChainOnce() { - page.route("**/empty.html", route -> { + boolean didFulfill[] = {false}; + page.route("**/title.html", route -> { route.fulfill(new Route.FulfillOptions().setStatus(200).setBody("fulfilled one")); + didFulfill[0] = true; }, new Page.RouteOptions().setTimes(1)); - page.route("**/empty.html", route -> { + page.route("**/title.html", route -> { route.fallback(); }, new Page.RouteOptions().setTimes(1)); - Response response = page.navigate(server.EMPTY_PAGE); + Response response = page.navigate(server.PREFIX + "/title.html"); + assertTrue(didFulfill[0]); assertEquals("fulfilled one", response.text()); }