Closed jgraham closed 5 months ago
The Browser Testing and Tools Working Group just discussed Network interception
.
The Browser Testing and Tools Working Group just discussed Network request interception
.
I'm interested in having the this feature being spec'd. As Webdriver doesn't currently have an equivelant to Network.getResponseBody. Which is important to me because I would like to use the feature in Firefox but it looks like their CDP implementation is maintenance mode in favor Webdriver bidi related APIs. Until then only a fork of firefox shipped with Playwright can get response body. Which I'm uncomfortable depending on.
I've started a (thus far incomplete) draft of this feature at https://github.com/w3c/webdriver-bidi/compare/network_interception
The design at this point is as follows:
network.addIntercept
. That takes a list of URL patterns the request can match (pattern language unspecified, but probably glob-like) and a list of phases at which you want to intercept (beforeRequestSent
/ auth
/ responseStarted
).network.removeIntercept
to disable the interception. This means that different parts of a client can independently register intercepts, and only remove the intercepts they registered (like preload scripts). The client is responsible for handling the case where there's more than one intercept registered for a specific request (see below).isBlocked
which is always sent and set to true if the request is being intercepted, and intercepts
which is a list of registered intercepts for the request, so the client can handle the case of multiple intercepts.auth
intercept phase is similar to responseStarted
, but only when the status code and headers indicate that HTTP Auth credentials are set.responseStarted
phase), plus setting the auth credentials to use for future requests. Modifying the response body is difficult with this protocol design since it either requires many back/forth events to deliver chunks of the response to the bidi client before they get to the browser, or a mechanism for allowing the browser itself to run the intercept code (e.g. sending over a function that is called with chunks of the body). So I expect to defer that to future versions of the protocol. Ignoring that, there are two obvious possible designs for responding to the intercept:
network.continueRequest
, but to set a response we'd have network.fulfillRequest
(or similar names).netwok.continueFetch
. This would allow setting either request or response properties, but it would be an error to attempt to set request properties after the request was sent. The behaviour would be determined by which fields are present in the command payload. So to just modify the request you'd set a request
field with relevant sub-fields indicating the updated properties, whereas if you wanted to modify or fulfill the response, you'd set the corresponding response
properties. The main question here is whether it's confusing that the actual action depends on the details of the payload parameters rather than the actual body itself. In principle this is also more flexible because you can do multiple actions at once (e.g. both modify the request headers and set credentials if you expect HTTP Auth to be required). Maybe one could extend it to modifying both the request and parts of the response in the request phase (e.g. both set the request body and some response headers, whilst still going to the network for the actual response body), but that may be unnecessary.I have double checked the current implementation in Puppeteer and here are some thoughts:
1) it would be great to allow intercepting requests for a context and related contexts (e.g., an entire tab): seems to be not in the current draft?
2) Puppeteer only supports "modifying the request headers and body, setting the full response headers / body" without "changing the response headers" or "modifying the response body" parts. CDP supports the latter two as well.
3) Having multiple commands (network.continueRequest
, network.fulfillRequest
and network.failRequest
) I think is more clear but combining them into one command would also work.
4) WDYT about isPaused
or isIntercepted
instead of isBlocked
for the indication of the interception? we could also rely on the presence/absence of intercepts
, I assume?
The Browser Testing and Tools Working Group just discussed Network request interception
.
Has there been a consideration of enhancing this feature to allow intercepting WebSocket messages?
Has there been a consideration of enhancing this feature to allow intercepting WebSocket messages?
Would you mind filing this as a new enhancement issue request?
This issue should basically be done, or is there something missing @jgraham?
Going to mark this issue as done based on the merge of #429 a while ago.
A common use case for automation is to take a request that would be made e.g. to a third party API and provide a mock response for that reques, or to take a request that requires HTTP authentication and automatically provide that authentication. This is a feature that's exposed in Puppeteer and is in the works for Selenium.
The existing CDP API is the Fetch domain. Something similar has been added to WebKit. Gecko has similar features with a different API exposed to extensions.
Given that the CDP-ish model is already widely implemented in browsers and used in clients, it makes sense to spec something close to that. Practically this means:
Auth seems to be handled somewhat seperately, with the following: