feat(wk,ff): amend method & postData upon continue (#703)

Fixes #668
This commit is contained in:
Pavel Feldman 2020-01-28 14:29:46 -08:00 коммит произвёл GitHub
Родитель c35c65b393
Коммит 09e97afd22
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 55 добавлений и 10 удалений

Просмотреть файл

@ -2849,6 +2849,8 @@ Exception is immediately thrown if the request interception is not enabled.
#### request.continue([overrides]) #### request.continue([overrides])
- `overrides` <[Object]> Optional request overwrites, which can be one of the following: - `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. - `headers` <[Object]> If set changes the request HTTP headers. Header values will be converted to a string.
- returns: <[Promise]> - returns: <[Promise]>

Просмотреть файл

@ -9,7 +9,7 @@
"main": "index.js", "main": "index.js",
"playwright": { "playwright": {
"chromium_revision": "733125", "chromium_revision": "733125",
"firefox_revision": "1018", "firefox_revision": "1019",
"webkit_revision": "1119" "webkit_revision": "1119"
}, },
"scripts": { "scripts": {

Просмотреть файл

@ -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)); 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', { await this._client.send('Fetch.continueRequest', {
requestId: this._interceptionId!, requestId: this._interceptionId!,
headers: overrides.headers ? headersArray(overrides.headers) : undefined, headers: overrides.headers ? headersArray(overrides.headers) : undefined,
method: overrides.method,
postData: overrides.postData
}).catch(error => { }).catch(error => {
// In certain cases, protocol will return error if the request was already canceled // In certain cases, protocol will return error if the request was already canceled
// or the page was closed. We should tolerate these errors. // or the page was closed. We should tolerate these errors.

Просмотреть файл

@ -158,13 +158,17 @@ class InterceptableRequest implements network.RequestDelegate {
payload.url, causeToResourceType[payload.cause] || 'other', payload.method, payload.postData, headers); 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 { const {
method,
headers, headers,
postData
} = overrides; } = overrides;
await this._session.send('Network.resumeInterceptedRequest', { await this._session.send('Network.resumeInterceptedRequest', {
requestId: this._id, requestId: this._id,
method,
headers: headers ? headersArray(headers) : undefined, headers: headers ? headersArray(headers) : undefined,
postData: postData ? Buffer.from(postData).toString('base64') : undefined
}).catch(error => { }).catch(error => {
debugError(error); debugError(error);
}); });

Просмотреть файл

@ -211,7 +211,7 @@ export class Request {
await this._delegate!.fulfill(response); 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._delegate, 'Request Interception is not enabled!');
assert(!this._interceptionHandled, 'Request is already handled!'); assert(!this._interceptionHandled, 'Request is already handled!');
await this._delegate!.continue(overrides); await this._delegate!.continue(overrides);
@ -301,7 +301,7 @@ export class Response {
export interface RequestDelegate { export interface RequestDelegate {
abort(errorCode: string): Promise<void>; abort(errorCode: string): Promise<void>;
fulfill(response: { status: number; headers: Headers; contentType: string; body: (string | platform.BufferType); }): 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 { export class WebSocket extends platform.EventEmitter {

Просмотреть файл

@ -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._interceptedPromise;
await this._session.send('Network.interceptContinue', { await this._session.send('Network.interceptContinue', {
requestId: this._requestId, requestId: this._requestId,
...overrides method: overrides.method,
}).catch(error => { 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 // In certain cases, protocol will return error if the request was already canceled
// or the page was closed. We should tolerate these errors. // or the page was closed. We should tolerate these errors.
debugError(error); debugError(error);

Просмотреть файл

@ -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}) => { it('should work', async({page, server}) => {
await page.setRequestInterception(true); await page.setRequestInterception(true);
page.on('request', request => request.continue()); page.on('request', request => request.continue());
@ -436,9 +436,44 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
]); ]);
expect(request.headers['foo']).toBe('bar'); 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}) => { it('should work', async({page, server}) => {
await page.setRequestInterception(true); await page.setRequestInterception(true);
page.on('request', request => { page.on('request', request => {