fix: support request.url overrides (#92)

This commit is contained in:
Yury Semikhatsky 2020-12-07 14:42:55 -08:00 committed by GitHub
parent 2c7caa6292
commit 5e3fc5b78e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 2 deletions

View File

@ -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);
}

View File

@ -498,6 +498,8 @@ public class TestPageRoute extends TestBase {
.withContentType("application/json")
.withHeaders(headers)
.withBody("[\"electric\",\"gas\"]"));
});
{
// Should succeed

View File

@ -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<Server.Request> serverRequest = server.waitForRequest("/global-var.html");
page.route("**/foo", route -> {
route.continue_(new Route.ContinueOverrides().withUrl(server.PREFIX + "/global-var.html"));
});
Deferred<Event<Page.EventType>> 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<Server.Request> 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<Server.Request> request = server.waitForRequest("/empty.html");