middleware.spec.ts
5.29 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { deepEqual, equal, ok } from "assert";
import { readFileSync } from "fs";
import { join } from "path";
import { HTTPError } from "../lib/exceptions";
import HTTPClient from "../lib/http";
import middleware from "../lib/middleware";
import * as Types from "../lib/types";
import { close, listen } from "./helpers/test-server";
const TEST_PORT = parseInt(process.env.TEST_PORT, 10);
const m = middleware({ channelSecret: "test_channel_secret" });
const getRecentReq = (): { body: Types.WebhookRequestBody } =>
JSON.parse(readFileSync(join(__dirname, "helpers/request.json")).toString());
describe("middleware", () => {
const webhook: Types.MessageEvent = {
message: {
id: "test_event_message_id",
text: "this is test message.😄😅😢😞😄😅😢😞",
type: "text",
},
replyToken: "test_reply_token",
source: {
groupId: "test_group_id",
type: "group",
},
timestamp: 0,
mode: "active",
type: "message",
};
const webhookSignature = {
"X-Line-Signature": "GzU7H3qOXDzDD6cNcS/9otLzlLFxnYYriz62rNu5BDE=",
};
const http = (headers: any = { ...webhookSignature }) =>
new HTTPClient({
baseURL: `http://localhost:${TEST_PORT}`,
defaultHeaders: headers,
});
before(() => listen(TEST_PORT, m));
after(() => close());
it("succeed", async () => {
await http().post(`/webhook`, {
events: [webhook],
destination: "Uaaaabbbbccccddddeeeeffff",
});
const req = getRecentReq();
deepEqual(req.body.destination, "Uaaaabbbbccccddddeeeeffff");
deepEqual(req.body.events, [webhook]);
});
it("succeed with pre-parsed string", async () => {
await http().post(`/mid-text`, {
events: [webhook],
destination: "Uaaaabbbbccccddddeeeeffff",
});
const req = getRecentReq();
deepEqual(req.body.destination, "Uaaaabbbbccccddddeeeeffff");
deepEqual(req.body.events, [webhook]);
});
it("succeed with pre-parsed buffer", async () => {
await http().post(`/mid-buffer`, {
events: [webhook],
destination: "Uaaaabbbbccccddddeeeeffff",
});
const req = getRecentReq();
deepEqual(req.body.destination, "Uaaaabbbbccccddddeeeeffff");
deepEqual(req.body.events, [webhook]);
});
it("succeed with pre-parsed buffer in rawBody", async () => {
await http().post(`/mid-rawbody`, {
events: [webhook],
destination: "Uaaaabbbbccccddddeeeeffff",
});
const req = getRecentReq();
deepEqual(req.body.destination, "Uaaaabbbbccccddddeeeeffff");
deepEqual(req.body.events, [webhook]);
});
it("fails on parsing raw as it's a not valid request and should be catched", async () => {
try {
await http({
"X-Line-Signature": "wqJD7WAIZhWcXThMCf8jZnwG3Hmn7EF36plkQGkj48w=",
"Content-Encoding": 1,
}).post(`/webhook`, {
events: [webhook],
destination: "Uaaaabbbbccccddddeeeeffff",
});
ok(false);
} catch (err) {
if (err instanceof HTTPError) {
equal(err.statusCode, 415);
} else {
throw err;
}
}
});
it("fails on pre-parsed json", async () => {
try {
await http().post(`/mid-json`, {
events: [webhook],
destination: "Uaaaabbbbccccddddeeeeffff",
});
ok(false);
} catch (err) {
if (err instanceof HTTPError) {
equal(err.statusCode, 500);
} else {
throw err;
}
}
});
it("fails on construct with no channelSecret", () => {
try {
middleware({ channelSecret: null });
ok(false);
} catch (err) {
equal(err.message, "no channel secret");
}
});
it("fails on wrong signature", async () => {
try {
await http({
"X-Line-Signature": "WqJD7WAIZhWcXThMCf8jZnwG3Hmn7EF36plkQGkj48w=",
}).post(`/webhook`, {
events: [webhook],
destination: "Uaaaabbbbccccddddeeeeffff",
});
ok(false);
} catch (err) {
if (err instanceof HTTPError) {
equal(err.statusCode, 401);
} else {
throw err;
}
}
});
it("fails on wrong signature (length)", async () => {
try {
await http({
"X-Line-Signature": "WqJD7WAIZ6plkQGkj48w=",
}).post(`/webhook`, {
events: [webhook],
destination: "Uaaaabbbbccccddddeeeeffff",
});
ok(false);
} catch (err) {
if (err instanceof HTTPError) {
equal(err.statusCode, 401);
} else {
throw err;
}
}
});
it("fails on invalid JSON", async () => {
try {
await http({
"X-Line-Signature": "Z8YlPpm0lQOqPipiCHVbiuwIDIzRzD7w5hvHgmwEuEs=",
}).post(`/webhook`, "i am not jason");
ok(false);
} catch (err) {
if (err instanceof HTTPError) {
equal(err.statusCode, 400);
} else {
throw err;
}
}
});
it("fails on empty signature", async () => {
try {
await http({}).post(`/webhook`, {
events: [webhook],
destination: "Uaaaabbbbccccddddeeeeffff",
});
ok(false);
} catch (err) {
if (err instanceof HTTPError) {
equal(err.statusCode, 401);
} else {
throw err;
}
}
});
});