Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ void applyFallbackOverrides(FallbackOverrides overrides) {
}
}

String originalUrl() {
return requestInitializer.url;
}

SerializedFallbackOverrides fallbackOverridesForContinue() {
return fallbackOverrides;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<JsonElement> 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<Boolean> startHandling() {
handlingPromise = new WaitableResult<>();
Expand Down Expand Up @@ -96,15 +92,15 @@ public void abort(String errorCode) {
handleRoute(() -> {
RouteAbortParams params = new RouteAbortParams();
params.errorCode = errorCode;
raceWithTargetClose(sendMessageAsync("abort", params));
sendMessageAsync("abort", params);
});
}

void redirectNavigationRequest(String url) {
handleRoute(() -> {
RouteRedirectNavigationRequestParams params = new RouteRedirectNavigationRequestParams();
params.url = url;
raceWithTargetClose(sendMessageAsync("redirectNavigationRequest", params));
sendMessageAsync("redirectNavigationRequest", params);
});
}

Expand Down Expand Up @@ -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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have the same limitation in other browsers as well?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, only WebKit: the driver throws this in wkInterceptableRequest.ts only, and Chromium and Firefox do fulfill with a redirect status (RO-032 runs there, RO-033 is WebKit-only). The check moved to the client because the reply that carried the error is no longer awaited.

&& 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.
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading