mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
test: consolidate network tests in one file (#2355)
This commit is contained in:
parent
f8cba455ba
commit
04991ad025
@ -14,7 +14,9 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const {waitEvent} = require('./utils');
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
const utils = require('./utils');
|
||||||
|
|
||||||
module.exports.addTests = function({testRunner, expect}) {
|
module.exports.addTests = function({testRunner, expect}) {
|
||||||
const {describe, xdescribe, fdescribe} = testRunner;
|
const {describe, xdescribe, fdescribe} = testRunner;
|
||||||
@ -121,7 +123,7 @@ module.exports.addTests = function({testRunner, expect}) {
|
|||||||
// send request and wait for server response
|
// send request and wait for server response
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
page.evaluate(() => fetch('./get', { method: 'GET'})),
|
page.evaluate(() => fetch('./get', { method: 'GET'})),
|
||||||
waitEvent(page, 'response')
|
utils.waitEvent(page, 'response')
|
||||||
]);
|
]);
|
||||||
|
|
||||||
expect(serverResponse).toBeTruthy();
|
expect(serverResponse).toBeTruthy();
|
||||||
@ -196,4 +198,380 @@ module.exports.addTests = function({testRunner, expect}) {
|
|||||||
expect(redirectChain[0].url()).toContain('/foo.html');
|
expect(redirectChain[0].url()).toContain('/foo.html');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Page.setRequestInterception', function() {
|
||||||
|
it('should intercept', async({page, server}) => {
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
page.on('request', request => {
|
||||||
|
expect(request.url()).toContain('empty.html');
|
||||||
|
expect(request.headers()['user-agent']).toBeTruthy();
|
||||||
|
expect(request.method()).toBe('GET');
|
||||||
|
expect(request.postData()).toBe(undefined);
|
||||||
|
expect(request.resourceType()).toBe('document');
|
||||||
|
expect(request.frame() === page.mainFrame()).toBe(true);
|
||||||
|
expect(request.frame().url()).toBe('about:blank');
|
||||||
|
request.continue();
|
||||||
|
});
|
||||||
|
const response = await page.goto(server.EMPTY_PAGE);
|
||||||
|
expect(response.ok()).toBe(true);
|
||||||
|
});
|
||||||
|
it('should stop intercepting', async({page, server}) => {
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
page.once('request', request => request.continue());
|
||||||
|
await page.goto(server.EMPTY_PAGE);
|
||||||
|
await page.setRequestInterception(false);
|
||||||
|
await page.goto(server.EMPTY_PAGE);
|
||||||
|
});
|
||||||
|
it('should show custom HTTP headers', async({page, server}) => {
|
||||||
|
await page.setExtraHTTPHeaders({
|
||||||
|
foo: 'bar'
|
||||||
|
});
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
page.on('request', request => {
|
||||||
|
expect(request.headers()['foo']).toBe('bar');
|
||||||
|
request.continue();
|
||||||
|
});
|
||||||
|
const response = await page.goto(server.EMPTY_PAGE);
|
||||||
|
expect(response.ok()).toBe(true);
|
||||||
|
});
|
||||||
|
it('should works with customizing referer headers', async({page, server}) => {
|
||||||
|
await page.setExtraHTTPHeaders({ 'referer': server.EMPTY_PAGE });
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
page.on('request', request => {
|
||||||
|
expect(request.headers()['referer']).toBe(server.EMPTY_PAGE);
|
||||||
|
request.continue();
|
||||||
|
});
|
||||||
|
const response = await page.goto(server.EMPTY_PAGE);
|
||||||
|
expect(response.ok()).toBe(true);
|
||||||
|
});
|
||||||
|
it('should be abortable', async({page, server}) => {
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
page.on('request', request => {
|
||||||
|
if (request.url().endsWith('.css'))
|
||||||
|
request.abort();
|
||||||
|
else
|
||||||
|
request.continue();
|
||||||
|
});
|
||||||
|
let failedRequests = 0;
|
||||||
|
page.on('requestfailed', event => ++failedRequests);
|
||||||
|
const response = await page.goto(server.PREFIX + '/one-style.html');
|
||||||
|
expect(response.ok()).toBe(true);
|
||||||
|
expect(response.request().failure()).toBe(null);
|
||||||
|
expect(failedRequests).toBe(1);
|
||||||
|
});
|
||||||
|
it('should be abortable with custom error codes', async({page, server}) => {
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
page.on('request', request => {
|
||||||
|
request.abort('internetdisconnected');
|
||||||
|
});
|
||||||
|
let failedRequest = null;
|
||||||
|
page.on('requestfailed', request => failedRequest = request);
|
||||||
|
await page.goto(server.EMPTY_PAGE).catch(e => {});
|
||||||
|
expect(failedRequest).toBeTruthy();
|
||||||
|
expect(failedRequest.failure().errorText).toBe('net::ERR_INTERNET_DISCONNECTED');
|
||||||
|
});
|
||||||
|
it('should send referer', async({page, server}) => {
|
||||||
|
await page.setExtraHTTPHeaders({
|
||||||
|
referer: 'http://google.com/'
|
||||||
|
});
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
page.on('request', request => request.continue());
|
||||||
|
const [request] = await Promise.all([
|
||||||
|
server.waitForRequest('/grid.html'),
|
||||||
|
page.goto(server.PREFIX + '/grid.html'),
|
||||||
|
]);
|
||||||
|
expect(request.headers['referer']).toBe('http://google.com/');
|
||||||
|
});
|
||||||
|
it('should amend HTTP headers', async({page, server}) => {
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
page.on('request', request => {
|
||||||
|
const headers = Object.assign({}, request.headers());
|
||||||
|
headers['FOO'] = 'bar';
|
||||||
|
request.continue({ headers });
|
||||||
|
});
|
||||||
|
await page.goto(server.EMPTY_PAGE);
|
||||||
|
const [request] = await Promise.all([
|
||||||
|
server.waitForRequest('/sleep.zzz'),
|
||||||
|
page.evaluate(() => fetch('/sleep.zzz'))
|
||||||
|
]);
|
||||||
|
expect(request.headers['foo']).toBe('bar');
|
||||||
|
});
|
||||||
|
it('should fail navigation when aborting main resource', async({page, server}) => {
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
page.on('request', request => request.abort());
|
||||||
|
let error = null;
|
||||||
|
await page.goto(server.EMPTY_PAGE).catch(e => error = e);
|
||||||
|
expect(error).toBeTruthy();
|
||||||
|
expect(error.message).toContain('net::ERR_FAILED');
|
||||||
|
});
|
||||||
|
it('should work with redirects', async({page, server}) => {
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
const requests = [];
|
||||||
|
page.on('request', request => {
|
||||||
|
request.continue();
|
||||||
|
requests.push(request);
|
||||||
|
});
|
||||||
|
server.setRedirect('/non-existing-page.html', '/non-existing-page-2.html');
|
||||||
|
server.setRedirect('/non-existing-page-2.html', '/non-existing-page-3.html');
|
||||||
|
server.setRedirect('/non-existing-page-3.html', '/non-existing-page-4.html');
|
||||||
|
server.setRedirect('/non-existing-page-4.html', '/empty.html');
|
||||||
|
const response = await page.goto(server.PREFIX + '/non-existing-page.html');
|
||||||
|
expect(response.status()).toBe(200);
|
||||||
|
expect(response.url()).toContain('empty.html');
|
||||||
|
expect(requests.length).toBe(5);
|
||||||
|
expect(requests[2].resourceType()).toBe('document');
|
||||||
|
// Check redirect chain
|
||||||
|
const redirectChain = response.request().redirectChain();
|
||||||
|
expect(redirectChain.length).toBe(4);
|
||||||
|
expect(redirectChain[0].url()).toContain('/non-existing-page.html');
|
||||||
|
expect(redirectChain[2].url()).toContain('/non-existing-page-3.html');
|
||||||
|
for (let i = 0; i < redirectChain.length; ++i) {
|
||||||
|
const request = redirectChain[i];
|
||||||
|
expect(request.redirectChain().indexOf(request)).toBe(i);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
it('should be able to abort redirects', async({page, server}) => {
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
server.setRedirect('/non-existing.json', '/non-existing-2.json');
|
||||||
|
server.setRedirect('/non-existing-2.json', '/simple.html');
|
||||||
|
page.on('request', request => {
|
||||||
|
if (request.url().includes('non-existing-2'))
|
||||||
|
request.abort();
|
||||||
|
else
|
||||||
|
request.continue();
|
||||||
|
});
|
||||||
|
await page.goto(server.EMPTY_PAGE);
|
||||||
|
const result = await page.evaluate(async() => {
|
||||||
|
try {
|
||||||
|
await fetch('/non-existing.json');
|
||||||
|
} catch (e) {
|
||||||
|
return e.message;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
expect(result).toContain('Failed to fetch');
|
||||||
|
});
|
||||||
|
it('should work with equal requests', async({page, server}) => {
|
||||||
|
await page.goto(server.EMPTY_PAGE);
|
||||||
|
let responseCount = 1;
|
||||||
|
server.setRoute('/zzz', (req, res) => res.end((responseCount++) * 11 + ''));
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
|
||||||
|
let spinner = false;
|
||||||
|
// Cancel 2nd request.
|
||||||
|
page.on('request', request => {
|
||||||
|
spinner ? request.abort() : request.continue();
|
||||||
|
spinner = !spinner;
|
||||||
|
});
|
||||||
|
const results = await page.evaluate(() => Promise.all([
|
||||||
|
fetch('/zzz').then(response => response.text()).catch(e => 'FAILED'),
|
||||||
|
fetch('/zzz').then(response => response.text()).catch(e => 'FAILED'),
|
||||||
|
fetch('/zzz').then(response => response.text()).catch(e => 'FAILED'),
|
||||||
|
]));
|
||||||
|
expect(results).toEqual(['11', 'FAILED', '22']);
|
||||||
|
});
|
||||||
|
it('should navigate to dataURL and fire dataURL requests', async({page, server}) => {
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
const requests = [];
|
||||||
|
page.on('request', request => {
|
||||||
|
requests.push(request);
|
||||||
|
request.continue();
|
||||||
|
});
|
||||||
|
const dataURL = 'data:text/html,<div>yo</div>';
|
||||||
|
const response = await page.goto(dataURL);
|
||||||
|
expect(response.status()).toBe(200);
|
||||||
|
expect(requests.length).toBe(1);
|
||||||
|
expect(requests[0].url()).toBe(dataURL);
|
||||||
|
});
|
||||||
|
it('should abort data server', async({page, server}) => {
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
page.on('request', request => {
|
||||||
|
request.abort();
|
||||||
|
});
|
||||||
|
let error = null;
|
||||||
|
await page.goto('data:text/html,No way!').catch(err => error = err);
|
||||||
|
expect(error.message).toContain('net::ERR_FAILED');
|
||||||
|
});
|
||||||
|
it('should navigate to URL with hash and and fire requests without hash', async({page, server}) => {
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
const requests = [];
|
||||||
|
page.on('request', request => {
|
||||||
|
requests.push(request);
|
||||||
|
request.continue();
|
||||||
|
});
|
||||||
|
const response = await page.goto(server.EMPTY_PAGE + '#hash');
|
||||||
|
expect(response.status()).toBe(200);
|
||||||
|
expect(response.url()).toBe(server.EMPTY_PAGE);
|
||||||
|
expect(requests.length).toBe(1);
|
||||||
|
expect(requests[0].url()).toBe(server.EMPTY_PAGE);
|
||||||
|
});
|
||||||
|
it('should work with encoded server', async({page, server}) => {
|
||||||
|
// The requestWillBeSent will report encoded URL, whereas interception will
|
||||||
|
// report URL as-is. @see crbug.com/759388
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
page.on('request', request => request.continue());
|
||||||
|
const response = await page.goto(server.PREFIX + '/some nonexisting page');
|
||||||
|
expect(response.status()).toBe(404);
|
||||||
|
});
|
||||||
|
it('should work with badly encoded server', async({page, server}) => {
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
server.setRoute('/malformed?rnd=%911', (req, res) => res.end());
|
||||||
|
page.on('request', request => request.continue());
|
||||||
|
const response = await page.goto(server.PREFIX + '/malformed?rnd=%911');
|
||||||
|
expect(response.status()).toBe(200);
|
||||||
|
});
|
||||||
|
it('should work with encoded server - 2', async({page, server}) => {
|
||||||
|
// The requestWillBeSent will report URL as-is, whereas interception will
|
||||||
|
// report encoded URL for stylesheet. @see crbug.com/759388
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
const requests = [];
|
||||||
|
page.on('request', request => {
|
||||||
|
request.continue();
|
||||||
|
requests.push(request);
|
||||||
|
});
|
||||||
|
const response = await page.goto(`data:text/html,<link rel="stylesheet" href="${server.PREFIX}/fonts?helvetica|arial"/>`);
|
||||||
|
expect(response.status()).toBe(200);
|
||||||
|
expect(requests.length).toBe(2);
|
||||||
|
expect(requests[1].response().status()).toBe(404);
|
||||||
|
});
|
||||||
|
it('should not throw "Invalid Interception Id" if the request was cancelled', async({page, server}) => {
|
||||||
|
await page.setContent('<iframe></iframe>');
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
let request = null;
|
||||||
|
page.on('request', async r => request = r);
|
||||||
|
page.$eval('iframe', (frame, url) => frame.src = url, server.EMPTY_PAGE),
|
||||||
|
// Wait for request interception.
|
||||||
|
await utils.waitEvent(page, 'request');
|
||||||
|
// Delete frame to cause request to be canceled.
|
||||||
|
await page.$eval('iframe', frame => frame.remove());
|
||||||
|
let error = null;
|
||||||
|
await request.continue().catch(e => error = e);
|
||||||
|
expect(error).toBe(null);
|
||||||
|
});
|
||||||
|
it('should throw if interception is not enabled', async({page, server}) => {
|
||||||
|
let error = null;
|
||||||
|
page.on('request', async request => {
|
||||||
|
try {
|
||||||
|
await request.continue();
|
||||||
|
} catch (e) {
|
||||||
|
error = e;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
await page.goto(server.EMPTY_PAGE);
|
||||||
|
expect(error.message).toContain('Request Interception is not enabled');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Request.respond', function() {
|
||||||
|
it('should work', async({page, server}) => {
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
page.on('request', request => {
|
||||||
|
request.respond({
|
||||||
|
status: 201,
|
||||||
|
headers: {
|
||||||
|
foo: 'bar'
|
||||||
|
},
|
||||||
|
body: 'Yo, page!'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
const response = await page.goto(server.EMPTY_PAGE);
|
||||||
|
expect(response.status()).toBe(201);
|
||||||
|
expect(response.headers().foo).toBe('bar');
|
||||||
|
expect(await page.evaluate(() => document.body.textContent)).toBe('Yo, page!');
|
||||||
|
});
|
||||||
|
it('should allow mocking binary responses', async({page, server}) => {
|
||||||
|
await page.setRequestInterception(true);
|
||||||
|
page.on('request', request => {
|
||||||
|
const imageBuffer = fs.readFileSync(path.join(__dirname, 'assets', 'pptr.png'));
|
||||||
|
request.respond({
|
||||||
|
contentType: 'image/png',
|
||||||
|
body: imageBuffer
|
||||||
|
});
|
||||||
|
});
|
||||||
|
await page.evaluate(PREFIX => {
|
||||||
|
const img = document.createElement('img');
|
||||||
|
img.src = PREFIX + '/does-not-exist.png';
|
||||||
|
document.body.appendChild(img);
|
||||||
|
return new Promise(fulfill => img.onload = fulfill);
|
||||||
|
}, server.PREFIX);
|
||||||
|
const img = await page.$('img');
|
||||||
|
expect(await img.screenshot()).toBeGolden('mock-binary-response.png');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Page.Events.Request', function() {
|
||||||
|
it('should fire', async({page, server}) => {
|
||||||
|
const requests = [];
|
||||||
|
page.on('request', request => requests.push(request));
|
||||||
|
await page.goto(server.EMPTY_PAGE);
|
||||||
|
await utils.attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
||||||
|
expect(requests.length).toBe(2);
|
||||||
|
expect(requests[0].url()).toBe(server.EMPTY_PAGE);
|
||||||
|
expect(requests[0].frame() === page.mainFrame()).toBe(true);
|
||||||
|
expect(requests[0].frame().url()).toBe(server.EMPTY_PAGE);
|
||||||
|
expect(requests[1].url()).toBe(server.EMPTY_PAGE);
|
||||||
|
expect(requests[1].frame() === page.frames()[1]).toBe(true);
|
||||||
|
expect(requests[1].frame().url()).toBe(server.EMPTY_PAGE);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Page.setExtraHTTPHeaders', function() {
|
||||||
|
it('should work', async({page, server}) => {
|
||||||
|
await page.setExtraHTTPHeaders({
|
||||||
|
foo: 'bar'
|
||||||
|
});
|
||||||
|
const [request] = await Promise.all([
|
||||||
|
server.waitForRequest('/empty.html'),
|
||||||
|
page.goto(server.EMPTY_PAGE),
|
||||||
|
]);
|
||||||
|
expect(request.headers['foo']).toBe('bar');
|
||||||
|
});
|
||||||
|
it('should throw for non-string header values', async({page, server}) => {
|
||||||
|
let error = null;
|
||||||
|
try {
|
||||||
|
await page.setExtraHTTPHeaders({ 'foo': 1 });
|
||||||
|
} catch (e) {
|
||||||
|
error = e;
|
||||||
|
}
|
||||||
|
expect(error.message).toBe('Expected value of header "foo" to be String, but "number" is found.');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Page.authenticate', function() {
|
||||||
|
it('should work', async({page, server}) => {
|
||||||
|
server.setAuth('/empty.html', 'user', 'pass');
|
||||||
|
let response = await page.goto(server.EMPTY_PAGE);
|
||||||
|
expect(response.status()).toBe(401);
|
||||||
|
await page.authenticate({
|
||||||
|
username: 'user',
|
||||||
|
password: 'pass'
|
||||||
|
});
|
||||||
|
response = await page.reload();
|
||||||
|
expect(response.status()).toBe(200);
|
||||||
|
});
|
||||||
|
it('should fail if wrong credentials', async({page, server}) => {
|
||||||
|
// Use unique user/password since Chrome caches credentials per origin.
|
||||||
|
server.setAuth('/empty.html', 'user2', 'pass2');
|
||||||
|
await page.authenticate({
|
||||||
|
username: 'foo',
|
||||||
|
password: 'bar'
|
||||||
|
});
|
||||||
|
const response = await page.goto(server.EMPTY_PAGE);
|
||||||
|
expect(response.status()).toBe(401);
|
||||||
|
});
|
||||||
|
it('should allow disable authentication', async({page, server}) => {
|
||||||
|
// Use unique user/password since Chrome caches credentials per origin.
|
||||||
|
server.setAuth('/empty.html', 'user3', 'pass3');
|
||||||
|
await page.authenticate({
|
||||||
|
username: 'user3',
|
||||||
|
password: 'pass3'
|
||||||
|
});
|
||||||
|
let response = await page.goto(server.EMPTY_PAGE);
|
||||||
|
expect(response.status()).toBe(200);
|
||||||
|
await page.authenticate(null);
|
||||||
|
// Navigate to a different origin to bust Chrome's credential caching.
|
||||||
|
response = await page.goto(server.CROSS_PROCESS_PREFIX + '/empty.html');
|
||||||
|
expect(response.status()).toBe(401);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -779,305 +779,6 @@ module.exports.addTests = function({testRunner, expect, puppeteer, DeviceDescrip
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Page.setRequestInterception', function() {
|
|
||||||
it('should intercept', async({page, server}) => {
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
page.on('request', request => {
|
|
||||||
expect(request.url()).toContain('empty.html');
|
|
||||||
expect(request.headers()['user-agent']).toBeTruthy();
|
|
||||||
expect(request.method()).toBe('GET');
|
|
||||||
expect(request.postData()).toBe(undefined);
|
|
||||||
expect(request.resourceType()).toBe('document');
|
|
||||||
expect(request.frame() === page.mainFrame()).toBe(true);
|
|
||||||
expect(request.frame().url()).toBe('about:blank');
|
|
||||||
request.continue();
|
|
||||||
});
|
|
||||||
const response = await page.goto(server.EMPTY_PAGE);
|
|
||||||
expect(response.ok()).toBe(true);
|
|
||||||
});
|
|
||||||
it('should stop intercepting', async({page, server}) => {
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
page.once('request', request => request.continue());
|
|
||||||
await page.goto(server.EMPTY_PAGE);
|
|
||||||
await page.setRequestInterception(false);
|
|
||||||
await page.goto(server.EMPTY_PAGE);
|
|
||||||
});
|
|
||||||
it('should show custom HTTP headers', async({page, server}) => {
|
|
||||||
await page.setExtraHTTPHeaders({
|
|
||||||
foo: 'bar'
|
|
||||||
});
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
page.on('request', request => {
|
|
||||||
expect(request.headers()['foo']).toBe('bar');
|
|
||||||
request.continue();
|
|
||||||
});
|
|
||||||
const response = await page.goto(server.EMPTY_PAGE);
|
|
||||||
expect(response.ok()).toBe(true);
|
|
||||||
});
|
|
||||||
it('should works with customizing referer headers', async({page, server}) => {
|
|
||||||
await page.setExtraHTTPHeaders({ 'referer': server.EMPTY_PAGE });
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
page.on('request', request => {
|
|
||||||
expect(request.headers()['referer']).toBe(server.EMPTY_PAGE);
|
|
||||||
request.continue();
|
|
||||||
});
|
|
||||||
const response = await page.goto(server.EMPTY_PAGE);
|
|
||||||
expect(response.ok()).toBe(true);
|
|
||||||
});
|
|
||||||
it('should be abortable', async({page, server}) => {
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
page.on('request', request => {
|
|
||||||
if (request.url().endsWith('.css'))
|
|
||||||
request.abort();
|
|
||||||
else
|
|
||||||
request.continue();
|
|
||||||
});
|
|
||||||
let failedRequests = 0;
|
|
||||||
page.on('requestfailed', event => ++failedRequests);
|
|
||||||
const response = await page.goto(server.PREFIX + '/one-style.html');
|
|
||||||
expect(response.ok()).toBe(true);
|
|
||||||
expect(response.request().failure()).toBe(null);
|
|
||||||
expect(failedRequests).toBe(1);
|
|
||||||
});
|
|
||||||
it('should be abortable with custom error codes', async({page, server}) => {
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
page.on('request', request => {
|
|
||||||
request.abort('internetdisconnected');
|
|
||||||
});
|
|
||||||
let failedRequest = null;
|
|
||||||
page.on('requestfailed', request => failedRequest = request);
|
|
||||||
await page.goto(server.EMPTY_PAGE).catch(e => {});
|
|
||||||
expect(failedRequest).toBeTruthy();
|
|
||||||
expect(failedRequest.failure().errorText).toBe('net::ERR_INTERNET_DISCONNECTED');
|
|
||||||
});
|
|
||||||
it('should send referer', async({page, server}) => {
|
|
||||||
await page.setExtraHTTPHeaders({
|
|
||||||
referer: 'http://google.com/'
|
|
||||||
});
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
page.on('request', request => request.continue());
|
|
||||||
const [request] = await Promise.all([
|
|
||||||
server.waitForRequest('/grid.html'),
|
|
||||||
page.goto(server.PREFIX + '/grid.html'),
|
|
||||||
]);
|
|
||||||
expect(request.headers['referer']).toBe('http://google.com/');
|
|
||||||
});
|
|
||||||
it('should amend HTTP headers', async({page, server}) => {
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
page.on('request', request => {
|
|
||||||
const headers = Object.assign({}, request.headers());
|
|
||||||
headers['FOO'] = 'bar';
|
|
||||||
request.continue({ headers });
|
|
||||||
});
|
|
||||||
await page.goto(server.EMPTY_PAGE);
|
|
||||||
const [request] = await Promise.all([
|
|
||||||
server.waitForRequest('/sleep.zzz'),
|
|
||||||
page.evaluate(() => fetch('/sleep.zzz'))
|
|
||||||
]);
|
|
||||||
expect(request.headers['foo']).toBe('bar');
|
|
||||||
});
|
|
||||||
it('should fail navigation when aborting main resource', async({page, server}) => {
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
page.on('request', request => request.abort());
|
|
||||||
let error = null;
|
|
||||||
await page.goto(server.EMPTY_PAGE).catch(e => error = e);
|
|
||||||
expect(error).toBeTruthy();
|
|
||||||
expect(error.message).toContain('net::ERR_FAILED');
|
|
||||||
});
|
|
||||||
it('should work with redirects', async({page, server}) => {
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
const requests = [];
|
|
||||||
page.on('request', request => {
|
|
||||||
request.continue();
|
|
||||||
requests.push(request);
|
|
||||||
});
|
|
||||||
server.setRedirect('/non-existing-page.html', '/non-existing-page-2.html');
|
|
||||||
server.setRedirect('/non-existing-page-2.html', '/non-existing-page-3.html');
|
|
||||||
server.setRedirect('/non-existing-page-3.html', '/non-existing-page-4.html');
|
|
||||||
server.setRedirect('/non-existing-page-4.html', '/empty.html');
|
|
||||||
const response = await page.goto(server.PREFIX + '/non-existing-page.html');
|
|
||||||
expect(response.status()).toBe(200);
|
|
||||||
expect(response.url()).toContain('empty.html');
|
|
||||||
expect(requests.length).toBe(5);
|
|
||||||
expect(requests[2].resourceType()).toBe('document');
|
|
||||||
// Check redirect chain
|
|
||||||
const redirectChain = response.request().redirectChain();
|
|
||||||
expect(redirectChain.length).toBe(4);
|
|
||||||
expect(redirectChain[0].url()).toContain('/non-existing-page.html');
|
|
||||||
expect(redirectChain[2].url()).toContain('/non-existing-page-3.html');
|
|
||||||
for (let i = 0; i < redirectChain.length; ++i) {
|
|
||||||
const request = redirectChain[i];
|
|
||||||
expect(request.redirectChain().indexOf(request)).toBe(i);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
it('should be able to abort redirects', async({page, server}) => {
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
server.setRedirect('/non-existing.json', '/non-existing-2.json');
|
|
||||||
server.setRedirect('/non-existing-2.json', '/simple.html');
|
|
||||||
page.on('request', request => {
|
|
||||||
if (request.url().includes('non-existing-2'))
|
|
||||||
request.abort();
|
|
||||||
else
|
|
||||||
request.continue();
|
|
||||||
});
|
|
||||||
await page.goto(server.EMPTY_PAGE);
|
|
||||||
const result = await page.evaluate(async() => {
|
|
||||||
try {
|
|
||||||
await fetch('/non-existing.json');
|
|
||||||
} catch (e) {
|
|
||||||
return e.message;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
expect(result).toContain('Failed to fetch');
|
|
||||||
});
|
|
||||||
it('should work with equal requests', async({page, server}) => {
|
|
||||||
await page.goto(server.EMPTY_PAGE);
|
|
||||||
let responseCount = 1;
|
|
||||||
server.setRoute('/zzz', (req, res) => res.end((responseCount++) * 11 + ''));
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
|
|
||||||
let spinner = false;
|
|
||||||
// Cancel 2nd request.
|
|
||||||
page.on('request', request => {
|
|
||||||
spinner ? request.abort() : request.continue();
|
|
||||||
spinner = !spinner;
|
|
||||||
});
|
|
||||||
const results = await page.evaluate(() => Promise.all([
|
|
||||||
fetch('/zzz').then(response => response.text()).catch(e => 'FAILED'),
|
|
||||||
fetch('/zzz').then(response => response.text()).catch(e => 'FAILED'),
|
|
||||||
fetch('/zzz').then(response => response.text()).catch(e => 'FAILED'),
|
|
||||||
]));
|
|
||||||
expect(results).toEqual(['11', 'FAILED', '22']);
|
|
||||||
});
|
|
||||||
it('should navigate to dataURL and fire dataURL requests', async({page, server}) => {
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
const requests = [];
|
|
||||||
page.on('request', request => {
|
|
||||||
requests.push(request);
|
|
||||||
request.continue();
|
|
||||||
});
|
|
||||||
const dataURL = 'data:text/html,<div>yo</div>';
|
|
||||||
const response = await page.goto(dataURL);
|
|
||||||
expect(response.status()).toBe(200);
|
|
||||||
expect(requests.length).toBe(1);
|
|
||||||
expect(requests[0].url()).toBe(dataURL);
|
|
||||||
});
|
|
||||||
it('should abort data server', async({page, server}) => {
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
page.on('request', request => {
|
|
||||||
request.abort();
|
|
||||||
});
|
|
||||||
let error = null;
|
|
||||||
await page.goto('data:text/html,No way!').catch(err => error = err);
|
|
||||||
expect(error.message).toContain('net::ERR_FAILED');
|
|
||||||
});
|
|
||||||
it('should navigate to URL with hash and and fire requests without hash', async({page, server}) => {
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
const requests = [];
|
|
||||||
page.on('request', request => {
|
|
||||||
requests.push(request);
|
|
||||||
request.continue();
|
|
||||||
});
|
|
||||||
const response = await page.goto(server.EMPTY_PAGE + '#hash');
|
|
||||||
expect(response.status()).toBe(200);
|
|
||||||
expect(response.url()).toBe(server.EMPTY_PAGE);
|
|
||||||
expect(requests.length).toBe(1);
|
|
||||||
expect(requests[0].url()).toBe(server.EMPTY_PAGE);
|
|
||||||
});
|
|
||||||
it('should work with encoded server', async({page, server}) => {
|
|
||||||
// The requestWillBeSent will report encoded URL, whereas interception will
|
|
||||||
// report URL as-is. @see crbug.com/759388
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
page.on('request', request => request.continue());
|
|
||||||
const response = await page.goto(server.PREFIX + '/some nonexisting page');
|
|
||||||
expect(response.status()).toBe(404);
|
|
||||||
});
|
|
||||||
it('should work with badly encoded server', async({page, server}) => {
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
server.setRoute('/malformed?rnd=%911', (req, res) => res.end());
|
|
||||||
page.on('request', request => request.continue());
|
|
||||||
const response = await page.goto(server.PREFIX + '/malformed?rnd=%911');
|
|
||||||
expect(response.status()).toBe(200);
|
|
||||||
});
|
|
||||||
it('should work with encoded server - 2', async({page, server}) => {
|
|
||||||
// The requestWillBeSent will report URL as-is, whereas interception will
|
|
||||||
// report encoded URL for stylesheet. @see crbug.com/759388
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
const requests = [];
|
|
||||||
page.on('request', request => {
|
|
||||||
request.continue();
|
|
||||||
requests.push(request);
|
|
||||||
});
|
|
||||||
const response = await page.goto(`data:text/html,<link rel="stylesheet" href="${server.PREFIX}/fonts?helvetica|arial"/>`);
|
|
||||||
expect(response.status()).toBe(200);
|
|
||||||
expect(requests.length).toBe(2);
|
|
||||||
expect(requests[1].response().status()).toBe(404);
|
|
||||||
});
|
|
||||||
it('should not throw "Invalid Interception Id" if the request was cancelled', async({page, server}) => {
|
|
||||||
await page.setContent('<iframe></iframe>');
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
let request = null;
|
|
||||||
page.on('request', async r => request = r);
|
|
||||||
page.$eval('iframe', (frame, url) => frame.src = url, server.EMPTY_PAGE),
|
|
||||||
// Wait for request interception.
|
|
||||||
await waitEvent(page, 'request');
|
|
||||||
// Delete frame to cause request to be canceled.
|
|
||||||
await page.$eval('iframe', frame => frame.remove());
|
|
||||||
let error = null;
|
|
||||||
await request.continue().catch(e => error = e);
|
|
||||||
expect(error).toBe(null);
|
|
||||||
});
|
|
||||||
it('should throw if interception is not enabled', async({page, server}) => {
|
|
||||||
let error = null;
|
|
||||||
page.on('request', async request => {
|
|
||||||
try {
|
|
||||||
await request.continue();
|
|
||||||
} catch (e) {
|
|
||||||
error = e;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
await page.goto(server.EMPTY_PAGE);
|
|
||||||
expect(error.message).toContain('Request Interception is not enabled');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Request.respond', function() {
|
|
||||||
it('should work', async({page, server}) => {
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
page.on('request', request => {
|
|
||||||
request.respond({
|
|
||||||
status: 201,
|
|
||||||
headers: {
|
|
||||||
foo: 'bar'
|
|
||||||
},
|
|
||||||
body: 'Yo, page!'
|
|
||||||
});
|
|
||||||
});
|
|
||||||
const response = await page.goto(server.EMPTY_PAGE);
|
|
||||||
expect(response.status()).toBe(201);
|
|
||||||
expect(response.headers().foo).toBe('bar');
|
|
||||||
expect(await page.evaluate(() => document.body.textContent)).toBe('Yo, page!');
|
|
||||||
});
|
|
||||||
it('should allow mocking binary responses', async({page, server}) => {
|
|
||||||
await page.setRequestInterception(true);
|
|
||||||
page.on('request', request => {
|
|
||||||
const imageBuffer = fs.readFileSync(path.join(__dirname, 'assets', 'pptr.png'));
|
|
||||||
request.respond({
|
|
||||||
contentType: 'image/png',
|
|
||||||
body: imageBuffer
|
|
||||||
});
|
|
||||||
});
|
|
||||||
await page.evaluate(PREFIX => {
|
|
||||||
const img = document.createElement('img');
|
|
||||||
img.src = PREFIX + '/does-not-exist.png';
|
|
||||||
document.body.appendChild(img);
|
|
||||||
return new Promise(fulfill => img.onload = fulfill);
|
|
||||||
}, server.PREFIX);
|
|
||||||
const img = await page.$('img');
|
|
||||||
expect(await img.screenshot()).toBeGolden('mock-binary-response.png');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Page.Events.Dialog', function() {
|
describe('Page.Events.Dialog', function() {
|
||||||
it('should fire', async({page, server}) => {
|
it('should fire', async({page, server}) => {
|
||||||
page.on('dialog', dialog => {
|
page.on('dialog', dialog => {
|
||||||
@ -1119,22 +820,6 @@ module.exports.addTests = function({testRunner, expect, puppeteer, DeviceDescrip
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Page.Events.Request', function() {
|
|
||||||
it('should fire', async({page, server}) => {
|
|
||||||
const requests = [];
|
|
||||||
page.on('request', request => requests.push(request));
|
|
||||||
await page.goto(server.EMPTY_PAGE);
|
|
||||||
await utils.attachFrame(page, 'frame1', server.EMPTY_PAGE);
|
|
||||||
expect(requests.length).toBe(2);
|
|
||||||
expect(requests[0].url()).toBe(server.EMPTY_PAGE);
|
|
||||||
expect(requests[0].frame() === page.mainFrame()).toBe(true);
|
|
||||||
expect(requests[0].frame().url()).toBe(server.EMPTY_PAGE);
|
|
||||||
expect(requests[1].url()).toBe(server.EMPTY_PAGE);
|
|
||||||
expect(requests[1].frame() === page.frames()[1]).toBe(true);
|
|
||||||
expect(requests[1].frame().url()).toBe(server.EMPTY_PAGE);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Page.$eval', function() {
|
describe('Page.$eval', function() {
|
||||||
it('should work', async({page, server}) => {
|
it('should work', async({page, server}) => {
|
||||||
await page.setContent('<section id="testAttribute">43543</section>');
|
await page.setContent('<section id="testAttribute">43543</section>');
|
||||||
@ -1230,66 +915,6 @@ module.exports.addTests = function({testRunner, expect, puppeteer, DeviceDescrip
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Page.setExtraHTTPHeaders', function() {
|
|
||||||
it('should work', async({page, server}) => {
|
|
||||||
await page.setExtraHTTPHeaders({
|
|
||||||
foo: 'bar'
|
|
||||||
});
|
|
||||||
const [request] = await Promise.all([
|
|
||||||
server.waitForRequest('/empty.html'),
|
|
||||||
page.goto(server.EMPTY_PAGE),
|
|
||||||
]);
|
|
||||||
expect(request.headers['foo']).toBe('bar');
|
|
||||||
});
|
|
||||||
it('should throw for non-string header values', async({page, server}) => {
|
|
||||||
let error = null;
|
|
||||||
try {
|
|
||||||
await page.setExtraHTTPHeaders({ 'foo': 1 });
|
|
||||||
} catch (e) {
|
|
||||||
error = e;
|
|
||||||
}
|
|
||||||
expect(error.message).toBe('Expected value of header "foo" to be String, but "number" is found.');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Page.authenticate', function() {
|
|
||||||
it('should work', async({page, server}) => {
|
|
||||||
server.setAuth('/empty.html', 'user', 'pass');
|
|
||||||
let response = await page.goto(server.EMPTY_PAGE);
|
|
||||||
expect(response.status()).toBe(401);
|
|
||||||
await page.authenticate({
|
|
||||||
username: 'user',
|
|
||||||
password: 'pass'
|
|
||||||
});
|
|
||||||
response = await page.reload();
|
|
||||||
expect(response.status()).toBe(200);
|
|
||||||
});
|
|
||||||
it('should fail if wrong credentials', async({page, server}) => {
|
|
||||||
// Use unique user/password since Chrome caches credentials per origin.
|
|
||||||
server.setAuth('/empty.html', 'user2', 'pass2');
|
|
||||||
await page.authenticate({
|
|
||||||
username: 'foo',
|
|
||||||
password: 'bar'
|
|
||||||
});
|
|
||||||
const response = await page.goto(server.EMPTY_PAGE);
|
|
||||||
expect(response.status()).toBe(401);
|
|
||||||
});
|
|
||||||
it('should allow disable authentication', async({page, server}) => {
|
|
||||||
// Use unique user/password since Chrome caches credentials per origin.
|
|
||||||
server.setAuth('/empty.html', 'user3', 'pass3');
|
|
||||||
await page.authenticate({
|
|
||||||
username: 'user3',
|
|
||||||
password: 'pass3'
|
|
||||||
});
|
|
||||||
let response = await page.goto(server.EMPTY_PAGE);
|
|
||||||
expect(response.status()).toBe(200);
|
|
||||||
await page.authenticate(null);
|
|
||||||
// Navigate to a different origin to bust Chrome's credential caching.
|
|
||||||
response = await page.goto(server.CROSS_PROCESS_PREFIX + '/empty.html');
|
|
||||||
expect(response.status()).toBe(401);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Page.setContent', function() {
|
describe('Page.setContent', function() {
|
||||||
const expectedOutput = '<html><head></head><body><div>hello</div></body></html>';
|
const expectedOutput = '<html><head></head><body><div>hello</div></body></html>';
|
||||||
it('should work', async({page, server}) => {
|
it('should work', async({page, server}) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user