Skip to content

Commit

Permalink
Rewrite tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cameron-robey committed Sep 9, 2022
1 parent 0b4c505 commit b079acb
Showing 1 changed file with 34 additions and 51 deletions.
85 changes: 34 additions & 51 deletions test/fetch/client-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,6 @@ test('unsupported formData 1', (t) => {
})

test('multipart formdata not base64', async (t) => {
t.plan(2)

// Construct example form data, with text and blob fields
const formData = new FormData()
formData.append('field1', 'value1')
Expand All @@ -185,76 +183,61 @@ test('multipart formdata not base64', async (t) => {
})
t.teardown(server.close.bind(server))

await new Promise((resolve) => {
server.listen(0, async () => {
const response = await fetch(`http://localhost:${server.address().port}`)
const form = await response.formData()

// Text field
const field1 = form.get('field1')
t.equal(field1, 'value1')

// Blob field
const field2 = form.get('field2')
const field2Text = await field2.text()
t.equal(field2Text, 'example\ntext file')
resolve()
})
server.listen(0, () => {
fetch(`http://localhost:${server.address().port}`)
.then(res => res.formData())
.then(form => {
const field1 = form.get('field1')
t.equal(field1, 'value1')
const field2 = form.get('field2')
return field2.text()
})
.then(text => {
t.equal(text, 'example\ntext file')
})
})
})

test('busboy emit error', async (t) => {
test('multipart formdata base64', (t) => {
t.plan(1)

const formData = new FormData()
formData.append('field1', 'value1')

const tempRes = new Response(formData)
const formRaw = await tempRes.text()

// Example form data with base64 encoding
const formRaw = '------formdata-undici-0.5786922755719377\r\nContent-Disposition: form-data; name="key"; filename="test.txt"\r\nContent-Type: text/plain\r\nContent-Transfer-Encoding: base64\r\n\r\ndmFsdWU=\r\n------formdata-undici-0.5786922755719377--'
const server = createServer((req, res) => {
res.setHeader('content-type', 'multipart/form-data; boundary=wrongboundary')
res.setHeader('content-type', 'multipart/form-data; boundary=----formdata-undici-0.5786922755719377')
res.write(formRaw)
res.end()
})
t.teardown(server.close.bind(server))

await new Promise((resolve) => {
server.listen(0, async () => {
const response = await fetch(`http://localhost:${server.address().port}`)

try {
await response.formData()
} catch (err) {
t.equal(err.message, 'Unexpected end of form')
}

resolve()
})
server.listen(0, () => {
fetch(`http://localhost:${server.address().port}`)
.then(res => res.formData())
.then(form => form.get('key').text())
.then(text => {
t.equal(text, 'value')
})
})
})

test('multipart formdata base64', async (t) => {
t.plan(1)
test('busboy emit error', async (t) => {
const formData = new FormData()
formData.append('field1', 'value1')

const tempRes = new Response(formData)
const formRaw = await tempRes.text()

// Example form data with base64 encoding
const formRaw = '------formdata-undici-0.5786922755719377\r\nContent-Disposition: form-data; name="key"; filename="test.txt"\r\nContent-Type: text/plain\r\nContent-Transfer-Encoding: base64\r\n\r\ndmFsdWU=\r\n------formdata-undici-0.5786922755719377--'
const server = createServer((req, res) => {
res.setHeader('content-type', 'multipart/form-data; boundary=----formdata-undici-0.5786922755719377')
res.setHeader('content-type', 'multipart/form-data; boundary=wrongboundary')
res.write(formRaw)
res.end()
})
t.teardown(server.close.bind(server))

await new Promise((resolve) => {
server.listen(0, async () => {
const response = await fetch(`http://localhost:${server.address().port}`)
const form = await response.formData()

const text = await form.get('key').text()
console.log(text)
t.equal(text, 'value')
resolve()
server.listen(0, async () => {
const res = await fetch(`http://localhost:${server.address().port}`)
res.formData().catch(err => {
t.equal(err.message, 'Unexpected end of form')
})
})
})
Expand Down

0 comments on commit b079acb

Please sign in to comment.