mirror of
https://github.com/microsoft/playwright-java.git
synced 2025-09-08 21:01:00 +00:00
fix: support request.url overrides (#92)
This commit is contained in:
parent
2c7caa6292
commit
5e3fc5b78e
@ -47,6 +47,9 @@ public class RouteImpl extends ChannelOwner implements Route {
|
|||||||
overrides = new ContinueOverrides();
|
overrides = new ContinueOverrides();
|
||||||
}
|
}
|
||||||
JsonObject params = new JsonObject();
|
JsonObject params = new JsonObject();
|
||||||
|
if (overrides.url != null) {
|
||||||
|
params.addProperty("url", overrides.url);
|
||||||
|
}
|
||||||
if (overrides.method != null) {
|
if (overrides.method != null) {
|
||||||
params.addProperty("method", overrides.method);
|
params.addProperty("method", overrides.method);
|
||||||
}
|
}
|
||||||
|
@ -498,6 +498,8 @@ public class TestPageRoute extends TestBase {
|
|||||||
.withContentType("application/json")
|
.withContentType("application/json")
|
||||||
.withHeaders(headers)
|
.withHeaders(headers)
|
||||||
.withBody("[\"electric\",\"gas\"]"));
|
.withBody("[\"electric\",\"gas\"]"));
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
{
|
{
|
||||||
// Should succeed
|
// Should succeed
|
||||||
|
@ -24,9 +24,9 @@ import java.util.Map;
|
|||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
|
import static com.microsoft.playwright.Page.EventType.RESPONSE;
|
||||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
|
|
||||||
public class TestRequestContinue extends TestBase {
|
public class TestRequestContinue extends TestBase {
|
||||||
|
|
||||||
@ -60,6 +60,46 @@ public class TestRequestContinue extends TestBase {
|
|||||||
assertEquals("POST", sRequest.get().method);
|
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
|
@Test
|
||||||
void shouldAmendMethodOnMainRequest() throws ExecutionException, InterruptedException {
|
void shouldAmendMethodOnMainRequest() throws ExecutionException, InterruptedException {
|
||||||
Future<Server.Request> request = server.waitForRequest("/empty.html");
|
Future<Server.Request> request = server.waitForRequest("/empty.html");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user