Enable POST data body override on native automation #8428
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Purpose
Currently overriding POST data body is only possible when running tests with
--disable-native-automation
.Take a look at following example:
This PR will allow this value to be overridden in RequestHooks also when running tests on native automation.
Approach
Based on https://chromedevtools.github.io/devtools-protocol/tot/Fetch/#method-continueRequest we can achieve this feature by just adding
postData
encoded to base64 to contineRequest props.This is already done at
_createContinueEventArgs
to fix upload-like requests:The only thing I've noticed that could be improved is that in the case of
--disable-native-automation
, we need to explicitly correct thecontent-length
header.In the case of native automation, after implementing this feature, the user shouldn't change it, as it leads to query failures, so the example RequestHook would look like this:
class TestHook extends RequestHook { constructor() { super([ { url: /echo.free.beeceptor.com/, method: 'POST' }, ], { includeBody: true, includeHeaders: true, }); } async onRequest(requestEvent: RequestEvent & { requestOptions: any }) { const mockResponse = Buffer.from(JSON.stringify({ test: 'override' })); requestEvent.requestOptions.body = mockResponse; - requestEvent.requestOptions.headers['content-length'] = mockResponse.length.toString(); }
Pre-Merge TODO