Skip to content

Commit 952e05e

Browse files
author
Hyunje Jun
committed
Change how to check request object in tests
Do not respond a request object, but write the object into a file and read it from test cases. By this change, we can check requests regardless of the form of response.
1 parent 2803232 commit 952e05e

File tree

5 files changed

+226
-156
lines changed

5 files changed

+226
-156
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,6 @@ dist
6262

6363
# GitBook
6464
_book
65+
66+
# Test recent request
67+
test/helpers/request.json

test/client.spec.ts

Lines changed: 136 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ const client = new Client({
1414
channelSecret: "test_channel_secret",
1515
});
1616

17+
const getRecentReq = (): any =>
18+
JSON.parse(readFileSync(join(__dirname, "helpers/request.json")).toString());
19+
1720
describe("client", () => {
1821
before(() => listen(TEST_PORT));
1922
after(() => close());
@@ -45,181 +48,214 @@ describe("client", () => {
4548

4649
it("reply", () => {
4750
return client.replyMessage("test_reply_token", testMsg).then((res: any) => {
48-
equal(res.headers.authorization, "Bearer test_channel_access_token");
49-
equal(res.path, "/message/reply");
50-
equal(res.method, "POST");
51-
equal(res.body.replyToken, "test_reply_token");
52-
deepEqual(res.body.messages, [testMsg]);
51+
const req = getRecentReq();
52+
equal(req.headers.authorization, "Bearer test_channel_access_token");
53+
equal(req.path, "/message/reply");
54+
equal(req.method, "POST");
55+
equal(req.body.replyToken, "test_reply_token");
56+
deepEqual(req.body.messages, [testMsg]);
57+
deepEqual(res, {});
5358
});
5459
});
5560

5661
it("push", () => {
5762
return client.pushMessage("test_user_id", testMsg).then((res: any) => {
58-
equal(res.headers.authorization, "Bearer test_channel_access_token");
59-
equal(res.path, "/message/push");
60-
equal(res.method, "POST");
61-
equal(res.body.to, "test_user_id");
62-
deepEqual(res.body.messages, [testMsg]);
63+
const req = getRecentReq();
64+
equal(req.headers.authorization, "Bearer test_channel_access_token");
65+
equal(req.path, "/message/push");
66+
equal(req.method, "POST");
67+
equal(req.body.to, "test_user_id");
68+
deepEqual(req.body.messages, [testMsg]);
69+
deepEqual(res, {});
6370
});
6471
});
6572

6673
it("multicast", () => {
6774
const ids = ["test_user_id_1", "test_user_id_2", "test_user_id_3"];
6875
return client.multicast(ids, [testMsg, testMsg]).then((res: any) => {
69-
equal(res.headers.authorization, "Bearer test_channel_access_token");
70-
equal(res.path, "/message/multicast");
71-
equal(res.method, "POST");
72-
deepEqual(res.body.to, [
76+
const req = getRecentReq();
77+
equal(req.headers.authorization, "Bearer test_channel_access_token");
78+
equal(req.path, "/message/multicast");
79+
equal(req.method, "POST");
80+
deepEqual(req.body.to, [
7381
"test_user_id_1",
7482
"test_user_id_2",
7583
"test_user_id_3",
7684
]);
77-
deepEqual(res.body.messages, [testMsg, testMsg]);
85+
deepEqual(req.body.messages, [testMsg, testMsg]);
86+
deepEqual(res, {});
7887
});
7988
});
8089

8190
it("getProfile", () => {
8291
return client.getProfile("test_user_id").then((res: any) => {
83-
equal(res.headers.authorization, "Bearer test_channel_access_token");
84-
equal(res.path, "/profile/test_user_id");
85-
equal(res.method, "GET");
92+
const req = getRecentReq();
93+
equal(req.headers.authorization, "Bearer test_channel_access_token");
94+
equal(req.path, "/profile/test_user_id");
95+
equal(req.method, "GET");
96+
deepEqual(res, {});
8697
});
8798
});
8899

89100
it("getGroupMemberProfile", () => {
90101
return client
91102
.getGroupMemberProfile("test_group_id", "test_user_id")
92103
.then((res: any) => {
93-
equal(res.headers.authorization, "Bearer test_channel_access_token");
94-
equal(res.path, "/group/test_group_id/member/test_user_id");
95-
equal(res.method, "GET");
104+
const req = getRecentReq();
105+
equal(req.headers.authorization, "Bearer test_channel_access_token");
106+
equal(req.path, "/group/test_group_id/member/test_user_id");
107+
equal(req.method, "GET");
108+
deepEqual(res, {});
96109
});
97110
});
98111

99112
it("getRoomMemberProfile", () => {
100113
return client
101114
.getRoomMemberProfile("test_room_id", "test_user_id")
102115
.then((res: any) => {
103-
equal(res.headers.authorization, "Bearer test_channel_access_token");
104-
equal(res.path, "/room/test_room_id/member/test_user_id");
105-
equal(res.method, "GET");
116+
const req = getRecentReq();
117+
equal(req.headers.authorization, "Bearer test_channel_access_token");
118+
equal(req.path, "/room/test_room_id/member/test_user_id");
119+
equal(req.method, "GET");
120+
deepEqual(res, {});
106121
});
107122
});
108123

109124
it("getGroupMemberIds", () => {
110-
return client
111-
.getGroupMemberIds("test_group_id")
112-
.then((ids: string[]) =>
113-
deepEqual(ids, [
114-
"group-test_group_id-0",
115-
"group-test_group_id-1",
116-
"group-test_group_id-2",
117-
"group-test_group_id-3",
118-
"group-test_group_id-4",
119-
"group-test_group_id-5",
120-
"group-test_group_id-6",
121-
"group-test_group_id-7",
122-
"group-test_group_id-8",
123-
]),
124-
);
125+
return client.getGroupMemberIds("test_group_id").then((ids: string[]) => {
126+
const req = getRecentReq();
127+
equal(req.headers.authorization, "Bearer test_channel_access_token");
128+
equal(req.path, "/group/test_group_id/members/ids");
129+
equal(req.method, "GET");
130+
deepEqual(ids, [
131+
"group-test_group_id-0",
132+
"group-test_group_id-1",
133+
"group-test_group_id-2",
134+
"group-test_group_id-3",
135+
"group-test_group_id-4",
136+
"group-test_group_id-5",
137+
"group-test_group_id-6",
138+
"group-test_group_id-7",
139+
"group-test_group_id-8",
140+
]);
141+
});
125142
});
126143

127144
it("getRoomMemberIds", () => {
128-
return client
129-
.getRoomMemberIds("test_room_id")
130-
.then((ids: string[]) =>
131-
deepEqual(ids, [
132-
"room-test_room_id-0",
133-
"room-test_room_id-1",
134-
"room-test_room_id-2",
135-
"room-test_room_id-3",
136-
"room-test_room_id-4",
137-
"room-test_room_id-5",
138-
"room-test_room_id-6",
139-
"room-test_room_id-7",
140-
"room-test_room_id-8",
141-
]),
142-
);
145+
return client.getRoomMemberIds("test_room_id").then((ids: string[]) => {
146+
const req = getRecentReq();
147+
equal(req.headers.authorization, "Bearer test_channel_access_token");
148+
equal(req.path, "/room/test_room_id/members/ids");
149+
equal(req.method, "GET");
150+
deepEqual(ids, [
151+
"room-test_room_id-0",
152+
"room-test_room_id-1",
153+
"room-test_room_id-2",
154+
"room-test_room_id-3",
155+
"room-test_room_id-4",
156+
"room-test_room_id-5",
157+
"room-test_room_id-6",
158+
"room-test_room_id-7",
159+
"room-test_room_id-8",
160+
]);
161+
});
143162
});
144163

145164
it("getMessageContent", () => {
146165
return client
147166
.getMessageContent("test_message_id")
148167
.then((s: Readable) => getStreamData(s))
149168
.then((data: string) => {
169+
const req = getRecentReq();
170+
equal(req.headers.authorization, "Bearer test_channel_access_token");
171+
equal(req.path, "/message/test_message_id/content");
172+
equal(req.method, "GET");
173+
150174
const res = JSON.parse(data);
151-
equal(res.headers.authorization, "Bearer test_channel_access_token");
152-
equal(res.path, "/message/test_message_id/content");
153-
equal(res.method, "GET");
175+
deepEqual(res, {});
154176
});
155177
});
156178

157179
it("leaveGroup", () => {
158180
return client.leaveGroup("test_group_id").then((res: any) => {
159-
equal(res.headers.authorization, "Bearer test_channel_access_token");
160-
equal(res.path, "/group/test_group_id/leave");
161-
equal(res.method, "POST");
181+
const req = getRecentReq();
182+
equal(req.headers.authorization, "Bearer test_channel_access_token");
183+
equal(req.path, "/group/test_group_id/leave");
184+
equal(req.method, "POST");
185+
deepEqual(res, {});
162186
});
163187
});
164188

165189
it("leaveRoom", () => {
166190
return client.leaveRoom("test_room_id").then((res: any) => {
167-
equal(res.headers.authorization, "Bearer test_channel_access_token");
168-
equal(res.path, "/room/test_room_id/leave");
169-
equal(res.method, "POST");
191+
const req = getRecentReq();
192+
equal(req.headers.authorization, "Bearer test_channel_access_token");
193+
equal(req.path, "/room/test_room_id/leave");
194+
equal(req.method, "POST");
195+
deepEqual(res, {});
170196
});
171197
});
172198

173199
it("getRichMenu", () => {
174200
return client.getRichMenu("test_rich_menu_id").then((res: any) => {
175-
equal(res.headers.authorization, "Bearer test_channel_access_token");
176-
equal(res.path, "/richmenu/test_rich_menu_id");
177-
equal(res.method, "GET");
201+
const req = getRecentReq();
202+
equal(req.headers.authorization, "Bearer test_channel_access_token");
203+
equal(req.path, "/richmenu/test_rich_menu_id");
204+
equal(req.method, "GET");
205+
deepEqual(res, {});
178206
});
179207
});
180208

181209
it("createRichMenu", () => {
182-
return client.createRichMenu(richMenu).then((res: any) => {
183-
equal(res.headers.authorization, "Bearer test_channel_access_token");
184-
equal(res.path, "/richmenu");
185-
equal(res.method, "POST");
186-
deepEqual(res.body, richMenu);
210+
return client.createRichMenu(richMenu).then(() => {
211+
const req = getRecentReq();
212+
equal(req.headers.authorization, "Bearer test_channel_access_token");
213+
equal(req.path, "/richmenu");
214+
equal(req.method, "POST");
215+
deepEqual(req.body, richMenu);
187216
});
188217
});
189218

190219
it("deleteRichMenu", () => {
191220
return client.deleteRichMenu("test_rich_menu_id").then((res: any) => {
192-
equal(res.headers.authorization, "Bearer test_channel_access_token");
193-
equal(res.path, "/richmenu/test_rich_menu_id");
194-
equal(res.method, "DELETE");
221+
const req = getRecentReq();
222+
equal(req.headers.authorization, "Bearer test_channel_access_token");
223+
equal(req.path, "/richmenu/test_rich_menu_id");
224+
equal(req.method, "DELETE");
225+
deepEqual(res, {});
195226
});
196227
});
197228

198229
it("getRichMenuIdOfUser", () => {
199-
return client.getRichMenuIdOfUser("test_user_id").then((res: any) => {
200-
equal(res.headers.authorization, "Bearer test_channel_access_token");
201-
equal(res.path, "/user/test_user_id/richmenu");
202-
equal(res.method, "GET");
230+
return client.getRichMenuIdOfUser("test_user_id").then(() => {
231+
const req = getRecentReq();
232+
equal(req.headers.authorization, "Bearer test_channel_access_token");
233+
equal(req.path, "/user/test_user_id/richmenu");
234+
equal(req.method, "GET");
203235
});
204236
});
205237

206238
it("linkRichMenuToUser", () => {
207239
return client
208240
.linkRichMenuToUser("test_user_id", "test_rich_menu_id")
209241
.then((res: any) => {
210-
equal(res.headers.authorization, "Bearer test_channel_access_token");
211-
equal(res.path, "/user/test_user_id/richmenu/test_rich_menu_id");
212-
equal(res.method, "POST");
242+
const req = getRecentReq();
243+
equal(req.headers.authorization, "Bearer test_channel_access_token");
244+
equal(req.path, "/user/test_user_id/richmenu/test_rich_menu_id");
245+
equal(req.method, "POST");
246+
deepEqual(res, {});
213247
});
214248
});
215249

216250
it("unlinkRichMenuFromUser", () => {
217251
return client
218252
.unlinkRichMenuFromUser("test_user_id", "test_rich_menu_id")
219253
.then((res: any) => {
220-
equal(res.headers.authorization, "Bearer test_channel_access_token");
221-
equal(res.path, "/user/test_user_id/richmenu/test_rich_menu_id");
222-
equal(res.method, "DELETE");
254+
const req = getRecentReq();
255+
equal(req.headers.authorization, "Bearer test_channel_access_token");
256+
equal(req.path, "/user/test_user_id/richmenu/test_rich_menu_id");
257+
equal(req.method, "DELETE");
258+
deepEqual(res, {});
223259
});
224260
});
225261

@@ -229,9 +265,11 @@ describe("client", () => {
229265
return client
230266
.setRichMenuImage("test_rich_menu_id", buffer)
231267
.then((res: any) => {
232-
equal(res.headers.authorization, "Bearer test_channel_access_token");
233-
equal(res.path, "/richmenu/test_rich_menu_id/content");
234-
equal(res.method, "POST");
268+
const req = getRecentReq();
269+
equal(req.headers.authorization, "Bearer test_channel_access_token");
270+
equal(req.path, "/richmenu/test_rich_menu_id/content");
271+
equal(req.method, "POST");
272+
deepEqual(res, {});
235273
});
236274
});
237275

@@ -240,18 +278,22 @@ describe("client", () => {
240278
.getRichMenuImage("test_rich_menu_id")
241279
.then((s: Readable) => getStreamData(s))
242280
.then((data: string) => {
281+
const req = getRecentReq();
282+
equal(req.headers.authorization, "Bearer test_channel_access_token");
283+
equal(req.path, "/richmenu/test_rich_menu_id/content");
284+
equal(req.method, "GET");
285+
243286
const res = JSON.parse(data);
244-
equal(res.headers.authorization, "Bearer test_channel_access_token");
245-
equal(res.path, "/richmenu/test_rich_menu_id/content");
246-
equal(res.method, "GET");
287+
deepEqual(res, {});
247288
});
248289
});
249290

250291
it("getRichMenuList", () => {
251-
return client.getRichMenuList().then((res: any) => {
252-
equal(res.headers.authorization, "Bearer test_channel_access_token");
253-
equal(res.path, "/richmenu/list");
254-
equal(res.method, "GET");
292+
return client.getRichMenuList().then(() => {
293+
const req = getRecentReq();
294+
equal(req.headers.authorization, "Bearer test_channel_access_token");
295+
equal(req.path, "/richmenu/list");
296+
equal(req.method, "GET");
255297
});
256298
});
257299
});

0 commit comments

Comments
 (0)