Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update useragent string with npm version #13903

Merged
merged 9 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .github/integ-config/integ-all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -701,14 +701,14 @@ tests:
browser: *minimal_browser_list

# GEO
# - test_name: integ_react_geo_display_map
# desc: 'Display Map'
# framework: react
# category: geo
# sample_name: [display-map]
# spec: display-map
# # Temp fix:
# browser: [chrome]
- test_name: integ_react_geo_display_map
desc: 'Display Map'
framework: react
category: geo
sample_name: [display-map]
spec: display-map
# Temp fix:
browser: [chrome]
- test_name: integ_react_geo_search_outside_map
desc: 'Search Outside Map'
framework: react
Expand Down
31 changes: 26 additions & 5 deletions packages/core/__tests__/Platform/userAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ import {
Platform,
getAmplifyUserAgent,
getAmplifyUserAgentObject,
sanitizeAmplifyVersion,
} from '../../src/Platform';
import { version } from '../../src/Platform/version';
import { AuthAction, Category, Framework } from '../../src/Platform/types';
import {
clearCache,
detectFramework,
} from '../../src/Platform/detectFramework';
import * as detection from '../../src/Platform/detection';
import { getCustomUserAgent } from '../../src/Platform/customUserAgent';
import { version } from '../../src/Platform/version';

jest.mock('../../src/Platform/customUserAgent');
const expectedVersion = version.replace(/\+.*/, '');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: For testing purposes this could be a some hardcoded strings

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was going to do that but then we will have to mock the version in first place so for now kept the same logic

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could push the version replacement into a function and then test that function with a variety of inputs/outputs.


describe('Platform test', () => {
const mockGetCustomUserAgent = getCustomUserAgent as jest.Mock;
Expand All @@ -36,10 +38,29 @@ describe('Platform test', () => {
});
});

describe('sanitizeAmplifyVersion', () => {
test('happy case with no special char', () => {
expect(sanitizeAmplifyVersion('6.6.0')).toEqual('6.6.0');
});

test('happy case with no special char +', () => {
expect(
sanitizeAmplifyVersion('6.6.4-unstable.ffa8a24.0+ffa8a24'),
).toEqual('6.6.4-unstable.ffa8a24.0');
});
});

describe('getAmplifyUserAgentObject test', () => {
test('without customUserAgentDetails', () => {
expect(getAmplifyUserAgentObject()).toStrictEqual([
['aws-amplify', version],
['aws-amplify', expectedVersion],
['framework', Framework.WebUnknown],
]);
});

test('should remove value after special char + in version', () => {
expect(getAmplifyUserAgentObject()).toStrictEqual([
['aws-amplify', expectedVersion],
['framework', Framework.WebUnknown],
]);
});
Expand All @@ -51,7 +72,7 @@ describe('Platform test', () => {
action: AuthAction.ConfirmSignIn,
}),
).toStrictEqual([
['aws-amplify', version],
['aws-amplify', expectedVersion],
[Category.Auth, AuthAction.ConfirmSignIn],
['framework', Framework.WebUnknown],
]);
Expand All @@ -68,7 +89,7 @@ describe('Platform test', () => {
action: AuthAction.ConfirmSignIn,
}),
).toStrictEqual([
['aws-amplify', version],
['aws-amplify', expectedVersion],
[Category.Auth, AuthAction.ConfirmSignIn],
['framework', Framework.WebUnknown],
['uiversion', '1.0.0'],
Expand Down Expand Up @@ -124,7 +145,7 @@ describe('Platform test', () => {
});

describe('detectFramework observers', () => {
let module;
let module: any;

beforeAll(() => {
jest.resetModules();
Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/Platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ import { getCustomUserAgent } from './customUserAgent';

const BASE_USER_AGENT = `aws-amplify`;

/** Sanitize Amplify version string be removing special character + and character post the special character */
export const sanitizeAmplifyVersion = (amplifyVersion: string) =>
amplifyVersion.replace(/\+.*/, '');

class PlatformBuilder {
userAgent = `${BASE_USER_AGENT}/${version}`;
userAgent = `${BASE_USER_AGENT}/${sanitizeAmplifyVersion(version)}`;
get framework() {
return detectFramework();
}
Expand All @@ -34,7 +38,10 @@ export const getAmplifyUserAgentObject = ({
category,
action,
}: CustomUserAgentDetails = {}): AWSUserAgent => {
const userAgent: AWSUserAgent = [[BASE_USER_AGENT, version]];
const userAgent: AWSUserAgent = [
[BASE_USER_AGENT, sanitizeAmplifyVersion(version)],
];

if (category) {
userAgent.push([category, action]);
}
Expand Down
Loading
Loading