Skip to content

Commit 541e463

Browse files
authored
fix: Update useragent string with npm version (#13903)
* update yarn lock and test * Revert "fix: temporarily disable failing test to unblock pipeline" This reverts commit 5960e13. * remove char after + in userAgent version * update unit tests * revert integ test run * update unit tests * update lock
1 parent f648577 commit 541e463

File tree

4 files changed

+1666
-1740
lines changed

4 files changed

+1666
-1740
lines changed

.github/integ-config/integ-all.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -701,14 +701,14 @@ tests:
701701
browser: *minimal_browser_list
702702

703703
# GEO
704-
# - test_name: integ_react_geo_display_map
705-
# desc: 'Display Map'
706-
# framework: react
707-
# category: geo
708-
# sample_name: [display-map]
709-
# spec: display-map
710-
# # Temp fix:
711-
# browser: [chrome]
704+
- test_name: integ_react_geo_display_map
705+
desc: 'Display Map'
706+
framework: react
707+
category: geo
708+
sample_name: [display-map]
709+
spec: display-map
710+
# Temp fix:
711+
browser: [chrome]
712712
- test_name: integ_react_geo_search_outside_map
713713
desc: 'Search Outside Map'
714714
framework: react

packages/core/__tests__/Platform/userAgent.test.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ import {
22
Platform,
33
getAmplifyUserAgent,
44
getAmplifyUserAgentObject,
5+
sanitizeAmplifyVersion,
56
} from '../../src/Platform';
6-
import { version } from '../../src/Platform/version';
77
import { AuthAction, Category, Framework } from '../../src/Platform/types';
88
import {
99
clearCache,
1010
detectFramework,
1111
} from '../../src/Platform/detectFramework';
1212
import * as detection from '../../src/Platform/detection';
1313
import { getCustomUserAgent } from '../../src/Platform/customUserAgent';
14+
import { version } from '../../src/Platform/version';
1415

1516
jest.mock('../../src/Platform/customUserAgent');
17+
const expectedVersion = version.replace(/\+.*/, '');
1618

1719
describe('Platform test', () => {
1820
const mockGetCustomUserAgent = getCustomUserAgent as jest.Mock;
@@ -36,10 +38,29 @@ describe('Platform test', () => {
3638
});
3739
});
3840

41+
describe('sanitizeAmplifyVersion', () => {
42+
test('happy case with no special char', () => {
43+
expect(sanitizeAmplifyVersion('6.6.0')).toEqual('6.6.0');
44+
});
45+
46+
test('happy case with no special char +', () => {
47+
expect(
48+
sanitizeAmplifyVersion('6.6.4-unstable.ffa8a24.0+ffa8a24'),
49+
).toEqual('6.6.4-unstable.ffa8a24.0');
50+
});
51+
});
52+
3953
describe('getAmplifyUserAgentObject test', () => {
4054
test('without customUserAgentDetails', () => {
4155
expect(getAmplifyUserAgentObject()).toStrictEqual([
42-
['aws-amplify', version],
56+
['aws-amplify', expectedVersion],
57+
['framework', Framework.WebUnknown],
58+
]);
59+
});
60+
61+
test('should remove value after special char + in version', () => {
62+
expect(getAmplifyUserAgentObject()).toStrictEqual([
63+
['aws-amplify', expectedVersion],
4364
['framework', Framework.WebUnknown],
4465
]);
4566
});
@@ -51,7 +72,7 @@ describe('Platform test', () => {
5172
action: AuthAction.ConfirmSignIn,
5273
}),
5374
).toStrictEqual([
54-
['aws-amplify', version],
75+
['aws-amplify', expectedVersion],
5576
[Category.Auth, AuthAction.ConfirmSignIn],
5677
['framework', Framework.WebUnknown],
5778
]);
@@ -68,7 +89,7 @@ describe('Platform test', () => {
6889
action: AuthAction.ConfirmSignIn,
6990
}),
7091
).toStrictEqual([
71-
['aws-amplify', version],
92+
['aws-amplify', expectedVersion],
7293
[Category.Auth, AuthAction.ConfirmSignIn],
7394
['framework', Framework.WebUnknown],
7495
['uiversion', '1.0.0'],
@@ -124,7 +145,7 @@ describe('Platform test', () => {
124145
});
125146

126147
describe('detectFramework observers', () => {
127-
let module;
148+
let module: any;
128149

129150
beforeAll(() => {
130151
jest.resetModules();

packages/core/src/Platform/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ import { getCustomUserAgent } from './customUserAgent';
1010

1111
const BASE_USER_AGENT = `aws-amplify`;
1212

13+
/** Sanitize Amplify version string be removing special character + and character post the special character */
14+
export const sanitizeAmplifyVersion = (amplifyVersion: string) =>
15+
amplifyVersion.replace(/\+.*/, '');
16+
1317
class PlatformBuilder {
14-
userAgent = `${BASE_USER_AGENT}/${version}`;
18+
userAgent = `${BASE_USER_AGENT}/${sanitizeAmplifyVersion(version)}`;
1519
get framework() {
1620
return detectFramework();
1721
}
@@ -34,7 +38,10 @@ export const getAmplifyUserAgentObject = ({
3438
category,
3539
action,
3640
}: CustomUserAgentDetails = {}): AWSUserAgent => {
37-
const userAgent: AWSUserAgent = [[BASE_USER_AGENT, version]];
41+
const userAgent: AWSUserAgent = [
42+
[BASE_USER_AGENT, sanitizeAmplifyVersion(version)],
43+
];
44+
3845
if (category) {
3946
userAgent.push([category, action]);
4047
}

0 commit comments

Comments
 (0)