-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.integration.spec.ts
94 lines (81 loc) · 2.95 KB
/
index.integration.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { Credentials } from "aws-sdk"
jest.mock("aws-sdk", () => {
return {
config: {
credentialProvider: {
resolvePromise: () =>
({
accessKeyId: "accessKeyId",
secretAccessKey: "secretAccessKey",
sessionToken: "sessionToken",
} as Credentials),
},
},
}
})
import { install, uninstall, mockClient } from "mappersmith/test"
import forge from "mappersmith"
import { configAWSMiddleware } from "./"
const logger = ({ info: jest.fn() } as unknown) as Console
const AWSMiddleware = configAWSMiddleware({ region: "us-east-1", service: "execute-api", logger })
const api = forge({
clientId: "test",
host: "https://test.execute-api.us-east-1.amazonaws.com",
middleware: [AWSMiddleware],
resources: {
test: {
get: { path: "/test", method: "get" },
post: { path: "/test", method: "post" },
},
},
})
describe("AWSMiddleware", () => {
beforeEach(() => install())
afterEach(() => uninstall())
test("get", async () => {
mockClient(api).resource("test").method("get").response({ ok: true })
const response = await api.test.get()
// @ts-ignore
expect(response.originalRequest?.requestParams).toEqual({
headers: {
Authorization: expect.stringMatching(
/^AWS4-HMAC-SHA256 Credential=accessKeyId\/\d{8}\/us-east-1\/execute-api\/aws4_request, SignedHeaders=host, Signature=[a-f0-9]{64}$/
),
"x-amz-date": expect.stringMatching(/^\d{8}T\d{6}Z$/),
"x-amz-security-token": "sessionToken",
},
})
expect(logger.info).toHaveBeenCalledWith({
canonicalRequest: expect.stringMatching(
/^GET\n\/test\n\nhost:test\.execute-api\.us-east-1\.amazonaws\.com\n\nhost\n[a-f0-9]{64}$/
),
stringToSign: expect.stringMatching(
/^AWS4-HMAC-SHA256\n\d{8}T\d{6}Z\n\d{8}\/us-east-1\/execute-api\/aws4_request\n[a-f0-9]{64}$/
),
})
})
test("post with headers", async () => {
mockClient(api).resource("test").method("post").with({ body: "body" }).response({ ok: true })
const response = await api.test.post({ body: "body", headers: { test: "test" } })
// @ts-ignore
expect(response.originalRequest?.requestParams).toEqual({
body: "body",
headers: {
Authorization: expect.stringMatching(
/^AWS4-HMAC-SHA256 Credential=accessKeyId\/\d{8}\/us-east-1\/execute-api\/aws4_request, SignedHeaders=host;test, Signature=[a-f0-9]{64}$/
),
"x-amz-date": expect.stringMatching(/^\d{8}T\d{6}Z$/),
"x-amz-security-token": "sessionToken",
test: "test",
},
})
expect(logger.info).toHaveBeenCalledWith({
canonicalRequest: expect.stringMatching(
/^GET\n\/test\n\nhost:test\.execute-api\.us-east-1\.amazonaws\.com\n\nhost\n[a-f0-9]{64}$/
),
stringToSign: expect.stringMatching(
/^AWS4-HMAC-SHA256\n\d{8}T\d{6}Z\n\d{8}\/us-east-1\/execute-api\/aws4_request\n[a-f0-9]{64}$/
),
})
})
})