1
0
mirror of synced 2026-08-05 15:06:54 +00:00

fix: throw if route is handled twice (#796)

This commit is contained in:
Yury Semikhatsky
2022-01-27 14:02:11 -08:00
committed by GitHub
parent c03f4a9384
commit 17a4143a83
2 changed files with 54 additions and 1 deletions
@@ -29,12 +29,15 @@ import java.util.LinkedHashMap;
import java.util.Map;
public class RouteImpl extends ChannelOwner implements Route {
private boolean handled;
public RouteImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
}
@Override
public void abort(String errorCode) {
startHandling();
withLogging("Route.abort", () -> {
JsonObject params = new JsonObject();
params.addProperty("errorCode", errorCode);
@@ -44,6 +47,7 @@ public class RouteImpl extends ChannelOwner implements Route {
@Override
public void resume(ResumeOptions options) {
startHandling();
withLogging("Route.resume", () -> resumeImpl(options));
}
@@ -78,6 +82,7 @@ public class RouteImpl extends ChannelOwner implements Route {
@Override
public void fulfill(FulfillOptions options) {
startHandling();
withLogging("Route.fulfill", () -> fulfillImpl(options));
}
@@ -135,4 +140,11 @@ public class RouteImpl extends ChannelOwner implements Route {
public Request request() {
return connection.getExistingObject(initializer.getAsJsonObject("request").get("guid").getAsString());
}
private void startHandling() {
if (handled) {
throw new PlaywrightException("Route is already handled!");
}
handled = true;
}
}
@@ -24,7 +24,7 @@ import java.nio.file.Files;
import java.nio.file.Paths;
import static com.microsoft.playwright.Utils.mapOf;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
public class TestRequestFulfill extends TestBase {
@Test
@@ -104,4 +104,45 @@ public class TestRequestFulfill extends TestBase {
}
@Test
void fulfillShouldThrowIfHandledTwice() {
try {
page.route("**/*", route -> {
route.fulfill();
route.fulfill();
});
page.navigate(server.EMPTY_PAGE);
fail("didn't throw");
} catch (PlaywrightException e) {
assertTrue(e.getMessage().contains("Route is already handled!"), e.getMessage());
}
}
@Test
void abortShouldThrowIfHandledTwice() {
try {
page.route("**/*", route -> {
route.abort();
route.abort();
});
page.navigate(server.EMPTY_PAGE);
fail("didn't throw");
} catch (PlaywrightException e) {
assertTrue(e.getMessage().contains("Route is already handled!"), e.getMessage());
}
}
@Test
void resumeShouldThrowIfHandledTwice() {
try {
page.route("**/*", route -> {
route.resume();
route.resume();
});
page.navigate(server.EMPTY_PAGE);
fail("didn't throw");
} catch (PlaywrightException e) {
assertTrue(e.getMessage().contains("Route is already handled!"), e.getMessage());
}
}
}