diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/RequestImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/RequestImpl.java index 9e8e4532b..724495812 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/RequestImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/RequestImpl.java @@ -306,6 +306,10 @@ void applyFallbackOverrides(FallbackOverrides overrides) { } } + String originalUrl() { + return requestInitializer.url; + } + SerializedFallbackOverrides fallbackOverridesForContinue() { return fallbackOverrides; } 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 1558ef5bc..480503aa2 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/RouteImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/RouteImpl.java @@ -30,6 +30,8 @@ import com.microsoft.playwright.impl.protocol.RouteRedirectNavigationRequestParams; import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -60,13 +62,7 @@ public RequestImpl request() { return routeInitializer.request; } - // java: takes the pending reply of a message sent with sendMessageAsync. - private void raceWithTargetClose(WaitableResult promise) { - // When page closes or crashes, we catch any potential rejects from this Route. - // Note that page could be missing when routing popup's initial request that - // does not have a Page initialized just yet. - request().targetClosedScope().safeRace(promise); - } + // java: _raceWithTargetClose is not ported, route actions do not wait for the reply. WaitableResult startHandling() { handlingPromise = new WaitableResult<>(); @@ -96,7 +92,7 @@ public void abort(String errorCode) { handleRoute(() -> { RouteAbortParams params = new RouteAbortParams(); params.errorCode = errorCode; - raceWithTargetClose(sendMessageAsync("abort", params)); + sendMessageAsync("abort", params); }); } @@ -104,7 +100,7 @@ void redirectNavigationRequest(String url) { handleRoute(() -> { RouteRedirectNavigationRequestParams params = new RouteRedirectNavigationRequestParams(); params.url = url; - raceWithTargetClose(sendMessageAsync("redirectNavigationRequest", params)); + sendMessageAsync("redirectNavigationRequest", params); }); } @@ -225,7 +221,12 @@ private void innerFulfill(FulfillOptions options, JsonElement json) { params.body = (String) body; params.isBase64 = isBase64; params.fetchResponseUid = fetchResponseUid; - raceWithTargetClose(sendMessageAsync("fulfill", params)); + // java: the check of wkInterceptableRequest.ts. + if (context != null && context.browser() != null && "webkit".equals(context.browser().browserName) + && 300 <= params.status && params.status < 400) { + throw new PlaywrightException("Cannot fulfill with redirect status: " + params.status); + } + sendMessageAsync("fulfill", params); } // java: resume() is continue() of the Node.js API. @@ -258,7 +259,13 @@ void innerContinue(boolean isFallback) { params.headers = options.headers != null ? headersObjectToArray(options.headers) : null; params.postData = options.postDataBuffer; params.isFallback = isFallback; - raceWithTargetClose(sendMessageAsync("continue", params)); + // java: the check of server/network.ts. + if (options.url != null && !options.url.isEmpty()) { + if (!protocol(request().originalUrl()).equals(protocol(options.url))) { + throw new PlaywrightException("New URL must have same protocol as overridden URL"); + } + } + sendMessageAsync("continue", params); } // java: members with no counterpart upstream; handlerReturned is set by RouteHandler when the handler @@ -274,6 +281,17 @@ private static RequestImpl.FallbackOverrides fallbackOverrides(String url, Strin return overrides; } + private static String protocol(String url) { + try { + String scheme = new URI(url).getScheme(); + if (scheme != null) { + return scheme.toLowerCase(); + } + } catch (URISyntaxException e) { + } + throw new PlaywrightException("Invalid URL"); + } + private static byte[] readFile(Path path) { try { return Files.readAllBytes(path); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java b/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java index 30484ffd0..e7d88dca6 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageRoute.java @@ -424,6 +424,39 @@ void shouldWorkWithRedirectsForSubresources(Page page, Server server, BrowserNam assertNull(r); } + @PlaywrightTest + void shouldResumeABurstOfRequests(Page page, Server server) { + // https://github.com/microsoft/playwright-java/issues/2068 + page.navigate(server.EMPTY_PAGE); + int[] handled = {0}; + page.route("**/*", route -> { + ++handled[0]; + route.resume(); + }); + assertEquals(3000, page.evaluate("() => Promise.allSettled(" + + "Array.from({length: 3000}, (_, i) => fetch('/empty.html?i=' + i)))" + + ".then(results => results.length)")); + assertEquals(3000, handled[0]); + } + + @PlaywrightTest + void shouldAbortAndFulfillABurstOfRequests(Page page, Server server) { + // https://github.com/microsoft/playwright-java/issues/2068 + page.navigate(server.EMPTY_PAGE); + int[] handled = {0}; + page.route("**/*", route -> { + if (++handled[0] % 2 == 0) { + route.abort(); + } else { + route.fulfill(new Route.FulfillOptions().setBody("ok")); + } + }); + assertEquals(1500, page.evaluate("() => Promise.allSettled(" + + "Array.from({length: 3000}, (_, i) => fetch('/empty.html?i=' + i)))" + + ".then(results => results.filter(r => r.status === 'fulfilled').length)")); + assertEquals(3000, handled[0]); + } + @PlaywrightTest void shouldWorkWithEqualRequests(Page page, Server server) { page.navigate(server.EMPTY_PAGE);