Skip to content

Commit

Permalink
add a test case for binary files
Browse files Browse the repository at this point in the history
  • Loading branch information
talentlessguy committed Oct 3, 2024
1 parent 7bdf917 commit c5a5099
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})

Expand Down Expand Up @@ -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))
})

Expand All @@ -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))
})

Expand All @@ -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')
})

Expand All @@ -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())

Expand Down Expand Up @@ -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) {
Expand All @@ -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', () => {
Expand Down

0 comments on commit c5a5099

Please sign in to comment.