fix: throw if intercepted response fails (#8195)

This commit is contained in:
Yury Semikhatsky 2021-08-16 16:40:47 -07:00 коммит произвёл GitHub
Родитель 1e8e547023
Коммит 8cc4140933
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 43 добавлений и 7 удалений

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

@ -13,12 +13,12 @@
},
{
"name": "firefox",
"revision": "1283",
"revision": "1284",
"installByDefault": true
},
{
"name": "firefox-beta",
"revision": "1278",
"revision": "1279",
"installByDefault": false
},
{

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

@ -488,6 +488,12 @@ class RouteImpl implements network.RouteDelegate {
const event = await this._responseInterceptedPromise;
this._interceptionId = event.requestId;
// FIXME: plumb status text from browser
if (event.responseErrorReason) {
this._client._sendMayFail('Fetch.continueRequest', {
requestId: event.requestId
});
throw new Error(`Request failed: ${event.responseErrorReason}`);
}
return new network.InterceptedResponse(request, event.responseStatusCode!, '', event.responseHeaders!);
}

1
src/server/firefox/protocol.d.ts поставляемый
Просмотреть файл

@ -935,6 +935,7 @@ export module Protocol {
value: string;
}[];
};
error?: string;
};
export type fulfillInterceptedRequestParameters = {
requestId: string;

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

@ -97,8 +97,8 @@ export class WKRouteImpl implements network.RouteDelegate {
private readonly _requestId: string;
_requestInterceptedCallback: () => void = () => {};
private readonly _requestInterceptedPromise: Promise<unknown>;
_responseInterceptedCallback: ((responsePayload: Protocol.Network.Response) => void) | undefined;
private _responseInterceptedPromise: Promise<Protocol.Network.Response> | undefined;
_responseInterceptedCallback: ((payload: { response?: Protocol.Network.Response, error?: Protocol.Network.loadingFailedPayload }) => void) | undefined;
private _responseInterceptedPromise: Promise<{ response?: Protocol.Network.Response, error?: Protocol.Network.loadingFailedPayload }> | undefined;
private readonly _page: WKPage;
constructor(session: WKSession, page: WKPage, requestId: string) {
@ -165,8 +165,10 @@ export class WKRouteImpl implements network.RouteDelegate {
});
if (!this._responseInterceptedPromise)
return null;
const responsePayload = await this._responseInterceptedPromise;
return new InterceptedResponse(request, responsePayload.status, responsePayload.statusText, headersObjectToArray(responsePayload.headers));
const { response, error } = await this._responseInterceptedPromise;
if (error)
throw new Error(`Request failed: ${error.errorText}`);
return new InterceptedResponse(request, response!.status, response!.statusText, headersObjectToArray(response!.headers));
}
}

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

@ -1008,7 +1008,7 @@ export class WKPage implements PageDelegate {
session.sendMayFail('Network.interceptContinue', { requestId: event.requestId, stage: 'response' });
return;
}
route._responseInterceptedCallback(event.response);
route._responseInterceptedCallback({ response: event.response });
}
_onResponseReceived(event: Protocol.Network.responseReceivedPayload) {
@ -1068,6 +1068,9 @@ export class WKPage implements PageDelegate {
// @see https://crbug.com/750469
if (!request)
return;
const route = request._routeForRedirectChain();
if (route?._responseInterceptedCallback)
route._responseInterceptedCallback({ error: event });
const response = request.request._existingResponse();
if (response) {
response._serverAddrFinished();

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

@ -85,6 +85,30 @@ it('should support fulfill after intercept', async ({page, server}) => {
expect(await response.text()).toBe('<title>Woof-Woof</title>' + os.EOL);
});
it('should intercept failures', async ({page, browserName, server}) => {
server.setRoute('/title.html', (req, res) => {
req.destroy();
});
const requestPromise = server.waitForRequest('/title.html');
let error;
await page.route('**', async route => {
try {
// @ts-expect-error
await route._intercept();
await route.fulfill();
} catch (e) {
error = e;
}
});
const [request] = await Promise.all([
requestPromise,
page.goto(server.PREFIX + '/title.html').catch(e => {})
]);
expect(error).toBeTruthy();
expect(error.message).toContain('Request failed');
expect(request.url).toBe('/title.html');
});
it('should support request overrides', async ({page, server, browserName, browserMajorVersion}) => {
it.skip(browserName === 'chromium' && browserMajorVersion <= 91);
const requestPromise = server.waitForRequest('/empty.html');