Class NetworkInterceptor

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class NetworkInterceptor
    extends java.lang.Object
    implements java.lang.AutoCloseable
    Provides a mechanism for stubbing out responses to requests in drivers which implement HasDevTools. Usage is done by specifying a Route, which will be checked for every request to see if that request should be handled or not. Note that the URLs given to the Route will be fully qualified.

    Example usage:

    
       Route route = Route.matching(req -> GET == req.getMethod() && req.getUri().endsWith("/example"))
         .to(() -> req -> new HttpResponse().setContent(Contents.utf8String("Hello, World!")));
    
       try (NetworkInterceptor interceptor = new NetworkInterceptor(driver, route)) {
         // Your code here.
       }
     

    It is also possible to intercept and modify responses that the browser will receive. Do this by calling NetworkInterceptor(WebDriver, Filter).

    • Field Detail

      • PROCEED_WITH_REQUEST

        public static final HttpResponse PROCEED_WITH_REQUEST
        Return this from a Routable in order to have the browser continue the request unmodified.
    • Constructor Detail

      • NetworkInterceptor

        public NetworkInterceptor​(WebDriver driver,
                                  Routable routable)
      • NetworkInterceptor

        public NetworkInterceptor​(WebDriver driver,
                                  Filter filter)
    • Method Detail

      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
      • convertFromCdpHttpMethod

        protected HttpMethod convertFromCdpHttpMethod​(java.lang.String method)
      • createHttpRequest

        protected HttpRequest createHttpRequest​(java.lang.String cdpMethod,
                                                java.lang.String url,
                                                java.util.Map<java.lang.String,​java.lang.Object> headers,
                                                java.util.Optional<java.lang.String> postData)