diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/RouteImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/RouteImpl.java index 4b9fd02b..c1e392c7 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/RouteImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/RouteImpl.java @@ -29,12 +29,15 @@ import java.util.LinkedHashMap; import java.util.Map; public class RouteImpl extends ChannelOwner implements Route { + private boolean handled; + public RouteImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) { super(parent, type, guid, initializer); } @Override public void abort(String errorCode) { + startHandling(); withLogging("Route.abort", () -> { JsonObject params = new JsonObject(); params.addProperty("errorCode", errorCode); @@ -44,6 +47,7 @@ public class RouteImpl extends ChannelOwner implements Route { @Override public void resume(ResumeOptions options) { + startHandling(); withLogging("Route.resume", () -> resumeImpl(options)); } @@ -78,6 +82,7 @@ public class RouteImpl extends ChannelOwner implements Route { @Override public void fulfill(FulfillOptions options) { + startHandling(); withLogging("Route.fulfill", () -> fulfillImpl(options)); } @@ -135,4 +140,11 @@ public class RouteImpl extends ChannelOwner implements Route { public Request request() { return connection.getExistingObject(initializer.getAsJsonObject("request").get("guid").getAsString()); } + + private void startHandling() { + if (handled) { + throw new PlaywrightException("Route is already handled!"); + } + handled = true; + } } diff --git a/playwright/src/test/java/com/microsoft/playwright/TestRequestFulfill.java b/playwright/src/test/java/com/microsoft/playwright/TestRequestFulfill.java index 76e3bb47..e137f869 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestRequestFulfill.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestRequestFulfill.java @@ -24,7 +24,7 @@ import java.nio.file.Files; import java.nio.file.Paths; import static com.microsoft.playwright.Utils.mapOf; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; public class TestRequestFulfill extends TestBase { @Test @@ -104,4 +104,45 @@ public class TestRequestFulfill extends TestBase { } + @Test + void fulfillShouldThrowIfHandledTwice() { + try { + page.route("**/*", route -> { + route.fulfill(); + route.fulfill(); + }); + page.navigate(server.EMPTY_PAGE); + fail("didn't throw"); + } catch (PlaywrightException e) { + assertTrue(e.getMessage().contains("Route is already handled!"), e.getMessage()); + } + } + + @Test + void abortShouldThrowIfHandledTwice() { + try { + page.route("**/*", route -> { + route.abort(); + route.abort(); + }); + page.navigate(server.EMPTY_PAGE); + fail("didn't throw"); + } catch (PlaywrightException e) { + assertTrue(e.getMessage().contains("Route is already handled!"), e.getMessage()); + } + } + + @Test + void resumeShouldThrowIfHandledTwice() { + try { + page.route("**/*", route -> { + route.resume(); + route.resume(); + }); + page.navigate(server.EMPTY_PAGE); + fail("didn't throw"); + } catch (PlaywrightException e) { + assertTrue(e.getMessage().contains("Route is already handled!"), e.getMessage()); + } + } }