feat(user-agent): Adding User-Agent in headers while making connection to browser (#6813)
This commit is contained in:
parent
17b6f06b98
commit
254ec155eb
|
|
@ -25,7 +25,7 @@ import { Events } from './events';
|
||||||
import { TimeoutSettings } from '../utils/timeoutSettings';
|
import { TimeoutSettings } from '../utils/timeoutSettings';
|
||||||
import { ChildProcess } from 'child_process';
|
import { ChildProcess } from 'child_process';
|
||||||
import { envObjectToArray } from './clientHelper';
|
import { envObjectToArray } from './clientHelper';
|
||||||
import { assert, headersObjectToArray, makeWaitForNextTask } from '../utils/utils';
|
import { assert, headersObjectToArray, makeWaitForNextTask, getUserAgent } from '../utils/utils';
|
||||||
import { kBrowserClosedError } from '../utils/errors';
|
import { kBrowserClosedError } from '../utils/errors';
|
||||||
import * as api from '../../types/types';
|
import * as api from '../../types/types';
|
||||||
import type { Playwright } from './playwright';
|
import type { Playwright } from './playwright';
|
||||||
|
|
@ -109,12 +109,13 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel, chann
|
||||||
|
|
||||||
async connect(params: ConnectOptions): Promise<Browser> {
|
async connect(params: ConnectOptions): Promise<Browser> {
|
||||||
const logger = params.logger;
|
const logger = params.logger;
|
||||||
|
const paramsHeaders = Object.assign({'User-Agent': getUserAgent()}, params.headers);
|
||||||
return this._wrapApiCall('browserType.connect', async () => {
|
return this._wrapApiCall('browserType.connect', async () => {
|
||||||
const ws = new WebSocket(params.wsEndpoint, [], {
|
const ws = new WebSocket(params.wsEndpoint, [], {
|
||||||
perMessageDeflate: false,
|
perMessageDeflate: false,
|
||||||
maxPayload: 256 * 1024 * 1024, // 256Mb,
|
maxPayload: 256 * 1024 * 1024, // 256Mb,
|
||||||
handshakeTimeout: this._timeoutSettings.timeout(params),
|
handshakeTimeout: this._timeoutSettings.timeout(params),
|
||||||
headers: params.headers,
|
headers: paramsHeaders,
|
||||||
});
|
});
|
||||||
const connection = new Connection(() => ws.close());
|
const connection = new Connection(() => ws.close());
|
||||||
|
|
||||||
|
|
@ -224,7 +225,8 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel, chann
|
||||||
throw new Error('Connecting over CDP is only supported in Chromium.');
|
throw new Error('Connecting over CDP is only supported in Chromium.');
|
||||||
const logger = params.logger;
|
const logger = params.logger;
|
||||||
return this._wrapApiCall('browserType.connectOverCDP', async (channel: channels.BrowserTypeChannel) => {
|
return this._wrapApiCall('browserType.connectOverCDP', async (channel: channels.BrowserTypeChannel) => {
|
||||||
const headers = params.headers ? headersObjectToArray(params.headers) : undefined;
|
const paramsHeaders = Object.assign({'User-Agent': getUserAgent()}, params.headers);
|
||||||
|
const headers = paramsHeaders ? headersObjectToArray(paramsHeaders) : undefined;
|
||||||
const result = await channel.connectOverCDP({
|
const result = await channel.connectOverCDP({
|
||||||
sdkLanguage: 'javascript',
|
sdkLanguage: 'javascript',
|
||||||
endpointURL: 'endpointURL' in params ? params.endpointURL : params.wsEndpoint,
|
endpointURL: 'endpointURL' in params ? params.endpointURL : params.wsEndpoint,
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ import fs from 'fs';
|
||||||
import removeFolder from 'rimraf';
|
import removeFolder from 'rimraf';
|
||||||
import * as util from 'util';
|
import * as util from 'util';
|
||||||
import * as crypto from 'crypto';
|
import * as crypto from 'crypto';
|
||||||
|
import os from 'os';
|
||||||
import { spawn } from 'child_process';
|
import { spawn } from 'child_process';
|
||||||
|
|
||||||
const mkdirAsync = util.promisify(fs.mkdir.bind(fs));
|
const mkdirAsync = util.promisify(fs.mkdir.bind(fs));
|
||||||
|
|
@ -203,3 +204,8 @@ const localIpAddresses = [
|
||||||
export function isLocalIpAddress(ipAdress: string): boolean {
|
export function isLocalIpAddress(ipAdress: string): boolean {
|
||||||
return localIpAddresses.includes(ipAdress);
|
return localIpAddresses.includes(ipAdress);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getUserAgent() {
|
||||||
|
const packageJson = require('./../../package.json');
|
||||||
|
return `Playwright/${packageJson.version} (${os.arch()}/${os.platform()}/${os.release()})`;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@
|
||||||
import { playwrightTest as test, expect } from './config/browserTest';
|
import { playwrightTest as test, expect } from './config/browserTest';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
|
import { getUserAgent } from '../lib/utils/utils';
|
||||||
|
|
||||||
test.slow(true, 'All connect tests are slow');
|
test.slow(true, 'All connect tests are slow');
|
||||||
|
|
||||||
|
|
@ -86,6 +87,21 @@ test('should send extra headers with connect request', async ({browserType, star
|
||||||
expect(request.headers['foo']).toBe('bar');
|
expect(request.headers['foo']).toBe('bar');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should send default User-Agent header with connect request', async ({browserType, startRemoteServer, server}) => {
|
||||||
|
const [request] = await Promise.all([
|
||||||
|
server.waitForWebSocketConnectionRequest(),
|
||||||
|
browserType.connect({
|
||||||
|
wsEndpoint: `ws://localhost:${server.PORT}/ws`,
|
||||||
|
headers: {
|
||||||
|
'foo': 'bar',
|
||||||
|
},
|
||||||
|
timeout: 100,
|
||||||
|
}).catch(() => {})
|
||||||
|
]);
|
||||||
|
expect(request.headers['user-agent']).toBe(getUserAgent());
|
||||||
|
expect(request.headers['foo']).toBe('bar');
|
||||||
|
});
|
||||||
|
|
||||||
test('disconnected event should be emitted when browser is closed or server is closed', async ({browserType, startRemoteServer}) => {
|
test('disconnected event should be emitted when browser is closed or server is closed', async ({browserType, startRemoteServer}) => {
|
||||||
const remoteServer = await startRemoteServer();
|
const remoteServer = await startRemoteServer();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@
|
||||||
import { contextTest as test, expect } from '../config/browserTest';
|
import { contextTest as test, expect } from '../config/browserTest';
|
||||||
import { playwrightTest } from '../config/browserTest';
|
import { playwrightTest } from '../config/browserTest';
|
||||||
import http from 'http';
|
import http from 'http';
|
||||||
|
import { getUserAgent } from '../../lib/utils/utils';
|
||||||
|
|
||||||
test('should create a worker from a service worker', async ({page, server}) => {
|
test('should create a worker from a service worker', async ({page, server}) => {
|
||||||
const [worker] = await Promise.all([
|
const [worker] = await Promise.all([
|
||||||
|
|
@ -241,6 +242,23 @@ playwrightTest('should send extra headers with connect request', async ({browser
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
playwrightTest('should send default User-Agent header with connect request', async ({browserType, browserOptions, server}, testInfo) => {
|
||||||
|
{
|
||||||
|
const [request] = await Promise.all([
|
||||||
|
server.waitForWebSocketConnectionRequest(),
|
||||||
|
browserType.connectOverCDP({
|
||||||
|
wsEndpoint: `ws://localhost:${server.PORT}/ws`,
|
||||||
|
headers: {
|
||||||
|
'foo': 'bar',
|
||||||
|
},
|
||||||
|
timeout: 100,
|
||||||
|
}).catch(() => {})
|
||||||
|
]);
|
||||||
|
expect(request.headers['user-agent']).toBe(getUserAgent());
|
||||||
|
expect(request.headers['foo']).toBe('bar');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
playwrightTest('should report all pages in an existing browser', async ({ browserType, browserOptions }, testInfo) => {
|
playwrightTest('should report all pages in an existing browser', async ({ browserType, browserOptions }, testInfo) => {
|
||||||
const port = 9339 + testInfo.workerIndex;
|
const port = 9339 + testInfo.workerIndex;
|
||||||
const browserServer = await browserType.launch({
|
const browserServer = await browserType.launch({
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue