From 64657c3b657b936fc1a3bca113e9c81587462152 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Tue, 28 Sep 2021 13:01:35 -0700 Subject: [PATCH] feat(fetch): send Playwright as default user-agent for global fetch (#9195) --- src/server/fetch.ts | 4 ++-- tests/global-fetch.spec.ts | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/server/fetch.ts b/src/server/fetch.ts index 23824dd6ba..471358a7db 100644 --- a/src/server/fetch.ts +++ b/src/server/fetch.ts @@ -23,7 +23,7 @@ import zlib from 'zlib'; import { HTTPCredentials } from '../../types/types'; import { NameValue, NewRequestOptions } from '../common/types'; import { TimeoutSettings } from '../utils/timeoutSettings'; -import { createGuid, isFilePayload, monotonicTime } from '../utils/utils'; +import { createGuid, getPlaywrightVersion, isFilePayload, monotonicTime } from '../utils/utils'; import { BrowserContext } from './browserContext'; import { MultipartFormData } from './formData'; import { SdkObject } from './instrumentation'; @@ -352,7 +352,7 @@ export class GlobalFetchRequest extends FetchRequest { } this._options = { baseURL: options.baseURL, - userAgent: options.userAgent || '', + userAgent: options.userAgent || `Playwright/${getPlaywrightVersion()}`, extraHTTPHeaders: options.extraHTTPHeaders, ignoreHTTPSErrors: !!options.ignoreHTTPSErrors, httpCredentials: options.httpCredentials, diff --git a/tests/global-fetch.spec.ts b/tests/global-fetch.spec.ts index 28ecc82c92..279958e3e0 100644 --- a/tests/global-fetch.spec.ts +++ b/tests/global-fetch.spec.ts @@ -15,6 +15,7 @@ */ import http from 'http'; +import { getPlaywrightVersion } from '../lib/utils/utils'; import { expect, playwrightTest as it } from './config/browserTest'; it.skip(({ mode }) => mode !== 'default'); @@ -143,3 +144,11 @@ it('should resolve url relative to gobal baseURL option', async ({ playwright, s expect(response.url()).toBe(server.EMPTY_PAGE); }); +it('should set playwright as user-agent', async ({ playwright, server }) => { + const request = await playwright._newRequest(); + const [serverRequest] = await Promise.all([ + server.waitForRequest('/empty.html'), + request.get(server.EMPTY_PAGE) + ]); + expect(serverRequest.headers['user-agent']).toBe('Playwright/' + getPlaywrightVersion()); +});