Skip to content

Commit 45240e6

Browse files
authored
test(middleware-sdk-transcribe-streaming): extensive tests for signer.ts (#1343)
1 parent db2651c commit 45240e6

File tree

1 file changed

+125
-35
lines changed

1 file changed

+125
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,132 @@
11
import { HttpRequest } from "@aws-sdk/protocol-http";
22
import { SignatureV4 } from "./signer";
3-
describe("transcribe streaming", () => {
4-
describe("WebSocket request signer", () => {
5-
it("should invoke base SigV4 signer correctly", async () => {
6-
expect.assertions(5);
7-
const toSign = new HttpRequest({
8-
headers: {
9-
"x-amz-foo": "foo",
10-
bar: "bar",
11-
"amz-sdk-invocation-id": "123",
12-
"amz-sdk-request": "attempt=1",
13-
host: "aws.amazon.com"
14-
},
15-
body: "hello world",
16-
query: {
17-
prop1: "A",
18-
prop2: "B"
19-
}
3+
import {
4+
RequestSigningArguments,
5+
RequestPresigningArguments
6+
} from "@aws-sdk/types";
7+
8+
jest.mock("@aws-sdk/protocol-http");
9+
10+
describe("signer", () => {
11+
const mockPresignedRequest = { req: "mockPresignedRequest" };
12+
const mockSignedRequest = { reg: "mockSignedRequest" };
13+
14+
const presign = jest.fn().mockResolvedValue(mockPresignedRequest);
15+
const sign = jest.fn().mockResolvedValue(mockSignedRequest);
16+
17+
const headers = {
18+
"x-amz-foo": "foo",
19+
bar: "bar",
20+
host: "aws.amazon.com"
21+
};
22+
const body = "body";
23+
const method = "method";
24+
25+
const request = { headers, body, method };
26+
const sigV4 = new SignatureV4({ signer: { sign, presign } as any });
27+
28+
afterEach(() => {
29+
jest.clearAllMocks();
30+
});
31+
32+
describe("presign", () => {
33+
const expectPresignArgs = (
34+
result: any,
35+
options: RequestPresigningArguments = {}
36+
) => {
37+
expect(result).toStrictEqual(mockPresignedRequest);
38+
expect(sign).not.toHaveBeenCalled();
39+
expect(presign).toHaveBeenCalledTimes(1);
40+
expect(presign).toHaveBeenCalledWith(request, options);
41+
};
42+
43+
it("without options", async () => {
44+
const result = await sigV4.presign(request as any);
45+
expectPresignArgs(result);
46+
});
47+
48+
it("with options", async () => {
49+
const options = {
50+
expiresIn: 300
51+
};
52+
const result = await sigV4.presign(request as any, options);
53+
expectPresignArgs(result, options);
54+
});
55+
});
56+
57+
describe("sign", () => {
58+
describe("calls presign when HttpRequest.isInstance returns true", () => {
59+
const { isInstance } = HttpRequest;
60+
61+
beforeEach(() => {
62+
((isInstance as unknown) as jest.Mock).mockReturnValueOnce(true);
63+
});
64+
65+
const expectSignArgs = (result: any) => {
66+
expect(result).toStrictEqual({
67+
...mockPresignedRequest,
68+
body: request.body
69+
});
70+
expect(isInstance).toHaveBeenCalledTimes(1);
71+
expect(isInstance).toHaveBeenCalledWith(request);
72+
expect(presign).toHaveBeenCalledTimes(1);
73+
expect(presign).toHaveBeenCalledWith(
74+
{ ...request, body: "" },
75+
{
76+
expiresIn: 300,
77+
unsignableHeaders: new Set(
78+
Object.keys(request.headers).filter(header => header !== "host")
79+
)
80+
}
81+
);
82+
expect(sign).not.toHaveBeenCalled();
83+
};
84+
85+
it("without options", async () => {
86+
const result = await sigV4.sign(request as any);
87+
expectSignArgs(result);
88+
});
89+
90+
it("with options", async () => {
91+
const options = {
92+
unsignableHeaders: new Set(Object.keys(headers))
93+
};
94+
const result = await sigV4.sign(request as any, options);
95+
expectSignArgs(result);
96+
});
97+
});
98+
99+
describe("calls sign when HttpRequest.isInstance returns false", () => {
100+
const { isInstance } = HttpRequest;
101+
102+
beforeEach(() => {
103+
((isInstance as unknown) as jest.Mock).mockReturnValueOnce(false);
20104
});
21-
const mockBaseSigner = {
22-
presign: jest
23-
.fn()
24-
.mockImplementation(request => Promise.resolve(request))
105+
106+
const expectSignArgs = (
107+
result: any,
108+
options?: RequestSigningArguments
109+
) => {
110+
expect(result).toStrictEqual(mockSignedRequest);
111+
expect(isInstance).toHaveBeenCalledTimes(1);
112+
expect(isInstance).toHaveBeenCalledWith(request);
113+
expect(sign).toHaveBeenCalledTimes(1);
114+
expect(sign).toHaveBeenCalledWith(request, options);
115+
expect(presign).not.toHaveBeenCalled();
25116
};
26-
const signer = new SignatureV4({ signer: mockBaseSigner as any });
27-
const signed = await signer.sign(toSign);
28-
expect(toSign).toMatchObject(signed);
29-
expect(mockBaseSigner.presign).toBeCalled();
30-
// The request's body should not be presigned
31-
expect(mockBaseSigner.presign.mock.calls[0][0].body).toEqual("");
32-
expect(
33-
mockBaseSigner.presign.mock.calls[0][1]!.unsignableHeaders
34-
).toBeDefined();
35-
const unsignableHeaders: Set<string> = mockBaseSigner.presign.mock
36-
.calls[0][1]!.unsignableHeaders;
37-
expect([...unsignableHeaders.entries()].map(([value]) => value)).toEqual(
38-
Object.keys(toSign.headers).filter(a => a !== "host")
39-
);
117+
118+
it("without options", async () => {
119+
const result = await sigV4.sign(request as any);
120+
expectSignArgs(result);
121+
});
122+
123+
it("with options", async () => {
124+
const options = {
125+
unsignableHeaders: new Set(Object.keys(headers))
126+
};
127+
const result = await sigV4.sign(request as any, options);
128+
expectSignArgs(result, options);
129+
});
40130
});
41131
});
42132
});

0 commit comments

Comments
 (0)