diff --git a/.changeset/cold-cats-rest.md b/.changeset/cold-cats-rest.md new file mode 100644 index 0000000..9a02aca --- /dev/null +++ b/.changeset/cold-cats-rest.md @@ -0,0 +1,5 @@ +--- +"nxjs-runtime": patch +--- + +Fix `FormData` test and add URL encoded POST test diff --git a/apps/tests/src/form-data.ts b/apps/tests/src/form-data.ts index 2abbcdc..9bee2a1 100644 --- a/apps/tests/src/form-data.ts +++ b/apps/tests/src/form-data.ts @@ -3,7 +3,24 @@ import * as assert from 'uvu/assert'; const test = suite('FormData'); -test('FormData', async () => { +test('FormData url encoded', async () => { + const r = new Response('a=a&a=b&b=c&c=d', { + headers: { + 'content-type': 'application/x-www-form-urlencoded', + }, + }); + const form = await r.formData(); + assert.instance(form, FormData); + assert.equal(form.get('a'), 'a'); + assert.equal(form.get('b'), 'c'); + assert.equal(form.get('c'), 'd'); + assert.equal(form.getAll('a'), ['a', 'b']); + assert.equal(form.getAll('b'), ['c']); + assert.equal(form.getAll('c'), ['d']); + assert.equal(form.get('missing'), null); +}); + +test('FormData multipart', async () => { const file = new File(['conte', new Blob(['nts'])], 'file.txt', { type: 'text/plain', }); diff --git a/apps/tests/src/url.ts b/apps/tests/src/url.ts index 0da6271..547fc93 100644 --- a/apps/tests/src/url.ts +++ b/apps/tests/src/url.ts @@ -3,7 +3,7 @@ import * as assert from 'uvu/assert'; const test = suite('URL'); -test('URLSearchParams iterator functions', () => { +test('URLSearchParams iterator functions', async () => { const p = new URLSearchParams('a=a&a=b&b=c&c=d'); assert.equal(Array.from(p.keys()), ['a', 'a', 'b', 'c']); assert.equal(Array.from(p.values()), ['a', 'b', 'c', 'd']); @@ -19,6 +19,10 @@ test('URLSearchParams iterator functions', () => { ['b', 'c'], ['c', 'd'], ]); + + const it = p.keys(); + await new Promise((r) => setTimeout(r, 100)); + assert.equal(Array.from(it), ['a', 'a', 'b', 'c']); }); /** diff --git a/packages/runtime/src/fetch/body.ts b/packages/runtime/src/fetch/body.ts index 4ea7e70..ba5b71c 100644 --- a/packages/runtime/src/fetch/body.ts +++ b/packages/runtime/src/fetch/body.ts @@ -180,7 +180,7 @@ export abstract class Body implements globalThis.Body { const v = decodeURIComponent(entry.slice(eq + 1)); form.append(k, v); } - } else if (contentType === 'multipart/form-data') { + } else if (contentType.startsWith('multipart/form-data;')) { const boundary = contentType .split(/;\s?/) .find((p) => p.startsWith('boundary='))