Skip to content

Commit

Permalink
Fix FormData test and add URL encoded POST test
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Mar 12, 2024
1 parent 56b6e88 commit 7b6f8b5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-cats-rest.md
@@ -0,0 +1,5 @@
---
"nxjs-runtime": patch
---

Fix `FormData` test and add URL encoded POST test
19 changes: 18 additions & 1 deletion apps/tests/src/form-data.ts
Expand Up @@ -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',
});
Expand Down
6 changes: 5 additions & 1 deletion apps/tests/src/url.ts
Expand Up @@ -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']);
Expand All @@ -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']);
});

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/fetch/body.ts
Expand Up @@ -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='))
Expand Down

0 comments on commit 7b6f8b5

Please sign in to comment.