From c5a5099b235c89c694295eb5b711a6504c9f80db Mon Sep 17 00:00:00 2001 From: v1rtl Date: Thu, 3 Oct 2024 16:23:33 +0300 Subject: [PATCH] add a test case for binary files --- test.ts | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/test.ts b/test.ts index 7caf541..1d8b0af 100644 --- a/test.ts +++ b/test.ts @@ -251,8 +251,6 @@ describe('Multipart', () => { it('should parse multipart body', async () => { const server = createServer(async (req: ReqWithBody, res) => { await multipart()(req, res, (err) => err && console.log(err)) - - res.setHeader('Content-Type', 'multipart/form-data') res.end(JSON.stringify(req.body)) }) @@ -297,8 +295,6 @@ describe('Multipart', () => { const server = createServer(async (req: ReqWithBody, res) => { await multipart()(req, res, (err) => err && res.end(err)) - res.setHeader('Content-Type', 'multipart/form-data; boundary=some-boundary') - res.end(JSON.stringify(req.body)) }) @@ -320,8 +316,6 @@ describe('Multipart', () => { const server = createServer(async (req: ReqWithBody, res) => { await multipart()(req, res, (err) => err && console.log(err)) - res.setHeader('Content-Type', 'multipart/form-data; boundary=some-boundary') - res.end(JSON.stringify(req.body)) }) @@ -343,8 +337,6 @@ describe('Multipart', () => { const server = createServer(async (req: ReqWithBody, res) => { await multipart()(req, res, (err) => err && console.log(err)) - res.setHeader('Content-Type', 'multipart/form-data; boundary=some-boundary') - res.end('GET is ignored') }) @@ -364,8 +356,6 @@ describe('Multipart', () => { const server = createServer(async (req: ReqWithBody<{ file: [File] }>, res) => { await multipart()(req, res, (err) => err && console.log(err)) - res.setHeader('Content-Type', 'multipart/form-data') - const formBuf = new Uint8Array(await file.arrayBuffer()) const buf = new Uint8Array(await req.body!.file[0].arrayBuffer()) @@ -395,8 +385,6 @@ describe('Multipart', () => { const server = createServer(async (req: ReqWithBody<{ file1: [File]; file2: [File] }>, res) => { await multipart()(req, res, (err) => err && console.log(err)) - res.setHeader('Content-Type', 'multipart/form-data') - const files = Object.values(req.body!) for (const file of files) { @@ -413,6 +401,30 @@ describe('Multipart', () => { method: 'POST' }).expect(200) }) + it('should support binary files', async () => { + const fd = new FormData() + const file = new File([new Uint8Array([1, 2, 3])], 'blob.bin', { type: 'application/octet-stream' }) + fd.set('file', file) + + const server = createServer(async (req: ReqWithBody<{ file: [File] }>, res) => { + await multipart()(req, res, (err) => err && console.log(err)) + + + const formBuf = new Uint8Array(await file.arrayBuffer()) + const buf = new Uint8Array(await req.body!.file[0].arrayBuffer()) + + assert.equal(Buffer.compare(buf, formBuf), 0) + assert.equal(req.body?.file[0].type, 'application/octet-stream') + + res.end(req.body?.file[0].name) + }) + + await makeFetch(server)('/', { + // probaly better to use form-data package + body: fd, + method: 'POST' + }).expect(200, 'blob.bin') + }) }) describe('Limits', () => {