Skip to content

Commit 7795137

Browse files
committed
Define mWith~ within test description
1 parent 067d4e9 commit 7795137

File tree

1 file changed

+25
-61
lines changed

1 file changed

+25
-61
lines changed

test/middleware.spec.ts

Lines changed: 25 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,6 @@ const TEST_PORT = parseInt(process.env.TEST_PORT || "1234", 10);
1313

1414
const m = middleware({ channelSecret: "test_channel_secret" });
1515

16-
// Middleware with skipSignatureVerification function (always true)
17-
const mWithSkipAlwaysTrue = middleware({
18-
channelSecret: "test_channel_secret",
19-
skipSignatureVerification: () => true,
20-
});
21-
22-
// Middleware with skipSignatureVerification function (dynamic behavior based on environment variable)
23-
let shouldSkipSignature = false;
24-
const mWithDynamicSkip = middleware({
25-
channelSecret: "test_channel_secret",
26-
skipSignatureVerification: () => shouldSkipSignature,
27-
});
28-
2916
const getRecentReq = (): { body: Types.WebhookRequestBody } =>
3017
JSON.parse(readFileSync(join(__dirname, "helpers/request.json")).toString());
3118

@@ -68,54 +55,32 @@ describe("middleware test", () => {
6855
});
6956

7057
describe("With skipSignatureVerification functionality", () => {
71-
// Port for always-true skip function
72-
let alwaysTruePort: number;
73-
// Port for dynamic skip function
74-
let dynamicSkipPort: number;
75-
76-
beforeAll(() => {
77-
alwaysTruePort = TEST_PORT + 1;
78-
dynamicSkipPort = TEST_PORT + 2;
79-
listen(alwaysTruePort, mWithSkipAlwaysTrue);
80-
return listen(dynamicSkipPort, mWithDynamicSkip);
81-
});
82-
83-
afterAll(() => {
84-
close(alwaysTruePort);
85-
return close(dynamicSkipPort);
86-
});
58+
let serverPort: number;
8759

88-
it("should skip signature verification when skipSignatureVerification returns true", async () => {
89-
const client = new HTTPClient({
90-
baseURL: `http://localhost:${alwaysTruePort}`,
60+
const createClient = (port: number) =>
61+
new HTTPClient({
62+
baseURL: `http://localhost:${port}`,
9163
defaultHeaders: {
9264
"X-Line-Signature": "invalid_signature",
9365
},
9466
});
9567

96-
// This should work even with invalid signature because verification is skipped
97-
await client.post("/webhook", {
98-
events: [webhook],
99-
destination: DESTINATION,
100-
});
101-
102-
const req = getRecentReq();
103-
deepEqual(req.body.destination, DESTINATION);
104-
deepEqual(req.body.events, [webhook]);
68+
afterEach(() => {
69+
if (serverPort) {
70+
close(serverPort);
71+
}
10572
});
10673

107-
it("should respect dynamic skipSignatureVerification behavior - when true", async () => {
108-
// Set to skip verification
109-
shouldSkipSignature = true;
110-
111-
const client = new HTTPClient({
112-
baseURL: `http://localhost:${dynamicSkipPort}`,
113-
defaultHeaders: {
114-
"X-Line-Signature": "invalid_signature",
115-
},
74+
it("should skip signature verification when skipSignatureVerification returns true", async () => {
75+
serverPort = TEST_PORT + 1;
76+
const m = middleware({
77+
channelSecret: "test_channel_secret",
78+
skipSignatureVerification: () => true,
11679
});
80+
await listen(serverPort, m);
81+
82+
const client = createClient(serverPort);
11783

118-
// This should work even with invalid signature because verification is skipped
11984
await client.post("/webhook", {
12085
events: [webhook],
12186
destination: DESTINATION,
@@ -126,19 +91,17 @@ describe("middleware test", () => {
12691
deepEqual(req.body.events, [webhook]);
12792
});
12893

129-
it("should respect dynamic skipSignatureVerification behavior - when false", async () => {
130-
// Set to NOT skip verification
131-
shouldSkipSignature = false;
132-
133-
const client = new HTTPClient({
134-
baseURL: `http://localhost:${dynamicSkipPort}`,
135-
defaultHeaders: {
136-
"X-Line-Signature": "invalid_signature",
137-
},
94+
it("should skip signature verification when skipSignatureVerification returns false", async () => {
95+
serverPort = TEST_PORT + 2;
96+
const m = middleware({
97+
channelSecret: "test_channel_secret",
98+
skipSignatureVerification: () => false,
13899
});
100+
await listen(serverPort, m);
101+
102+
const client = createClient(serverPort);
139103

140104
try {
141-
// This should fail because signature verification is not skipped
142105
await client.post("/webhook", {
143106
events: [webhook],
144107
destination: DESTINATION,
@@ -153,6 +116,7 @@ describe("middleware test", () => {
153116
}
154117
});
155118
});
119+
156120
afterAll(() => {
157121
close(TEST_PORT);
158122
});

0 commit comments

Comments
 (0)