feat(webkit): response interception after redirects (#8017)

This commit is contained in:
Yury Semikhatsky 2021-08-11 11:14:50 -07:00 committed by GitHub
parent 156c5d48dc
commit 41770bf444
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 17 deletions

View file

@ -108,10 +108,7 @@ export class WKRouteImpl implements network.RouteDelegate {
this._requestInterceptedPromise = new Promise<void>(f => this._requestInterceptedCallback = f);
}
async responseBody(forFulfill: boolean): Promise<Buffer> {
// Empty buffer will result in the response being used.
if (forFulfill)
return Buffer.from('');
async responseBody(): Promise<Buffer> {
const response = await this._session.send('Network.getInterceptedResponseBody', { requestId: this._requestId });
return Buffer.from(response.body, 'base64');
}
@ -120,9 +117,10 @@ export class WKRouteImpl implements network.RouteDelegate {
const errorType = errorReasons[errorCode];
assert(errorType, 'Unknown error code: ' + errorCode);
await this._requestInterceptedPromise;
const isResponseIntercepted = await this._responseInterceptedPromise;
// In certain cases, protocol will return error if the request was already canceled
// or the page was closed. We should tolerate these errors.
await this._session.sendMayFail('Network.interceptRequestWithError', { requestId: this._requestId, errorType });
await this._session.sendMayFail(isResponseIntercepted ? 'Network.interceptResponseWithError' : 'Network.interceptRequestWithError', { requestId: this._requestId, errorType });
}
async fulfill(response: types.NormalizedFulfillResponse) {

View file

@ -57,8 +57,7 @@ it('should throw on continue after intercept', async ({page, server, browserName
}
});
it('should support fulfill after intercept', async ({page, server, browserName, browserMajorVersion}) => {
it.fail(browserName === 'webkit', 'Response body is empty');
it('should support fulfill after intercept', async ({page, server}) => {
const requestPromise = server.waitForRequest('/title.html');
await page.route('**', async route => {
// @ts-expect-error
@ -71,8 +70,7 @@ it('should support fulfill after intercept', async ({page, server, browserName,
expect(await response.text()).toBe('<title>Woof-Woof</title>' + os.EOL);
});
it('should support request overrides', async ({page, server, browserName, browserMajorVersion}) => {
it.skip(browserName === 'chromium' && browserMajorVersion <= 91);
it('should support request overrides', async ({page, server}) => {
const requestPromise = server.waitForRequest('/empty.html');
await page.route('**/foo', async route => {
// @ts-expect-error
@ -92,7 +90,7 @@ it('should support request overrides', async ({page, server, browserName, browse
expect((await request.postBody).toString('utf8')).toBe('my data');
});
it('should give access to the intercepted response', async ({page, server, browserName}) => {
it('should give access to the intercepted response', async ({page, server}) => {
await page.goto(server.EMPTY_PAGE);
let routeCallback;
@ -113,7 +111,7 @@ it('should give access to the intercepted response', async ({page, server, brows
await Promise.all([route.fulfill(), evalPromise]);
});
it('should give access to the intercepted response body', async ({page, server, browserName}) => {
it('should give access to the intercepted response body', async ({page, server}) => {
await page.goto(server.EMPTY_PAGE);
let routeCallback;
@ -132,8 +130,6 @@ it('should give access to the intercepted response body', async ({page, server,
});
it('should be abortable after interception', async ({page, server, browserName}) => {
it.fixme(browserName === 'webkit');
await page.route(/\.css$/, async route => {
// @ts-expect-error
await route._intercept();
@ -151,7 +147,7 @@ it('should be abortable after interception', async ({page, server, browserName})
});
it('should fulfill after redirects', async ({page, server, browserName}) => {
it.fixme(browserName !== 'chromium');
it.fixme(browserName === 'firefox');
server.setRedirect('/redirect/1.html', '/redirect/2.html');
server.setRedirect('/redirect/2.html', '/empty.html');
const expectedUrls = ['/redirect/1.html', '/redirect/2.html', '/empty.html'].map(s => server.PREFIX + s);
@ -194,7 +190,7 @@ it('should fulfill after redirects', async ({page, server, browserName}) => {
});
it('should fulfill original response after redirects', async ({page, browserName, server}) => {
it.fixme(browserName !== 'chromium');
it.fixme(browserName === 'firefox');
server.setRedirect('/redirect/1.html', '/redirect/2.html');
server.setRedirect('/redirect/2.html', '/title.html');
const expectedUrls = ['/redirect/1.html', '/redirect/2.html', '/title.html'].map(s => server.PREFIX + s);
@ -228,7 +224,7 @@ it('should fulfill original response after redirects', async ({page, browserName
});
it('should abort after redirects', async ({page, browserName, server}) => {
it.fixme(browserName !== 'chromium');
it.fixme(browserName === 'firefox');
server.setRedirect('/redirect/1.html', '/redirect/2.html');
server.setRedirect('/redirect/2.html', '/title.html');
const expectedUrls = ['/redirect/1.html', '/redirect/2.html', '/title.html'].map(s => server.PREFIX + s);
@ -251,7 +247,10 @@ it('should abort after redirects', async ({page, browserName, server}) => {
try {
await page.goto(server.PREFIX + '/redirect/1.html');
} catch (e) {
expect(e.message).toContain('ERR_CONNECTION_RESET');
if (browserName === 'webkit')
expect(e.message).toContain('Request intercepted');
else
expect(e.message).toContain('ERR_CONNECTION_RESET');
}
expect(requestUrls).toEqual(expectedUrls);
expect(responseUrls).toEqual(expectedUrls.slice(0, -1));