Skip to content

Commit 4e6fa23

Browse files
committed
test: resolveAuthSchemes.spec.ts
1 parent a61d087 commit 4e6fa23

File tree

2 files changed

+57
-14
lines changed

2 files changed

+57
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { describe, it, expect } from "vitest";
2+
import { HttpAuthScheme } from "@smithy/types";
3+
import { resolveAuthSchemes } from "./resolveAuthSchemes";
4+
5+
describe("resolveAuthSchemes", () => {
6+
const sigv4 = "sigv4";
7+
const sigv4a = "sigv4a";
8+
9+
const mockSigV4AuthScheme = { schemeId: `aws.auth#${sigv4}` } as HttpAuthScheme;
10+
const mockSigV4aAuthScheme = { schemeId: `aws.auth#${sigv4a}` } as HttpAuthScheme;
11+
12+
it("should return candidate auth schemes is preference list is not available", () => {
13+
const candidateAuthSchemes = [mockSigV4AuthScheme, mockSigV4aAuthScheme];
14+
expect(resolveAuthSchemes(candidateAuthSchemes, [])).toEqual(candidateAuthSchemes);
15+
16+
// @ts-expect-error case where callee incorrectly passes undefined
17+
expect(resolveAuthSchemes(candidateAuthSchemes)).toEqual(candidateAuthSchemes);
18+
});
19+
20+
it("should return auth scheme from preference if it's available", () => {
21+
expect(resolveAuthSchemes([mockSigV4AuthScheme, mockSigV4aAuthScheme], [sigv4a])).toEqual([
22+
mockSigV4aAuthScheme,
23+
mockSigV4AuthScheme,
24+
]);
25+
26+
expect(resolveAuthSchemes([mockSigV4AuthScheme, mockSigV4aAuthScheme], [sigv4a, sigv4])).toEqual([
27+
mockSigV4aAuthScheme,
28+
mockSigV4AuthScheme,
29+
]);
30+
31+
expect(resolveAuthSchemes([mockSigV4AuthScheme, mockSigV4aAuthScheme], [sigv4, sigv4a])).toEqual([
32+
mockSigV4AuthScheme,
33+
mockSigV4aAuthScheme,
34+
]);
35+
});
36+
37+
it("should ignore auth scheme from preference if it's not available", () => {
38+
expect(resolveAuthSchemes([mockSigV4AuthScheme], [sigv4a])).toEqual([mockSigV4AuthScheme]);
39+
expect(resolveAuthSchemes([mockSigV4AuthScheme], ["sigv3"])).toEqual([mockSigV4AuthScheme]);
40+
});
41+
});

packages/core/src/middleware-http-auth-scheme/resolveAuthSchemes.ts

+16-14
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,27 @@ import { HttpAuthScheme } from "@smithy/types";
1010
*/
1111
export const resolveAuthSchemes = (candidateAuthSchemes: HttpAuthScheme[], authSchemePreference: string[]) => {
1212
if (!authSchemePreference || authSchemePreference.length === 0) {
13-
// reprioritize candidates based on user's preference
14-
const preferredAuthSchemes = [];
13+
return candidateAuthSchemes;
14+
}
1515

16-
for (const preferredSchemeName of authSchemePreference) {
17-
for (const candidateAuthScheme of candidateAuthSchemes) {
18-
const candidateAuthSchemeName = candidateAuthScheme.schemeId.split("#")[1];
19-
if (candidateAuthSchemeName === preferredSchemeName) {
20-
preferredAuthSchemes.push(candidateAuthScheme);
21-
}
22-
}
23-
}
16+
// reprioritize candidates based on user's preference
17+
const preferredAuthSchemes = [];
2418

25-
// add any remaining candidates that weren't in the preference list
19+
for (const preferredSchemeName of authSchemePreference) {
2620
for (const candidateAuthScheme of candidateAuthSchemes) {
27-
if (!preferredAuthSchemes.find(({ schemeId }) => schemeId === candidateAuthScheme.schemeId)) {
21+
const candidateAuthSchemeName = candidateAuthScheme.schemeId.split("#")[1];
22+
if (candidateAuthSchemeName === preferredSchemeName) {
2823
preferredAuthSchemes.push(candidateAuthScheme);
2924
}
3025
}
31-
} else {
32-
return candidateAuthSchemes;
3326
}
27+
28+
// add any remaining candidates that weren't in the preference list
29+
for (const candidateAuthScheme of candidateAuthSchemes) {
30+
if (!preferredAuthSchemes.find(({ schemeId }) => schemeId === candidateAuthScheme.schemeId)) {
31+
preferredAuthSchemes.push(candidateAuthScheme);
32+
}
33+
}
34+
35+
return preferredAuthSchemes;
3436
};

0 commit comments

Comments
 (0)