From b079acb0948e32c656f576c7eeadf53db5c5c21b Mon Sep 17 00:00:00 2001 From: Cameron Robey Date: Sat, 10 Sep 2022 00:00:25 +0100 Subject: [PATCH] Rewrite tests --- test/fetch/client-fetch.js | 85 +++++++++++++++----------------------- 1 file changed, 34 insertions(+), 51 deletions(-) diff --git a/test/fetch/client-fetch.js b/test/fetch/client-fetch.js index 631f0137232..e6c619130da 100644 --- a/test/fetch/client-fetch.js +++ b/test/fetch/client-fetch.js @@ -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') @@ -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') }) }) })