public interface Route
Page.route()
or BrowserContext.route()
, the Route
object allows to handle the route.
Learn more about networking.
Modifier and Type | Interface and Description |
---|---|
static class |
Route.FulfillOptions |
static class |
Route.ResumeOptions |
Modifier and Type | Method and Description |
---|---|
default void |
abort()
Aborts the route's request.
|
void |
abort(String errorCode)
Aborts the route's request.
|
default void |
fulfill()
Fulfills route's request with given response.
|
void |
fulfill(Route.FulfillOptions options)
Fulfills route's request with given response.
|
Request |
request()
A request to be routed.
|
default void |
resume()
Continues route's request with optional overrides.
|
void |
resume(Route.ResumeOptions options)
Continues route's request with optional overrides.
|
default void abort()
void abort(String errorCode)
errorCode
- Optional error code. Defaults to failed
, could be one of the following:
"aborted"
- An operation was aborted (due to user action)"accessdenied"
- Permission to access a resource, other than the network, was denied"addressunreachable"
- The IP address is unreachable. This usually means that there is no route to the specified host
or network."blockedbyclient"
- The client chose to block the request."blockedbyresponse"
- The request failed because the response was delivered along with requirements which are not met
('X-Frame-Options' and 'Content-Security-Policy' ancestor checks, for instance)."connectionaborted"
- A connection timed out as a result of not receiving an ACK for data sent."connectionclosed"
- A connection was closed (corresponding to a TCP FIN)."connectionfailed"
- A connection attempt failed."connectionrefused"
- A connection attempt was refused."connectionreset"
- A connection was reset (corresponding to a TCP RST)."internetdisconnected"
- The Internet connection has been lost."namenotresolved"
- The host name could not be resolved."timedout"
- An operation timed out."failed"
- A generic failure occurred.default void resume()
page.route("**\/*", route -> {
// Override headers
Map<String, String> headers = new HashMap<>(route.request().headers());
headers.put("foo", "bar"); // set "foo" header
headers.remove("origin"); // remove "origin" header
route.resume(new Route.ResumeOptions().setHeaders(headers));
});
void resume(Route.ResumeOptions options)
page.route("**\/*", route -> {
// Override headers
Map<String, String> headers = new HashMap<>(route.request().headers());
headers.put("foo", "bar"); // set "foo" header
headers.remove("origin"); // remove "origin" header
route.resume(new Route.ResumeOptions().setHeaders(headers));
});
default void fulfill()
An example of fulfilling all requests with 404 responses:
page.route("**\/*", route -> {
route.fulfill(new Route.FulfillOptions()
.setStatus(404)
.setContentType("text/plain")
.setBody("Not Found!"));
});
An example of serving static file:
page.route("**\/xhr_endpoint", route -> route.fulfill(
new Route.FulfillOptions().setPath(Paths.get("mock_data.json"))));
void fulfill(Route.FulfillOptions options)
An example of fulfilling all requests with 404 responses:
page.route("**\/*", route -> {
route.fulfill(new Route.FulfillOptions()
.setStatus(404)
.setContentType("text/plain")
.setBody("Not Found!"));
});
An example of serving static file:
page.route("**\/xhr_endpoint", route -> route.fulfill(
new Route.FulfillOptions().setPath(Paths.get("mock_data.json"))));
Request request()
Copyright © 2022. All rights reserved.