Skip to content

Commit

Permalink
fix(readMultipartFormData): handle utf8 encoding for name and `file…
Browse files Browse the repository at this point in the history
…name` (#416)
  • Loading branch information
zhiyuanzmj authored Jul 24, 2023
1 parent ed3ae90 commit 78aec63
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/utils/internal/multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ function process(data: number[], headers: [string, string][]): MultiPartData {
}
const key = (s[0] || "").trim();
if (key === "name" || key === "filename") {
dataObj[key] = (s[1] || "").trim().replace(/"/g, "");
const _value = (s[1] || "").trim().replace(/"/g, "");
dataObj[key] = Buffer.from(_value, "latin1").toString("utf8");
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/body.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ describe("", () => {
"multipart/form-data; boundary=---------------------------12537827810750053901680552518"
)
.send(
'-----------------------------12537827810750053901680552518\r\nContent-Disposition: form-data; name="baz"\r\n\r\nother\r\n-----------------------------12537827810750053901680552518\r\nContent-Disposition: form-data; name="bar"\r\n\r\nsomething\r\n-----------------------------12537827810750053901680552518--\r\n'
'-----------------------------12537827810750053901680552518\r\nContent-Disposition: form-data; name="baz"\r\n\r\nother\r\n-----------------------------12537827810750053901680552518\r\nContent-Disposition: form-data; name="号楼电表数据模版.xlsx"\r\n\r\nsomething\r\n-----------------------------12537827810750053901680552518--\r\n'
);

expect(result.body).toMatchInlineSnapshot(`
Expand All @@ -260,7 +260,7 @@ describe("", () => {
},
{
"data": "something",
"name": "bar",
"name": "号楼电表数据模版.xlsx",
},
]
`);
Expand Down

0 comments on commit 78aec63

Please sign in to comment.