fix(route): throw on attempt to fulfill with redirect in WebKit (#4449)

This commit is contained in:
Yury Semikhatsky 2020-11-17 16:56:04 -08:00 committed by GitHub
parent cb21d5dc54
commit a877c24f05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 7 deletions

View File

@ -74,6 +74,9 @@ export class WKInterceptableRequest implements network.RouteDelegate {
}
async fulfill(response: types.NormalizedFulfillResponse) {
if (300 <= response.status && response.status < 400)
throw new Error('Cannot fulfill with redirect status: ' + response.status);
await this._interceptedPromise;
// In certain cases, protocol will return error if the request was already canceled

View File

@ -394,15 +394,17 @@ it('should intercept main resource during cross-process navigation', async ({pag
expect(intercepted).toBe(true);
});
it('should create a redirect', async ({page, server}) => {
await page.goto(server.PREFIX + '/empty.html');
it('should fulfill with redirect status', (test, { browserName, headful}) => {
test.fixme(browserName === 'webkit', 'in WebKit the redirects are handled by the network stack and we intercept before');
}, async ({page, server}) => {
await page.goto(server.PREFIX + '/title.html');
await page.route('**/*', async (route, request) => {
if (request.url() !== server.PREFIX + '/redirect_this')
return route.continue();
await route.fulfill({
status: 301,
headers: {
'location': '/empty.html',
'location': '/title.html',
}
});
});
@ -411,7 +413,41 @@ it('should create a redirect', async ({page, server}) => {
const data = await fetch(url);
return data.text();
}, server.PREFIX + '/redirect_this');
expect(text).toBe('');
expect(text).toBe('<title>Woof-Woof</title>\n');
});
it('should not fulfill with redirect status', (test, { browserName, headful}) => {
test.skip(browserName !== 'webkit', 'we should support fulfill with redirect in webkit and delete this test');
}, async ({page, server}) => {
await page.goto(server.PREFIX + '/empty.html');
let status;
let fulfill;
let reject;
await page.route('**/*', async (route, request) => {
if (request.url() !== server.PREFIX + '/redirect_this')
return route.continue();
try {
await route.fulfill({
status,
headers: {
'location': '/empty.html',
}
});
reject('fullfill didn\'t throw');
} catch (e) {
fulfill(e);
}
});
for (status = 300; status < 310; status++) {
const exception = await Promise.race([
page.evaluate(url => location.href = url, server.PREFIX + '/redirect_this'),
new Promise((f, r) => {fulfill = f; reject = r;})
]) as any;
expect(exception).toBeTruthy();
expect(exception.message.includes('Cannot fulfill with redirect status')).toBe(true);
}
});
it('should support cors with GET', async ({page, server}) => {

View File

@ -50,11 +50,16 @@ it('should amend method', async ({page, server}) => {
});
it('should override request url', async ({page, server}) => {
const request = server.waitForRequest('/empty.html');
const request = server.waitForRequest('/global-var.html');
await page.route('**/foo', route => {
route.continue({ url: server.EMPTY_PAGE });
route.continue({ url: server.PREFIX + '/global-var.html' });
});
await page.goto(server.PREFIX + '/foo');
const [response] = await Promise.all([
page.waitForEvent('response'),
page.goto(server.PREFIX + '/foo'),
]);
expect(response.url()).toBe(server.PREFIX + '/foo');
expect(await page.evaluate(() => window['globalVar'])).toBe(123);
expect((await request).method).toBe('GET');
});