Skip to content

Commit f7a5c8f

Browse files
fix: only append 'Elastic/Synthetics' to default UA (#514)
* Only append 'Elastic/Synthetics' to default UA When we originally implemented #232 it appears we didn't implement the third AC, allowing users to override our custom UA string "Elastic/Synthetics" which is appended. Some users need to fully replace the UA, this allows them to do that. Rather than adding a boolean, this assumes that if you're overriding the UA you want full control, which is more in line with the principle of least surprise. * fix tests and rebase Co-authored-by: vigneshshanmugam <[email protected]>
1 parent a4c10b5 commit f7a5c8f

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

__tests__/core/gatherer.test.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ describe('Gatherer', () => {
117117
});
118118

119119
describe('Elastic UA identifier', () => {
120-
it('works on a single page', async () => {
120+
it('present when UA is not overriden', async () => {
121121
const driver = await Gatherer.setupDriver({ wsEndpoint });
122122
expect(await driver.page.evaluate(() => navigator.userAgent)).toContain(
123123
' Elastic/Synthetics'
@@ -126,29 +126,26 @@ describe('Gatherer', () => {
126126
await Gatherer.stop();
127127
});
128128

129-
it('works with device emulation', async () => {
129+
it('not present when UA is modified - emulated', async () => {
130130
const driver = await Gatherer.setupDriver({
131131
wsEndpoint,
132132
playwrightOptions: { ...devices['iPhone 12 Pro Max'] },
133133
});
134134
const userAgent = await driver.page.evaluate(() => navigator.userAgent);
135-
expect(userAgent).toContain('Elastic/Synthetics');
135+
expect(userAgent).not.toContain('Elastic/Synthetics');
136136
expect(userAgent).toContain('iPhone');
137137
await Gatherer.dispose(driver);
138138
await Gatherer.stop();
139139
});
140140

141-
it('works with popup window', async () => {
141+
it('present on popup windows', async () => {
142142
const driver = await Gatherer.setupDriver({
143143
wsEndpoint,
144-
playwrightOptions: { ...devices['Galaxy S9+'] },
145144
});
146145
const { page, context } = driver;
147146
await page.goto(server.TEST_PAGE);
148147
context.on('request', request => {
149-
expect(request.headers()['user-agent']).toContain(
150-
' Elastic/Synthetics'
151-
);
148+
expect(request.headers()['user-agent']).toContain('Elastic/Synthetics');
152149
});
153150
await page.setContent(
154151
'<a target=_blank rel=noopener href="/popup.html">popup</a>'
@@ -159,7 +156,7 @@ describe('Gatherer', () => {
159156
]);
160157
await page1.waitForLoadState();
161158
expect(await page1.evaluate(() => navigator.userAgent)).toContain(
162-
' Elastic/Synthetics'
159+
'Elastic/Synthetics'
163160
);
164161
expect(await page1.textContent('body')).toEqual('Not found');
165162
await Gatherer.dispose(driver);

src/core/gatherer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ export class Gatherer {
6565
}
6666

6767
static async getUserAgent(userAgent?: string) {
68-
const syntheticsIdentifier = ' Elastic/Synthetics';
6968
if (!userAgent) {
7069
const session = await Gatherer.browser.newBrowserCDPSession();
7170
({ userAgent } = await session.send('Browser.getVersion'));
71+
return userAgent + ' Elastic/Synthetics';
7272
}
73-
return userAgent + syntheticsIdentifier;
73+
return userAgent;
7474
}
7575

7676
static setNetworkConditions(

0 commit comments

Comments
 (0)