Skip to content

Commit 7516f23

Browse files
author
Hyunje Jun
committed
Add body assertions for postBinary
It's failing for the stream case for the time being.
1 parent 3e02442 commit 7516f23

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

test/helpers/test-server.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ function listen(port: number, middleware?: express.RequestHandler) {
2828
app.use(middleware);
2929
}
3030

31-
app.use(bodyParser.json());
31+
app.use((req: express.Request, res, next) => {
32+
if (req.path === "/post/binary") {
33+
bodyParser.raw({ type: "*/*" })(req, res, next);
34+
} else {
35+
bodyParser.json({ type: "application/json" })(req, res, next);
36+
}
37+
});
3238

3339
// for getIds API
3440
app.get("/:groupOrRoom/:id/members/ids", (req, res) => {
@@ -55,10 +61,16 @@ function listen(port: number, middleware?: express.RequestHandler) {
5561
} else if (req.path === "/404") {
5662
res.status(404).end();
5763
} else {
58-
const keys = ["body", "headers", "method", "path", "query"];
59-
res.json(
60-
keys.reduce((r, k) => Object.assign(r, { [k]: (req as any)[k] }), {}),
64+
const result: any = ["headers", "method", "path", "query"].reduce(
65+
(r, k) => Object.assign(r, { [k]: (req as any)[k] }),
66+
{},
6167
);
68+
if (Buffer.isBuffer(req.body)) {
69+
result.body = req.body.toString("base64");
70+
} else {
71+
result.body = req.body;
72+
}
73+
res.json(result);
6274
}
6375
});
6476

test/http.spec.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,13 @@ describe("http", () => {
9898
const filepath = join(__dirname, "/helpers/line-icon.png");
9999
const buffer = readFileSync(filepath);
100100
return postBinary(
101-
`${TEST_URL}/post`,
101+
`${TEST_URL}/post/binary`,
102102
testHeaders,
103103
buffer,
104104
).then((res: any) => {
105105
equal(res.method, "POST");
106-
equal(res.path, "/post");
106+
equal(res.path, "/post/binary");
107+
equal(res.body, buffer.toString("base64"));
107108
equal(res.headers["test-header-key"], testHeaders["test-header-key"]);
108109
equal(res.headers["user-agent"], `${pkg.name}/${pkg.version}`);
109110
equal(res.headers["content-type"], "image/png");
@@ -114,19 +115,25 @@ describe("http", () => {
114115
const filepath = join(__dirname, "/helpers/line-icon.png");
115116
const buffer = readFileSync(filepath);
116117
return postBinary(
117-
`${TEST_URL}/post`,
118+
`${TEST_URL}/post/binary`,
118119
{},
119120
buffer,
120121
"image/jpeg",
121122
).then((res: any) => {
123+
equal(res.body, buffer.toString("base64"));
122124
equal(res.headers["content-type"], "image/jpeg");
123125
});
124126
});
125127

126128
it("postBinary with stream", () => {
127129
const filepath = join(__dirname, "/helpers/line-icon.png");
128130
const stream = createReadStream(filepath);
129-
return postBinary(`${TEST_URL}/post`, {}, stream).then((res: any) => {
131+
return postBinary(
132+
`${TEST_URL}/post/binary`,
133+
{},
134+
stream,
135+
).then((res: any) => {
136+
equal(res.body, readFileSync(filepath).toString("base64"));
130137
equal(res.headers["content-type"], "image/png");
131138
});
132139
});

0 commit comments

Comments
 (0)