Skip to content

Commit

Permalink
feat(wk,ff): amend method & postData upon continue (#703)
Browse files Browse the repository at this point in the history
Fixes #668
  • Loading branch information
pavelfeldman authored Jan 28, 2020
1 parent c35c65b commit 09e97af
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 10 deletions.
2 changes: 2 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2849,6 +2849,8 @@ Exception is immediately thrown if the request interception is not enabled.

#### request.continue([overrides])
- `overrides` <[Object]> Optional request overwrites, which can be one of the following:
- `method` <[string]> If set changes the request method (e.g. GET or POST)
- `postData` <[string]> If set changes the post data of request
- `headers` <[Object]> If set changes the request HTTP headers. Header values will be converted to a string.
- returns: <[Promise]>

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"main": "index.js",
"playwright": {
"chromium_revision": "733125",
"firefox_revision": "1018",
"firefox_revision": "1019",
"webkit_revision": "1119"
},
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion src/chromium/crNetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,12 @@ class InterceptableRequest implements network.RequestDelegate {
event.request.url, (event.type || '').toLowerCase(), event.request.method, event.request.postData, headersObject(event.request.headers));
}

async continue(overrides: { headers?: network.Headers; } = {}) {
async continue(overrides: { method?: string; headers?: network.Headers; postData?: string } = {}) {
await this._client.send('Fetch.continueRequest', {
requestId: this._interceptionId!,
headers: overrides.headers ? headersArray(overrides.headers) : undefined,
method: overrides.method,
postData: overrides.postData
}).catch(error => {
// In certain cases, protocol will return error if the request was already canceled
// or the page was closed. We should tolerate these errors.
Expand Down
6 changes: 5 additions & 1 deletion src/firefox/ffNetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,17 @@ class InterceptableRequest implements network.RequestDelegate {
payload.url, causeToResourceType[payload.cause] || 'other', payload.method, payload.postData, headers);
}

async continue(overrides: { headers?: { [key: string]: string } } = {}) {
async continue(overrides: { method?: string; headers?: network.Headers; postData?: string }) {
const {
method,
headers,
postData
} = overrides;
await this._session.send('Network.resumeInterceptedRequest', {
requestId: this._id,
method,
headers: headers ? headersArray(headers) : undefined,
postData: postData ? Buffer.from(postData).toString('base64') : undefined
}).catch(error => {
debugError(error);
});
Expand Down
4 changes: 2 additions & 2 deletions src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export class Request {
await this._delegate!.fulfill(response);
}

async continue(overrides: { headers?: { [key: string]: string } } = {}) {
async continue(overrides: { method?: string; headers?: Headers; postData?: string } = {}) {
assert(this._delegate, 'Request Interception is not enabled!');
assert(!this._interceptionHandled, 'Request is already handled!');
await this._delegate!.continue(overrides);
Expand Down Expand Up @@ -301,7 +301,7 @@ export class Response {
export interface RequestDelegate {
abort(errorCode: string): Promise<void>;
fulfill(response: { status: number; headers: Headers; contentType: string; body: (string | platform.BufferType); }): Promise<void>;
continue(overrides: { url?: string; method?: string; postData?: string; headers?: Headers; }): Promise<void>;
continue(overrides: { method?: string; headers?: Headers; postData?: string; }): Promise<void>;
}

export class WebSocket extends platform.EventEmitter {
Expand Down
8 changes: 5 additions & 3 deletions src/webkit/wkInterceptableRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@ export class WKInterceptableRequest implements network.RequestDelegate {
});
}

async continue(overrides: { headers?: { [key: string]: string; }; }) {
async continue(overrides: { method?: string; headers?: network.Headers; postData?: string }) {
await this._interceptedPromise;
await this._session.send('Network.interceptContinue', {
requestId: this._requestId,
...overrides
}).catch(error => {
method: overrides.method,
headers: overrides.headers,
postData: overrides.postData ? Buffer.from(overrides.postData).toString('base64') : undefined
}).catch((error: Error) => {
// In certain cases, protocol will return error if the request was already canceled
// or the page was closed. We should tolerate these errors.
debugError(error);
Expand Down
39 changes: 37 additions & 2 deletions test/interception.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
});
});

describe('Interception.continue', function() {
describe('Request.continue', function() {
it('should work', async({page, server}) => {
await page.setRequestInterception(true);
page.on('request', request => request.continue());
Expand All @@ -436,9 +436,44 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
]);
expect(request.headers['foo']).toBe('bar');
});
it('should amend method', async({page, server}) => {
const sRequest = server.waitForRequest('/sleep.zzz');
await page.goto(server.EMPTY_PAGE);
await page.setRequestInterception(true);
page.on('request', request => {
request.continue({ method: 'POST' });
});
const [request] = await Promise.all([
server.waitForRequest('/sleep.zzz'),
page.evaluate(() => fetch('/sleep.zzz'))
]);
expect(request.method).toBe('POST');
expect((await sRequest).method).toBe('POST');
});
it('should amend method on main request', async({page, server}) => {
const request = server.waitForRequest('/empty.html');
await page.setRequestInterception(true);
page.on('request', request => {
request.continue({ method: 'POST' });
});
await page.goto(server.EMPTY_PAGE);
expect((await request).method).toBe('POST');
});
it('should amend post data', async({page, server}) => {
await page.goto(server.EMPTY_PAGE);
await page.setRequestInterception(true);
page.on('request', request => {
request.continue({ postData: 'doggo' });
});
const [serverRequest] = await Promise.all([
server.waitForRequest('/sleep.zzz'),
page.evaluate(() => fetch('/sleep.zzz', { method: 'POST', body: 'birdy' }))
]);
expect(await serverRequest.postBody).toBe('doggo');
});
});

describe('interception.fulfill', function() {
describe('Request.fulfill', function() {
it('should work', async({page, server}) => {
await page.setRequestInterception(true);
page.on('request', request => {
Expand Down

0 comments on commit 09e97af

Please sign in to comment.