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-11-03 03:48:38 +03:00
|
|
|
import type { Route } from 'playwright-core';
|
2021-08-04 21:54:52 +03:00
|
|
|
import { expect, test as it } from './pageTest';
|
2021-11-13 11:29:51 +03:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2021-06-18 21:04:48 +03:00
|
|
|
|
2021-11-04 23:36:04 +03:00
|
|
|
it('should fulfill intercepted response', async ({ page, server, isElectron }) => {
|
|
|
|
it.fixme(isElectron, 'error: Browser context management is not supported.');
|
2021-06-18 21:04:48 +03:00
|
|
|
await page.route('**/*', async route => {
|
2021-11-03 03:48:38 +03:00
|
|
|
const response = await page.request.fetch(route.request());
|
2021-06-18 21:04:48 +03:00
|
|
|
await route.fulfill({
|
2021-11-03 03:48:38 +03:00
|
|
|
response,
|
2021-06-18 21:04:48 +03:00
|
|
|
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-09-27 19:58:08 +03:00
|
|
|
it('should fulfill response with empty body', async ({ page, server, browserName, browserMajorVersion }) => {
|
2021-08-19 17:44:13 +03:00
|
|
|
it.skip(browserName === 'chromium' && browserMajorVersion <= 91, 'Fails in Electron that uses old Chromium');
|
2021-08-12 00:47:05 +03:00
|
|
|
await page.route('**/*', async route => {
|
2021-11-03 03:48:38 +03:00
|
|
|
const response = await page.request.fetch(route.request());
|
2021-08-12 00:47:05 +03:00
|
|
|
await route.fulfill({
|
2021-08-27 01:44:20 +03:00
|
|
|
response,
|
2021-08-12 00:47:05 +03:00
|
|
|
status: 201,
|
|
|
|
body: ''
|
|
|
|
});
|
|
|
|
});
|
|
|
|
const response = await page.goto(server.PREFIX + '/title.html');
|
|
|
|
expect(response.status()).toBe(201);
|
|
|
|
expect(await response.text()).toBe('');
|
|
|
|
});
|
|
|
|
|
2021-11-04 23:36:04 +03:00
|
|
|
it('should override with defaults when intercepted response not provided', async ({ page, server, browserName, isElectron }) => {
|
|
|
|
it.fixme(isElectron, 'error: Browser context management is not supported.');
|
2021-08-24 21:07:54 +03:00
|
|
|
server.setRoute('/empty.html', (req, res) => {
|
|
|
|
res.setHeader('foo', 'bar');
|
|
|
|
res.end('my content');
|
|
|
|
});
|
|
|
|
await page.route('**/*', async route => {
|
2021-11-03 03:48:38 +03:00
|
|
|
await page.request.fetch(route.request());
|
2021-08-24 21:07:54 +03:00
|
|
|
await route.fulfill({
|
|
|
|
status: 201,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(response.status()).toBe(201);
|
|
|
|
expect(await response.text()).toBe('');
|
|
|
|
if (browserName === 'webkit')
|
2021-09-27 19:58:08 +03:00
|
|
|
expect(response.headers()).toEqual({ 'content-type': 'text/plain' });
|
2021-08-24 21:07:54 +03:00
|
|
|
else
|
|
|
|
expect(response.headers()).toEqual({ });
|
|
|
|
});
|
|
|
|
|
2021-11-04 23:36:04 +03:00
|
|
|
it('should fulfill with any response', async ({ page, server, isElectron }) => {
|
|
|
|
it.fixme(isElectron, 'error: Browser context management is not supported.');
|
2021-08-24 21:07:54 +03:00
|
|
|
|
|
|
|
server.setRoute('/sample', (req, res) => {
|
|
|
|
res.setHeader('foo', 'bar');
|
|
|
|
res.end('Woo-hoo');
|
|
|
|
});
|
2021-11-03 03:48:38 +03:00
|
|
|
const sampleResponse = await page.request.get(`${server.PREFIX}/sample`);
|
2021-08-24 21:07:54 +03:00
|
|
|
|
|
|
|
await page.route('**/*', async route => {
|
|
|
|
await route.fulfill({
|
2021-08-27 01:44:20 +03:00
|
|
|
response: sampleResponse,
|
2021-08-24 21:07:54 +03:00
|
|
|
status: 201,
|
2021-08-25 03:34:29 +03:00
|
|
|
contentType: 'text/plain'
|
2021-08-24 21:07:54 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
const response = await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect(response.status()).toBe(201);
|
|
|
|
expect(await response.text()).toBe('Woo-hoo');
|
|
|
|
expect(response.headers()['foo']).toBe('bar');
|
|
|
|
});
|
|
|
|
|
2021-11-04 23:36:04 +03:00
|
|
|
it('should support fulfill after intercept', async ({ page, server, isElectron }) => {
|
|
|
|
it.fixme(isElectron, 'error: Browser context management is not supported.');
|
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-11-03 03:48:38 +03:00
|
|
|
const response = await page.request.fetch(route.request());
|
2021-08-27 01:44:20 +03:00
|
|
|
await route.fulfill({ response });
|
2021-06-18 21:04:48 +03:00
|
|
|
});
|
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-11-13 11:29:51 +03:00
|
|
|
const original = await fs.promises.readFile(path.join(__dirname, '..', 'assets', 'title.html'), 'utf8');
|
|
|
|
expect(await response.text()).toBe(original);
|
2021-06-18 21:04:48 +03:00
|
|
|
});
|
|
|
|
|
2021-11-04 23:36:04 +03:00
|
|
|
it('should give access to the intercepted response', async ({ page, server, isElectron }) => {
|
|
|
|
it.fixme(isElectron, 'error: Browser context management is not supported.');
|
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-11-03 03:48:38 +03:00
|
|
|
const response = await page.request.fetch(route.request());
|
2021-06-18 21:04:48 +03:00
|
|
|
|
|
|
|
expect(response.status()).toBe(200);
|
2021-11-03 03:48:38 +03:00
|
|
|
expect(response.statusText()).toBe('OK');
|
2021-06-18 21:04:48 +03:00
|
|
|
expect(response.ok()).toBeTruthy();
|
|
|
|
expect(response.url()).toBe(server.PREFIX + '/title.html');
|
|
|
|
expect(response.headers()['content-type']).toBe('text/html; charset=utf-8');
|
2021-09-11 23:27:00 +03:00
|
|
|
expect(await (await response.headersArray()).filter(({ name }) => name.toLowerCase() === 'content-type')).toEqual([{ name: 'Content-Type', value: 'text/html; charset=utf-8' }]);
|
2021-06-18 21:04:48 +03:00
|
|
|
|
2021-08-27 01:44:20 +03:00
|
|
|
await Promise.all([route.fulfill({ response }), evalPromise]);
|
2021-06-18 21:04:48 +03:00
|
|
|
});
|
|
|
|
|
2021-11-04 23:36:04 +03:00
|
|
|
it('should give access to the intercepted response body', async ({ page, server, isElectron }) => {
|
|
|
|
it.fixme(isElectron, 'error: Browser context management is not supported.');
|
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);
|
|
|
|
|
2021-10-27 18:28:53 +03:00
|
|
|
const evalPromise = page.evaluate(url => fetch(url), server.PREFIX + '/simple.json').catch(() => {});
|
2021-06-18 21:04:48 +03:00
|
|
|
|
|
|
|
const route = await routePromise;
|
2021-11-03 03:48:38 +03:00
|
|
|
const response = await page.request.fetch(route.request());
|
2021-06-18 21:04:48 +03:00
|
|
|
|
|
|
|
expect((await response.text())).toBe('{"foo": "bar"}\n');
|
|
|
|
|
2021-08-27 01:44:20 +03:00
|
|
|
await Promise.all([route.fulfill({ response }), evalPromise]);
|
2021-06-18 21:04:48 +03:00
|
|
|
});
|