Skip to content

Commit 8212881

Browse files
committed
test(util-user-agent-node): refactor unit test
1 parent db3f5c3 commit 8212881

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

packages/util-user-agent-node/src/index.spec.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,17 @@ jest.mock("@aws-sdk/node-config-provider", () => ({
1616
loadConfig: () => mockAppIdLoader,
1717
}));
1818

19+
import { UserAgent } from "@aws-sdk/types";
20+
1921
import { defaultUserAgent } from ".";
2022

23+
const validateUserAgent = (userAgent: UserAgent, expected: UserAgent) => {
24+
expect(userAgent.length).toBe(expected.length);
25+
for (const pair of expected) {
26+
expect(userAgent).toContainEqual(pair);
27+
}
28+
};
29+
2130
describe("defaultUserAgent", () => {
2231
beforeEach(() => {
2332
jest.resetAllMocks();
@@ -27,40 +36,54 @@ describe("defaultUserAgent", () => {
2736
jest.clearAllMocks();
2837
});
2938

39+
const basicUserAgent: UserAgent = [
40+
["aws-sdk-js", "0.1.0"],
41+
["api/s3", "0.1.0"],
42+
["os/darwin", "19.6.0"],
43+
["lang/js"],
44+
["md/nodejs", "14.13.1"],
45+
];
46+
3047
it("should response basic node default user agent", async () => {
3148
const userAgent = await defaultUserAgent({ serviceId: "s3", clientVersion: "0.1.0" })();
32-
expect(userAgent).toContainEqual(["aws-sdk-js", "0.1.0"]);
33-
expect(userAgent).toContainEqual(["api/s3", "0.1.0"]);
34-
expect(userAgent).toContainEqual(["os/darwin", "19.6.0"]);
35-
expect(userAgent).toContainEqual(["lang/js"]);
49+
validateUserAgent(userAgent, basicUserAgent);
3650
});
3751

3852
it("should skip api version if service id is not supplied", async () => {
3953
const userAgent = await defaultUserAgent({ serviceId: undefined, clientVersion: "0.1.0" })();
40-
expect(userAgent).not.toContainEqual(["api/s3", "0.1.0"]);
54+
validateUserAgent(
55+
userAgent,
56+
basicUserAgent.filter((pair) => pair[0] !== "api/s3")
57+
);
4158
});
4259

4360
it("should add AWS_EXECUTION_ENV", async () => {
4461
//@ts-ignore mock environmental variables
4562
mockEnv.AWS_EXECUTION_ENV = "lambda";
4663
const userAgent = await defaultUserAgent({ serviceId: "s3", clientVersion: "0.1.0" })();
47-
expect(userAgent).toContainEqual(["exec-env/lambda"]);
64+
const expectedUserAgent: UserAgent = [...basicUserAgent, ["exec-env/lambda"]];
65+
validateUserAgent(userAgent, expectedUserAgent);
66+
//@ts-ignore mock environmental variables
67+
delete mockEnv.AWS_EXECUTION_ENV;
4868
});
4969

5070
it("should load app id if available", async () => {
5171
mockAppIdLoader.mockClear();
5272
const appId = "appId12345";
5373
mockAppIdLoader.mockResolvedValue(appId);
5474
const userAgent = await defaultUserAgent({ serviceId: "s3", clientVersion: "0.1.0" })();
55-
expect(userAgent).toContainEqual([`app/${appId}`]);
75+
const expectedUserAgent: UserAgent = [...basicUserAgent, [`app/${appId}`]];
76+
validateUserAgent(userAgent, expectedUserAgent);
5677
});
5778

5879
it("should memoize app id", async () => {
5980
mockAppIdLoader.mockClear();
6081
const appId = "appId12345";
6182
mockAppIdLoader.mockResolvedValue(appId);
6283
const userAgentProvider = defaultUserAgent({ serviceId: "s3", clientVersion: "0.1.0" });
63-
expect(await userAgentProvider()).toEqual(await userAgentProvider());
84+
const expectedUserAgent: UserAgent = [...basicUserAgent, [`app/${appId}`]];
85+
validateUserAgent(await userAgentProvider(), expectedUserAgent);
86+
validateUserAgent(await userAgentProvider(), expectedUserAgent);
6487
expect(mockAppIdLoader).toBeCalledTimes(1);
6588
});
6689
});

packages/util-user-agent-node/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ export const defaultUserAgent = ({ serviceId, clientVersion }: DefaultUserAgentO
4646
let resolvedUserAgent: UserAgent | undefined = undefined;
4747
return async () => {
4848
if (!resolvedUserAgent) {
49-
resolvedUserAgent = [...sections, [`app/${await appIdPromise}`]];
49+
const appId = await appIdPromise;
50+
resolvedUserAgent = appId ? [...sections, [`app/${appId}`]] : [...sections];
5051
}
5152
return resolvedUserAgent;
5253
};

0 commit comments

Comments
 (0)