mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: fix interception tests (#12105)
This commit is contained in:
parent
6ba6bef1b9
commit
e6e861ce1d
@ -36,6 +36,12 @@ module.exports = {
|
|||||||
selector:
|
selector:
|
||||||
'CallExpression[callee.object.name="it"] > MemberExpression > Identifier[name="deflake"], CallExpression[callee.object.name="it"] > MemberExpression > Identifier[name="deflakeOnly"]',
|
'CallExpression[callee.object.name="it"] > MemberExpression > Identifier[name="deflake"], CallExpression[callee.object.name="it"] > MemberExpression > Identifier[name="deflakeOnly"]',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
message:
|
||||||
|
'No `expect` in EventHandler. They will never throw errors',
|
||||||
|
selector:
|
||||||
|
'CallExpression[callee.property.name="on"] BlockStatement > :not(TryStatement) > ExpressionStatement > CallExpression[callee.object.callee.name="expect"]',
|
||||||
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -1213,13 +1213,15 @@ describe('Page', function () {
|
|||||||
expect(result).toBe(36);
|
expect(result).toBe(36);
|
||||||
await page.removeExposedFunction('compute');
|
await page.removeExposedFunction('compute');
|
||||||
|
|
||||||
let error: Error | null = null;
|
const error = await page
|
||||||
await page
|
|
||||||
.evaluate(async function () {
|
.evaluate(async function () {
|
||||||
return (globalThis as any).compute(9, 4);
|
return (globalThis as any).compute(9, 4);
|
||||||
})
|
})
|
||||||
.catch(_error => {
|
.then(() => {
|
||||||
return (error = _error);
|
return null;
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
return error;
|
||||||
});
|
});
|
||||||
expect(error).toBeTruthy();
|
expect(error).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
@ -94,24 +94,36 @@ describe('cooperative request interception', function () {
|
|||||||
const {page, server} = await getTestState();
|
const {page, server} = await getTestState();
|
||||||
|
|
||||||
await page.setRequestInterception(true);
|
await page.setRequestInterception(true);
|
||||||
|
let requestError;
|
||||||
page.on('request', request => {
|
page.on('request', request => {
|
||||||
if (isFavicon(request)) {
|
if (isFavicon(request)) {
|
||||||
void request.continue({}, 0);
|
void request.continue({}, 0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
expect(request.url()).toContain('empty.html');
|
try {
|
||||||
expect(request.headers()['user-agent']).toBeTruthy();
|
expect(request).toBeTruthy();
|
||||||
expect(request.method()).toBe('GET');
|
expect(request.url()).toContain('empty.html');
|
||||||
expect(request.postData()).toBe(undefined);
|
expect(request.headers()['user-agent']).toBeTruthy();
|
||||||
expect(request.isNavigationRequest()).toBe(true);
|
expect(request.method()).toBe('GET');
|
||||||
expect(request.resourceType()).toBe('document');
|
expect(request.postData()).toBe(undefined);
|
||||||
expect(request.frame() === page.mainFrame()).toBe(true);
|
expect(request.isNavigationRequest()).toBe(true);
|
||||||
expect(request.frame()!.url()).toBe('about:blank');
|
expect(request.resourceType()).toBe('document');
|
||||||
void request.continue({}, 0);
|
expect(request.frame()!.url()).toBe('about:blank');
|
||||||
|
expect(request.frame() === page.mainFrame()).toBe(true);
|
||||||
|
} catch (error) {
|
||||||
|
requestError = error;
|
||||||
|
} finally {
|
||||||
|
void request.continue({}, 0);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const response = (await page.goto(server.EMPTY_PAGE))!;
|
const response = (await page.goto(server.EMPTY_PAGE))!;
|
||||||
expect(response!.ok()).toBe(true);
|
if (requestError) {
|
||||||
expect(response!.remoteAddress().port).toBe(server.PORT);
|
throw requestError;
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(response.ok()).toBe(true);
|
||||||
|
expect(response.remoteAddress().port).toBe(server.PORT);
|
||||||
});
|
});
|
||||||
// @see https://github.com/puppeteer/puppeteer/pull/3105
|
// @see https://github.com/puppeteer/puppeteer/pull/3105
|
||||||
it('should work when POST is redirected with 302', async () => {
|
it('should work when POST is redirected with 302', async () => {
|
||||||
@ -141,16 +153,24 @@ describe('cooperative request interception', function () {
|
|||||||
|
|
||||||
server.setRedirect('/rrredirect', '/empty.html');
|
server.setRedirect('/rrredirect', '/empty.html');
|
||||||
await page.setRequestInterception(true);
|
await page.setRequestInterception(true);
|
||||||
|
let requestError;
|
||||||
page.on('request', request => {
|
page.on('request', request => {
|
||||||
const headers = Object.assign({}, request.headers(), {
|
const headers = Object.assign({}, request.headers(), {
|
||||||
foo: 'bar',
|
foo: 'bar',
|
||||||
});
|
});
|
||||||
void request.continue({headers}, 0);
|
void request.continue({headers}, 0);
|
||||||
|
try {
|
||||||
expect(request.continueRequestOverrides()).toEqual({headers});
|
expect(request.continueRequestOverrides()).toEqual({headers});
|
||||||
|
} catch (error) {
|
||||||
|
requestError = error;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
// Make sure that the goto does not time out.
|
// Make sure that the goto does not time out.
|
||||||
await page.goto(server.PREFIX + '/rrredirect');
|
await page.goto(server.PREFIX + '/rrredirect');
|
||||||
|
|
||||||
|
if (requestError) {
|
||||||
|
throw requestError;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
// @see https://github.com/puppeteer/puppeteer/issues/4743
|
// @see https://github.com/puppeteer/puppeteer/issues/4743
|
||||||
it('should be able to remove headers', async () => {
|
it('should be able to remove headers', async () => {
|
||||||
@ -220,11 +240,20 @@ describe('cooperative request interception', function () {
|
|||||||
foo: 'bar',
|
foo: 'bar',
|
||||||
});
|
});
|
||||||
await page.setRequestInterception(true);
|
await page.setRequestInterception(true);
|
||||||
|
let requestError;
|
||||||
page.on('request', request => {
|
page.on('request', request => {
|
||||||
expect(request.headers()['foo']).toBe('bar');
|
try {
|
||||||
void request.continue({}, 0);
|
expect(request.headers()['foo']).toBe('bar');
|
||||||
|
} catch (error) {
|
||||||
|
requestError = error;
|
||||||
|
} finally {
|
||||||
|
void request.continue({}, 0);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
const response = await page.goto(server.EMPTY_PAGE);
|
const response = await page.goto(server.EMPTY_PAGE);
|
||||||
|
if (requestError) {
|
||||||
|
throw requestError;
|
||||||
|
}
|
||||||
expect(response!.ok()).toBe(true);
|
expect(response!.ok()).toBe(true);
|
||||||
});
|
});
|
||||||
// @see https://github.com/puppeteer/puppeteer/issues/4337
|
// @see https://github.com/puppeteer/puppeteer/issues/4337
|
||||||
@ -250,11 +279,20 @@ describe('cooperative request interception', function () {
|
|||||||
|
|
||||||
await page.setExtraHTTPHeaders({referer: server.EMPTY_PAGE});
|
await page.setExtraHTTPHeaders({referer: server.EMPTY_PAGE});
|
||||||
await page.setRequestInterception(true);
|
await page.setRequestInterception(true);
|
||||||
|
let requestError;
|
||||||
page.on('request', request => {
|
page.on('request', request => {
|
||||||
expect(request.headers()['referer']).toBe(server.EMPTY_PAGE);
|
try {
|
||||||
void request.continue({}, 0);
|
expect(request.headers()['referer']).toBe(server.EMPTY_PAGE);
|
||||||
|
} catch (error) {
|
||||||
|
requestError = error;
|
||||||
|
} finally {
|
||||||
|
void request.continue({}, 0);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
const response = await page.goto(server.EMPTY_PAGE);
|
const response = await page.goto(server.EMPTY_PAGE);
|
||||||
|
if (requestError) {
|
||||||
|
throw requestError;
|
||||||
|
}
|
||||||
expect(response!.ok()).toBe(true);
|
expect(response!.ok()).toBe(true);
|
||||||
});
|
});
|
||||||
it('should be abortable', async () => {
|
it('should be abortable', async () => {
|
||||||
@ -947,14 +985,26 @@ describe('cooperative request interception', function () {
|
|||||||
page.on('request', request => {
|
page.on('request', request => {
|
||||||
void request.continue();
|
void request.continue();
|
||||||
});
|
});
|
||||||
|
let requestError;
|
||||||
page.on('request', request => {
|
page.on('request', request => {
|
||||||
expect(request.isInterceptResolutionHandled()).toBeTruthy();
|
try {
|
||||||
|
expect(request.isInterceptResolutionHandled()).toBeTruthy();
|
||||||
|
} catch (error) {
|
||||||
|
requestError = error;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
page.on('request', request => {
|
page.on('request', request => {
|
||||||
const {action} = request.interceptResolutionState();
|
const {action} = request.interceptResolutionState();
|
||||||
expect(action).toBe(InterceptResolutionAction.AlreadyHandled);
|
try {
|
||||||
|
expect(action).toBe(InterceptResolutionAction.AlreadyHandled);
|
||||||
|
} catch (error) {
|
||||||
|
requestError = error;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
await page.goto(server.EMPTY_PAGE);
|
await page.goto(server.EMPTY_PAGE);
|
||||||
|
if (requestError) {
|
||||||
|
throw requestError;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -22,23 +22,34 @@ describe('request interception', function () {
|
|||||||
const {page, server} = await getTestState();
|
const {page, server} = await getTestState();
|
||||||
|
|
||||||
await page.setRequestInterception(true);
|
await page.setRequestInterception(true);
|
||||||
|
let requestError;
|
||||||
page.on('request', request => {
|
page.on('request', request => {
|
||||||
if (isFavicon(request)) {
|
if (isFavicon(request)) {
|
||||||
void request.continue();
|
void request.continue();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
expect(request.url()).toContain('empty.html');
|
try {
|
||||||
expect(request.headers()['user-agent']).toBeTruthy();
|
expect(request).toBeTruthy();
|
||||||
expect(request.headers()['accept']).toBeTruthy();
|
expect(request.url()).toContain('empty.html');
|
||||||
expect(request.method()).toBe('GET');
|
expect(request.headers()['user-agent']).toBeTruthy();
|
||||||
expect(request.postData()).toBe(undefined);
|
expect(request.method()).toBe('GET');
|
||||||
expect(request.isNavigationRequest()).toBe(true);
|
expect(request.postData()).toBe(undefined);
|
||||||
expect(request.resourceType()).toBe('document');
|
expect(request.isNavigationRequest()).toBe(true);
|
||||||
expect(request.frame() === page.mainFrame()).toBe(true);
|
expect(request.resourceType()).toBe('document');
|
||||||
expect(request.frame()!.url()).toBe('about:blank');
|
expect(request.frame()!.url()).toBe('about:blank');
|
||||||
void request.continue();
|
expect(request.frame() === page.mainFrame()).toBe(true);
|
||||||
|
} catch (error) {
|
||||||
|
requestError = error;
|
||||||
|
} finally {
|
||||||
|
void request.continue();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const response = (await page.goto(server.EMPTY_PAGE))!;
|
const response = (await page.goto(server.EMPTY_PAGE))!;
|
||||||
|
if (requestError) {
|
||||||
|
throw requestError;
|
||||||
|
}
|
||||||
|
|
||||||
expect(response.ok()).toBe(true);
|
expect(response.ok()).toBe(true);
|
||||||
expect(response.remoteAddress().port).toBe(server.PORT);
|
expect(response.remoteAddress().port).toBe(server.PORT);
|
||||||
});
|
});
|
||||||
@ -162,11 +173,21 @@ describe('request interception', function () {
|
|||||||
foo: 'bar',
|
foo: 'bar',
|
||||||
});
|
});
|
||||||
await page.setRequestInterception(true);
|
await page.setRequestInterception(true);
|
||||||
|
let requestError;
|
||||||
page.on('request', request => {
|
page.on('request', request => {
|
||||||
expect(request.headers()['foo']).toBe('bar');
|
try {
|
||||||
void request.continue();
|
expect(request.headers()['foo']).toBe('bar');
|
||||||
|
} catch (error) {
|
||||||
|
requestError = error;
|
||||||
|
} finally {
|
||||||
|
void request.continue();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const response = (await page.goto(server.EMPTY_PAGE))!;
|
const response = (await page.goto(server.EMPTY_PAGE))!;
|
||||||
|
if (requestError) {
|
||||||
|
throw requestError;
|
||||||
|
}
|
||||||
expect(response.ok()).toBe(true);
|
expect(response.ok()).toBe(true);
|
||||||
});
|
});
|
||||||
// @see https://github.com/puppeteer/puppeteer/issues/4337
|
// @see https://github.com/puppeteer/puppeteer/issues/4337
|
||||||
@ -192,11 +213,13 @@ describe('request interception', function () {
|
|||||||
|
|
||||||
await page.setExtraHTTPHeaders({referer: server.EMPTY_PAGE});
|
await page.setExtraHTTPHeaders({referer: server.EMPTY_PAGE});
|
||||||
await page.setRequestInterception(true);
|
await page.setRequestInterception(true);
|
||||||
page.on('request', request => {
|
let request!: HTTPRequest;
|
||||||
expect(request.headers()['referer']).toBe(server.EMPTY_PAGE);
|
page.on('request', req => {
|
||||||
|
request = req;
|
||||||
void request.continue();
|
void request.continue();
|
||||||
});
|
});
|
||||||
const response = (await page.goto(server.EMPTY_PAGE))!;
|
const response = (await page.goto(server.EMPTY_PAGE))!;
|
||||||
|
expect(request.headers()['referer']).toBe(server.EMPTY_PAGE);
|
||||||
expect(response.ok()).toBe(true);
|
expect(response.ok()).toBe(true);
|
||||||
});
|
});
|
||||||
it('should be abortable', async () => {
|
it('should be abortable', async () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user