From 41770bf444fa9c6c1192bf152dab58208a1a8d11 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Wed, 11 Aug 2021 11:14:50 -0700 Subject: [PATCH] feat(webkit): response interception after redirects (#8017) --- src/server/webkit/wkInterceptableRequest.ts | 8 +++---- tests/page/page-request-intercept.spec.ts | 23 ++++++++++----------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/server/webkit/wkInterceptableRequest.ts b/src/server/webkit/wkInterceptableRequest.ts index 7a3496c441..2c50d175fb 100644 --- a/src/server/webkit/wkInterceptableRequest.ts +++ b/src/server/webkit/wkInterceptableRequest.ts @@ -108,10 +108,7 @@ export class WKRouteImpl implements network.RouteDelegate { this._requestInterceptedPromise = new Promise(f => this._requestInterceptedCallback = f); } - async responseBody(forFulfill: boolean): Promise { - // Empty buffer will result in the response being used. - if (forFulfill) - return Buffer.from(''); + async responseBody(): Promise { 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) { diff --git a/tests/page/page-request-intercept.spec.ts b/tests/page/page-request-intercept.spec.ts index fc8009c95b..df9cddc4ed 100644 --- a/tests/page/page-request-intercept.spec.ts +++ b/tests/page/page-request-intercept.spec.ts @@ -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('Woof-Woof' + 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));