diff options
author | 2023-08-02 18:56:41 -0700 | |
---|---|---|
committer | 2023-08-02 18:56:41 -0700 | |
commit | e83058cfe7fd7ae09363d7a9f697fa9f7d062307 (patch) | |
tree | 6407ba70d019112e8a3f354d7b3f58c013151218 /test | |
parent | aaaaf744a80bc2876b4edc174a0d86c11625ec20 (diff) | |
download | bun-e83058cfe7fd7ae09363d7a9f697fa9f7d062307.tar.gz bun-e83058cfe7fd7ae09363d7a9f697fa9f7d062307.tar.zst bun-e83058cfe7fd7ae09363d7a9f697fa9f7d062307.zip |
Fix http write (#3939)
* Fix encoding problem when uploading a binary file.
Close: #3116
* use BufferList
* Finish rebase
---------
Co-authored-by: Hanaasagi <ambiguous404@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/js/node/http/node-http.test.ts | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/test/js/node/http/node-http.test.ts b/test/js/node/http/node-http.test.ts index 0e7b3ca13..e8fe4dae8 100644 --- a/test/js/node/http/node-http.test.ts +++ b/test/js/node/http/node-http.test.ts @@ -169,6 +169,18 @@ describe("node:http", () => { res.end("Hello World"); return; } + if (reqUrl.pathname === "/uploadFile") { + let requestData = Buffer.alloc(0); + req.on("data", chunk => { + requestData = Buffer.concat([requestData, chunk]); + }); + req.on("end", () => { + res.writeHead(200, { "Content-Type": "text/plain" }); + res.write(requestData); + res.end(); + }); + return; + } } res.writeHead(200, { "Content-Type": "text/plain" }); @@ -540,6 +552,49 @@ describe("node:http", () => { req.end(); }); }); + it("uploading file by 'formdata/multipart', issue#3116", done => { + runTest(done, (server, serverPort, done) => { + const boundary = "----FormBoundary" + Date.now(); + + const formDataBegin = `--${boundary}\r\nContent-Disposition: form-data; name="file"; filename="myfile.txt"\r\nContent-Type: application/octet-stream\r\n\r\n`; + const fileData = Buffer.from("80818283", "hex"); + const formDataEnd = `\r\n--${boundary}--`; + + const requestOptions = { + hostname: "localhost", + port: serverPort, + path: "/uploadFile", + method: "POST", + headers: { + "Content-Type": `multipart/form-data; boundary=${boundary}`, + }, + }; + + const req = request(requestOptions, res => { + let responseData = Buffer.alloc(0); + res.on("data", chunk => { + responseData = Buffer.concat([responseData, chunk]); + }); + res.on("end", () => { + try { + expect(responseData).toEqual( + Buffer.concat([Buffer.from(formDataBegin), fileData, Buffer.from(formDataEnd)]), + ); + } catch (e) { + return done(e); + } + done(); + }); + }); + req.on("error", err => { + done(err); + }); + req.write(formDataBegin); // string + req.write(fileData); // Buffer + req.write(formDataEnd); // string + req.end(); + }); + }); }); describe("signal", () => { |