Skip to content

Commit b079acb

Browse files
committed
Rewrite tests
1 parent 0b4c505 commit b079acb

1 file changed

Lines changed: 34 additions & 51 deletions

File tree

test/fetch/client-fetch.js

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ test('unsupported formData 1', (t) => {
166166
})
167167

168168
test('multipart formdata not base64', async (t) => {
169-
t.plan(2)
170-
171169
// Construct example form data, with text and blob fields
172170
const formData = new FormData()
173171
formData.append('field1', 'value1')
@@ -185,76 +183,61 @@ test('multipart formdata not base64', async (t) => {
185183
})
186184
t.teardown(server.close.bind(server))
187185

188-
await new Promise((resolve) => {
189-
server.listen(0, async () => {
190-
const response = await fetch(`http://localhost:${server.address().port}`)
191-
const form = await response.formData()
192-
193-
// Text field
194-
const field1 = form.get('field1')
195-
t.equal(field1, 'value1')
196-
197-
// Blob field
198-
const field2 = form.get('field2')
199-
const field2Text = await field2.text()
200-
t.equal(field2Text, 'example\ntext file')
201-
resolve()
202-
})
186+
server.listen(0, () => {
187+
fetch(`http://localhost:${server.address().port}`)
188+
.then(res => res.formData())
189+
.then(form => {
190+
const field1 = form.get('field1')
191+
t.equal(field1, 'value1')
192+
const field2 = form.get('field2')
193+
return field2.text()
194+
})
195+
.then(text => {
196+
t.equal(text, 'example\ntext file')
197+
})
203198
})
204199
})
205200

206-
test('busboy emit error', async (t) => {
201+
test('multipart formdata base64', (t) => {
207202
t.plan(1)
208203

209-
const formData = new FormData()
210-
formData.append('field1', 'value1')
211-
212-
const tempRes = new Response(formData)
213-
const formRaw = await tempRes.text()
214-
204+
// Example form data with base64 encoding
205+
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--'
215206
const server = createServer((req, res) => {
216-
res.setHeader('content-type', 'multipart/form-data; boundary=wrongboundary')
207+
res.setHeader('content-type', 'multipart/form-data; boundary=----formdata-undici-0.5786922755719377')
217208
res.write(formRaw)
218209
res.end()
219210
})
220211
t.teardown(server.close.bind(server))
221212

222-
await new Promise((resolve) => {
223-
server.listen(0, async () => {
224-
const response = await fetch(`http://localhost:${server.address().port}`)
225-
226-
try {
227-
await response.formData()
228-
} catch (err) {
229-
t.equal(err.message, 'Unexpected end of form')
230-
}
231-
232-
resolve()
233-
})
213+
server.listen(0, () => {
214+
fetch(`http://localhost:${server.address().port}`)
215+
.then(res => res.formData())
216+
.then(form => form.get('key').text())
217+
.then(text => {
218+
t.equal(text, 'value')
219+
})
234220
})
235221
})
236222

237-
test('multipart formdata base64', async (t) => {
238-
t.plan(1)
223+
test('busboy emit error', async (t) => {
224+
const formData = new FormData()
225+
formData.append('field1', 'value1')
226+
227+
const tempRes = new Response(formData)
228+
const formRaw = await tempRes.text()
239229

240-
// Example form data with base64 encoding
241-
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--'
242230
const server = createServer((req, res) => {
243-
res.setHeader('content-type', 'multipart/form-data; boundary=----formdata-undici-0.5786922755719377')
231+
res.setHeader('content-type', 'multipart/form-data; boundary=wrongboundary')
244232
res.write(formRaw)
245233
res.end()
246234
})
247235
t.teardown(server.close.bind(server))
248236

249-
await new Promise((resolve) => {
250-
server.listen(0, async () => {
251-
const response = await fetch(`http://localhost:${server.address().port}`)
252-
const form = await response.formData()
253-
254-
const text = await form.get('key').text()
255-
console.log(text)
256-
t.equal(text, 'value')
257-
resolve()
237+
server.listen(0, async () => {
238+
const res = await fetch(`http://localhost:${server.address().port}`)
239+
res.formData().catch(err => {
240+
t.equal(err.message, 'Unexpected end of form')
258241
})
259242
})
260243
})

0 commit comments

Comments
 (0)