This repository was archived by the owner on Jan 28, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathtriggerStaticRegeneration.test.ts
121 lines (111 loc) · 3.75 KB
/
triggerStaticRegeneration.test.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { triggerStaticRegeneration } from "../../src/lib/triggerStaticRegeneration";
import {
mockSQSClient,
mockSendMessageCommand
} from "../mocks/sqs/aws-sdk-sqs-client.mock";
import { jest } from "@jest/globals";
jest.mock("@aws-sdk/client-sqs", () =>
require("../mocks/sqs/aws-sdk-sqs-client.mock")
);
describe("triggerStaticRegeneration()", () => {
beforeEach(() => {
mockSQSClient.mockReset();
});
const options = {
basePath: "",
request: {
uri: "index.html",
origin: {
s3: {
region: "us-west-2",
domainName: `my-bucket.s3.us-west-2.amazonaws.com`
}
}
} as AWSLambda.CloudFrontRequest,
eTag: "123",
lastModified: "1",
queueName: "my-bucket.fifo",
pagePath: "/",
pageS3Path: "/static-pages/build-id/index.html"
};
class RequestThrottledException extends Error {
code = "RequestThrottled";
}
it("should not throttle if no rate limit is thrown", async () => {
mockSQSClient.mockImplementationOnce(() => ({ send: jest.fn() }));
const staticRegeneratedResponse = await triggerStaticRegeneration(options);
expect(staticRegeneratedResponse.throttle).toBe(false);
});
it("should throttle if a RequestThrottledException is thrown", async () => {
mockSendMessageCommand.mockImplementationOnce(() => {
throw new RequestThrottledException();
});
const staticRegeneratedResponse = await triggerStaticRegeneration(options);
expect(staticRegeneratedResponse.throttle).toBe(true);
});
it("should rethrow an unknown error", async () => {
mockSendMessageCommand.mockImplementationOnce(() => {
throw new Error("Unknown error");
});
await expect(triggerStaticRegeneration(options)).rejects.toEqual(
new Error("Unknown error")
);
});
it("should reject when corrupt s3 name is passed", async () => {
await expect(
triggerStaticRegeneration({
...options,
request: {
...options.request,
origin: {
...options.request.origin,
s3: undefined
}
} as AWSLambda.CloudFrontRequest
})
).rejects.toEqual(new Error("Expected bucket name to be defined"));
});
it("should reject when no region is passed", async () => {
await expect(
triggerStaticRegeneration({
...options,
request: {
...options.request,
origin: {
...options.request.origin,
s3: { ...options.request.origin?.s3, region: "" }
}
} as AWSLambda.CloudFrontRequest
})
).rejects.toEqual(new Error("Expected region to be defined"));
});
it.each`
lastModified | etag | expected
${"2021-05-05T17:15:04.472Z"} | ${"tag"} | ${"tag"}
${"2021-05-05T17:15:04.472Z"} | ${undefined} | ${"1620234904472"}
`(
"should throttle send correct parameters to the queue",
async ({ lastModified, etag, expected }) => {
mockSQSClient.mockImplementationOnce(() => ({ send: jest.fn() }));
const staticRegeneratedResponse = await triggerStaticRegeneration({
...options,
eTag: etag,
lastModified: lastModified
});
expect(staticRegeneratedResponse.throttle).toBe(false);
expect(mockSendMessageCommand).toHaveBeenCalledWith({
QueueUrl: `https://sqs.us-west-2.amazonaws.com/my-bucket.fifo`,
MessageBody: JSON.stringify({
region: "us-west-2",
bucketName: "my-bucket",
pageS3Path: "/static-pages/build-id/index.html",
cloudFrontEventRequest: options.request,
basePath: "",
pagePath: "/"
}),
MessageDeduplicationId: expected,
MessageGroupId: "eacf331f0ffc35d4b482f1d15a887d3b" // md5 hash of "index.html"
});
}
);
});