2021-06-18 21:04:48 +03:00
|
|
|
/**
|
|
|
|
* Copyright 2018 Google Inc. All rights reserved.
|
|
|
|
* Modifications copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-06-23 12:08:35 +03:00
|
|
|
import { fail } from 'assert';
|
2021-08-04 21:54:52 +03:00
|
|
|
import os from 'os';
|
2021-06-18 21:04:48 +03:00
|
|
|
import type { Route } from '../../index';
|
2021-08-04 21:54:52 +03:00
|
|
|
import { expect, test as it } from './pageTest';
|
2021-06-18 21:04:48 +03:00
|
|
|
|
|
|
|
it('should fulfill intercepted response', async ({page, server, browserName}) => {
|
|
|
|
await page.route('**/*', async route => {
|
2021-07-01 19:33:47 +03:00
|
|
|
// @ts-expect-error
|
|
|
|
await route._intercept({});
|
2021-06-18 21:04:48 +03:00
|
|
|
await route.fulfill({
|
|
|
|
status: 201,
|
|
|
|
headers: {
|
|
|
|
foo: 'bar'
|
|
|
|
},
|
|
|
|
contentType: 'text/plain',
|
|
|
|
body: 'Yo, page!'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
const response = await page.goto(server.PREFIX + '/empty.html');
|
|
|
|
expect(response.status()).toBe(201);
|
|
|
|
expect(response.headers().foo).toBe('bar');
|
|
|
|
expect(response.headers()['content-type']).toBe('text/plain');
|
|
|
|
expect(await page.evaluate(() => document.body.textContent)).toBe('Yo, page!');
|
|
|
|
});
|
|
|
|
|
2021-08-12 19:38:46 +03:00
|
|
|
it('should fulfill response with empty body', async ({page, server, browserName, browserMajorVersion}) => {
|
|
|
|
it.skip(browserName === 'chromium' && browserMajorVersion <= 91);
|
2021-08-12 00:47:05 +03:00
|
|
|
await page.route('**/*', async route => {
|
|
|
|
// @ts-expect-error
|
|
|
|
await route._intercept({});
|
|
|
|
await route.fulfill({
|
|
|
|
status: 201,
|
|
|
|
body: ''
|
|
|
|
});
|
|
|
|
});
|
|
|
|
const response = await page.goto(server.PREFIX + '/title.html');
|
|
|
|
expect(response.status()).toBe(201);
|
|
|
|
expect(await response.text()).toBe('');
|
|
|
|
});
|
|
|
|
|
2021-06-18 21:04:48 +03:00
|
|
|
it('should throw on continue after intercept', async ({page, server, browserName}) => {
|
|
|
|
let routeCallback;
|
|
|
|
const routePromise = new Promise<Route>(f => routeCallback = f);
|
|
|
|
await page.route('**', routeCallback);
|
|
|
|
|
|
|
|
page.goto(server.EMPTY_PAGE).catch(e => {});
|
|
|
|
const route = await routePromise;
|
2021-07-01 19:33:47 +03:00
|
|
|
// @ts-expect-error
|
|
|
|
await route._intercept();
|
2021-06-18 21:04:48 +03:00
|
|
|
try {
|
|
|
|
await route.continue();
|
|
|
|
fail('did not throw');
|
|
|
|
} catch (e) {
|
2021-06-23 12:08:35 +03:00
|
|
|
expect(e.message).toContain('Cannot call continue after response interception!');
|
2021-06-18 21:04:48 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-08-11 21:14:50 +03:00
|
|
|
it('should support fulfill after intercept', async ({page, server}) => {
|
2021-08-04 01:29:51 +03:00
|
|
|
const requestPromise = server.waitForRequest('/title.html');
|
2021-06-18 21:04:48 +03:00
|
|
|
await page.route('**', async route => {
|
2021-07-01 19:33:47 +03:00
|
|
|
// @ts-expect-error
|
|
|
|
await route._intercept();
|
2021-06-18 21:04:48 +03:00
|
|
|
await route.fulfill();
|
|
|
|
});
|
2021-08-04 01:29:51 +03:00
|
|
|
const response = await page.goto(server.PREFIX + '/title.html');
|
2021-06-18 21:04:48 +03:00
|
|
|
const request = await requestPromise;
|
2021-08-04 01:29:51 +03:00
|
|
|
expect(request.url).toBe('/title.html');
|
2021-08-04 21:54:52 +03:00
|
|
|
expect(await response.text()).toBe('<title>Woof-Woof</title>' + os.EOL);
|
2021-06-18 21:04:48 +03:00
|
|
|
});
|
|
|
|
|
2021-08-17 02:40:47 +03:00
|
|
|
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');
|
|
|
|
});
|
|
|
|
|
2021-08-12 19:38:46 +03:00
|
|
|
it('should support request overrides', async ({page, server, browserName, browserMajorVersion}) => {
|
|
|
|
it.skip(browserName === 'chromium' && browserMajorVersion <= 91);
|
2021-06-18 21:04:48 +03:00
|
|
|
const requestPromise = server.waitForRequest('/empty.html');
|
|
|
|
await page.route('**/foo', async route => {
|
2021-07-01 19:33:47 +03:00
|
|
|
// @ts-expect-error
|
|
|
|
await route._intercept({
|
2021-06-18 21:04:48 +03:00
|
|
|
url: server.EMPTY_PAGE,
|
|
|
|
method: 'POST',
|
|
|
|
headers: {'foo': 'bar'},
|
|
|
|
postData: 'my data',
|
|
|
|
});
|
|
|
|
await route.fulfill();
|
|
|
|
});
|
|
|
|
await page.goto(server.PREFIX + '/foo');
|
|
|
|
const request = await requestPromise;
|
|
|
|
expect(request.method).toBe('POST');
|
|
|
|
expect(request.url).toBe('/empty.html');
|
|
|
|
expect(request.headers['foo']).toBe('bar');
|
|
|
|
expect((await request.postBody).toString('utf8')).toBe('my data');
|
|
|
|
});
|
|
|
|
|
2021-08-11 21:14:50 +03:00
|
|
|
it('should give access to the intercepted response', async ({page, server}) => {
|
2021-06-18 21:04:48 +03:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
|
|
|
|
let routeCallback;
|
|
|
|
const routePromise = new Promise<Route>(f => routeCallback = f);
|
|
|
|
await page.route('**/title.html', routeCallback);
|
|
|
|
|
2021-08-14 02:28:42 +03:00
|
|
|
const evalPromise = page.evaluate(url => fetch(url), server.PREFIX + '/title.html');
|
2021-06-18 21:04:48 +03:00
|
|
|
|
|
|
|
const route = await routePromise;
|
2021-07-01 19:33:47 +03:00
|
|
|
// @ts-expect-error
|
|
|
|
const response = await route._intercept();
|
2021-06-18 21:04:48 +03:00
|
|
|
|
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
expect(response.ok()).toBeTruthy();
|
|
|
|
expect(response.url()).toBe(server.PREFIX + '/title.html');
|
|
|
|
expect(response.headers()['content-type']).toBe('text/html; charset=utf-8');
|
|
|
|
|
|
|
|
await Promise.all([route.fulfill(), evalPromise]);
|
|
|
|
});
|
|
|
|
|
2021-08-14 02:28:42 +03:00
|
|
|
it('should give access to the intercepted response status text', async ({page, server, browserName}) => {
|
|
|
|
it.fail(browserName === 'chromium', 'Status line is not reported for intercepted responses');
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
server.setRoute('/title.html', (req, res) => {
|
|
|
|
res.statusCode = 200;
|
|
|
|
res.statusMessage = 'You are awesome';
|
|
|
|
res.setHeader('Content-Type', 'text/plain');
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
let routeCallback;
|
|
|
|
const routePromise = new Promise<Route>(f => routeCallback = f);
|
|
|
|
await page.route('**/title.html', routeCallback);
|
|
|
|
const evalPromise = page.evaluate(url => fetch(url), server.PREFIX + '/title.html');
|
|
|
|
const route = await routePromise;
|
|
|
|
// @ts-expect-error
|
|
|
|
const response = await route._intercept();
|
|
|
|
|
|
|
|
expect(response.statusText()).toBe('You are awesome');
|
|
|
|
expect(response.url()).toBe(server.PREFIX + '/title.html');
|
|
|
|
|
|
|
|
await Promise.all([route.fulfill(), evalPromise]);
|
|
|
|
});
|
|
|
|
|
2021-08-11 21:14:50 +03:00
|
|
|
it('should give access to the intercepted response body', async ({page, server}) => {
|
2021-06-18 21:04:48 +03:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
|
|
|
|
let routeCallback;
|
|
|
|
const routePromise = new Promise<Route>(f => routeCallback = f);
|
|
|
|
await page.route('**/simple.json', routeCallback);
|
|
|
|
|
|
|
|
const evalPromise = page.evaluate(url => fetch(url), server.PREFIX + '/simple.json').catch(console.log);
|
|
|
|
|
|
|
|
const route = await routePromise;
|
2021-07-01 19:33:47 +03:00
|
|
|
// @ts-expect-error
|
|
|
|
const response = await route._intercept();
|
2021-06-18 21:04:48 +03:00
|
|
|
|
|
|
|
expect((await response.text())).toBe('{"foo": "bar"}\n');
|
|
|
|
|
|
|
|
await Promise.all([route.fulfill(), evalPromise]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be abortable after interception', async ({page, server, browserName}) => {
|
|
|
|
await page.route(/\.css$/, async route => {
|
2021-07-01 19:33:47 +03:00
|
|
|
// @ts-expect-error
|
|
|
|
await route._intercept();
|
2021-06-18 21:04:48 +03:00
|
|
|
await route.abort();
|
|
|
|
});
|
|
|
|
let failed = false;
|
|
|
|
page.on('requestfailed', request => {
|
|
|
|
if (request.url().includes('.css'))
|
|
|
|
failed = true;
|
|
|
|
});
|
|
|
|
const response = await page.goto(server.PREFIX + '/one-style.html');
|
|
|
|
expect(response.ok()).toBe(true);
|
|
|
|
expect(response.request().failure()).toBe(null);
|
|
|
|
expect(failed).toBe(true);
|
|
|
|
});
|
|
|
|
|
2021-08-05 18:49:02 +03:00
|
|
|
it('should fulfill after redirects', async ({page, server, browserName}) => {
|
2021-07-12 13:07:47 +03:00
|
|
|
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);
|
|
|
|
const requestUrls = [];
|
|
|
|
const responseUrls = [];
|
|
|
|
const requestFinishedUrls = [];
|
|
|
|
page.on('request', request => requestUrls.push(request.url()));
|
|
|
|
page.on('response', response => responseUrls.push(response.url()));
|
|
|
|
page.on('requestfinished', request => requestFinishedUrls.push(request.url()));
|
2021-08-05 18:49:02 +03:00
|
|
|
let routeCalls = 0;
|
2021-07-12 13:07:47 +03:00
|
|
|
await page.route('**/*', async route => {
|
2021-08-05 18:49:02 +03:00
|
|
|
++routeCalls;
|
2021-07-12 13:07:47 +03:00
|
|
|
// @ts-expect-error
|
|
|
|
await route._intercept({});
|
|
|
|
await route.fulfill({
|
|
|
|
status: 201,
|
|
|
|
headers: {
|
|
|
|
foo: 'bar'
|
|
|
|
},
|
|
|
|
contentType: 'text/plain',
|
|
|
|
body: 'Yo, page!'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
const response = await page.goto(server.PREFIX + '/redirect/1.html');
|
|
|
|
expect(requestUrls).toEqual(expectedUrls);
|
|
|
|
expect(responseUrls).toEqual(expectedUrls);
|
|
|
|
await response.finished();
|
|
|
|
expect(requestFinishedUrls).toEqual(expectedUrls);
|
2021-08-05 18:49:02 +03:00
|
|
|
expect(routeCalls).toBe(1);
|
2021-07-12 13:07:47 +03:00
|
|
|
|
|
|
|
const redirectChain = [];
|
|
|
|
for (let req = response.request(); req; req = req.redirectedFrom())
|
|
|
|
redirectChain.unshift(req.url());
|
|
|
|
expect(redirectChain).toEqual(expectedUrls);
|
|
|
|
|
|
|
|
expect(response.status()).toBe(201);
|
|
|
|
expect(response.headers().foo).toBe('bar');
|
|
|
|
expect(response.headers()['content-type']).toBe('text/plain');
|
|
|
|
expect(await response.text()).toBe('Yo, page!');
|
|
|
|
});
|
2021-08-05 18:49:02 +03:00
|
|
|
|
|
|
|
it('should fulfill original response after redirects', async ({page, browserName, server}) => {
|
|
|
|
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);
|
|
|
|
const requestUrls = [];
|
|
|
|
const responseUrls = [];
|
|
|
|
const requestFinishedUrls = [];
|
|
|
|
page.on('request', request => requestUrls.push(request.url()));
|
|
|
|
page.on('response', response => responseUrls.push(response.url()));
|
|
|
|
page.on('requestfinished', request => requestFinishedUrls.push(request.url()));
|
|
|
|
let routeCalls = 0;
|
|
|
|
await page.route('**/*', async route => {
|
|
|
|
++routeCalls;
|
|
|
|
// @ts-expect-error
|
|
|
|
await route._intercept({});
|
|
|
|
await route.fulfill();
|
|
|
|
});
|
|
|
|
const response = await page.goto(server.PREFIX + '/redirect/1.html');
|
|
|
|
expect(requestUrls).toEqual(expectedUrls);
|
|
|
|
expect(responseUrls).toEqual(expectedUrls);
|
|
|
|
await response.finished();
|
|
|
|
expect(requestFinishedUrls).toEqual(expectedUrls);
|
|
|
|
expect(routeCalls).toBe(1);
|
|
|
|
|
|
|
|
const redirectChain = [];
|
|
|
|
for (let req = response.request(); req; req = req.redirectedFrom())
|
|
|
|
redirectChain.unshift(req.url());
|
|
|
|
expect(redirectChain).toEqual(expectedUrls);
|
|
|
|
|
|
|
|
expect(response.status()).toBe(200);
|
2021-08-05 18:59:48 +03:00
|
|
|
expect(await response.text()).toBe('<title>Woof-Woof</title>' + os.EOL);
|
2021-08-05 18:49:02 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should abort after redirects', async ({page, browserName, server}) => {
|
|
|
|
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);
|
|
|
|
const requestUrls = [];
|
|
|
|
const responseUrls = [];
|
|
|
|
const requestFinishedUrls = [];
|
|
|
|
const requestFailedUrls = [];
|
|
|
|
page.on('request', request => requestUrls.push(request.url()));
|
|
|
|
page.on('response', response => responseUrls.push(response.url()));
|
|
|
|
page.on('requestfinished', request => requestFinishedUrls.push(request.url()));
|
|
|
|
page.on('requestfailed', request => requestFailedUrls.push(request.url()));
|
|
|
|
let routeCalls = 0;
|
|
|
|
await page.route('**/*', async route => {
|
|
|
|
++routeCalls;
|
|
|
|
// @ts-expect-error
|
|
|
|
await route._intercept({});
|
|
|
|
await route.abort('connectionreset');
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
await page.goto(server.PREFIX + '/redirect/1.html');
|
|
|
|
} catch (e) {
|
2021-08-11 21:14:50 +03:00
|
|
|
if (browserName === 'webkit')
|
|
|
|
expect(e.message).toContain('Request intercepted');
|
2021-08-11 22:48:30 +03:00
|
|
|
else if (browserName === 'chromium')
|
2021-08-11 21:14:50 +03:00
|
|
|
expect(e.message).toContain('ERR_CONNECTION_RESET');
|
2021-08-11 22:48:30 +03:00
|
|
|
else
|
|
|
|
expect(e.message).toContain('NS_ERROR_NET_RESET');
|
2021-08-05 18:49:02 +03:00
|
|
|
}
|
|
|
|
expect(requestUrls).toEqual(expectedUrls);
|
|
|
|
expect(responseUrls).toEqual(expectedUrls.slice(0, -1));
|
|
|
|
expect(requestFinishedUrls).toEqual(expectedUrls.slice(0, -1));
|
|
|
|
expect(requestFailedUrls).toEqual(expectedUrls.slice(-1));
|
|
|
|
expect(routeCalls).toBe(1);
|
|
|
|
});
|