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 79ece786..efc0fee0 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/RouteImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/RouteImpl.java @@ -47,6 +47,9 @@ public class RouteImpl extends ChannelOwner implements Route { overrides = new ContinueOverrides(); } JsonObject params = new JsonObject(); + if (overrides.url != null) { + params.addProperty("url", overrides.url); + } if (overrides.method != null) { params.addProperty("method", overrides.method); } diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java b/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java index a55ba70c..84974714 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java @@ -498,6 +498,8 @@ public class TestPageRoute extends TestBase { .withContentType("application/json") .withHeaders(headers) .withBody("[\"electric\",\"gas\"]")); + + }); { // Should succeed diff --git a/playwright/src/test/java/com/microsoft/playwright/TestRequestContinue.java b/playwright/src/test/java/com/microsoft/playwright/TestRequestContinue.java index 00ed43c1..209219eb 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestRequestContinue.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestRequestContinue.java @@ -24,9 +24,9 @@ import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +import static com.microsoft.playwright.Page.EventType.RESPONSE; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; public class TestRequestContinue extends TestBase { @@ -60,6 +60,46 @@ public class TestRequestContinue extends TestBase { assertEquals("POST", sRequest.get().method); } + @Test + void shouldOverrideRequestUrl() throws ExecutionException, InterruptedException { + Future serverRequest = server.waitForRequest("/global-var.html"); + page.route("**/foo", route -> { + route.continue_(new Route.ContinueOverrides().withUrl(server.PREFIX + "/global-var.html")); + }); + Deferred> responseEvent = page.waitForEvent(RESPONSE); + page.navigate(server.PREFIX + "/foo"); + Response response = (Response) responseEvent.get().data(); + assertEquals(server.PREFIX + "/foo", response.url()); + assertEquals(123, page.evaluate("window['globalVar']")); + assertEquals("GET", serverRequest.get().method); + } + + @Test + void shouldNotAllowChangingProtocolWhenOverridingUrl() { + PlaywrightException[] error = {null}; + page.route("**/*", route -> { + try { + route.continue_(new Route.ContinueOverrides().withUrl("file:///tmp/foo")); + } catch (PlaywrightException e) { + error[0] = e; + route.continue_(); + } + }); + page.navigate(server.EMPTY_PAGE); + assertNotNull(error[0]); + assertTrue(error[0].getMessage().contains("New URL must have same protocol as overriden URL")); + } + + @Test + void shouldOverrideMethodAlongWithUrl() throws ExecutionException, InterruptedException { + Future serverRequest = server.waitForRequest("/empty.html"); + page.route("**/foo", route -> { + route.continue_(new Route.ContinueOverrides().withUrl(server.EMPTY_PAGE).withMethod("POST")); + }); + page.navigate(server.PREFIX + "/foo"); + assertEquals("POST", serverRequest.get().method); + } + @Test void shouldAmendMethodOnMainRequest() throws ExecutionException, InterruptedException { Future request = server.waitForRequest("/empty.html");