File
Blob: src/workerd/api/tests/form-data-test-ts.ts
| 1 | // Copyright (c) 2026 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | import { strictEqual } from 'node:assert'; |
| 6 | |
| 7 | // https://github.com/cloudflare/workerd/issues/5934 |
| 8 | export const formDataUnionTypeOverloads = { |
| 9 | test() { |
| 10 | const formData = new FormData(); |
| 11 | |
| 12 | formData.append('stringKey', 'stringValue' as string | Blob); |
| 13 | strictEqual(formData.get('stringKey'), 'stringValue'); |
| 14 | |
| 15 | // We explicitly use string | Blob to test types. |
| 16 | const blob: string | Blob = new Blob(['blob content'], { |
| 17 | type: 'text/plain', |
| 18 | }); |
| 19 | formData.append('blobKey', blob); |
| 20 | strictEqual(formData.get('blobKey') instanceof File, true); |
| 21 | |
| 22 | const value: string | Blob = 'setValue'; |
| 23 | formData.set('setStringKey', value); |
| 24 | strictEqual(formData.get('setStringKey'), 'setValue'); |
| 25 | |
| 26 | formData.set('setBlobKey', blob); |
| 27 | strictEqual(formData.get('setBlobKey') instanceof File, true); |
| 28 | |
| 29 | const values: Array<{ key: string; value: string | Blob }> = [ |
| 30 | { key: 'key1', value: 'string value' }, |
| 31 | { key: 'key2', value: new Blob(['blob']) }, |
| 32 | ]; |
| 33 | |
| 34 | const formData2 = new FormData(); |
| 35 | for (const { key, value } of values) { |
| 36 | if (typeof value === 'string' || value instanceof Blob) { |
| 37 | formData2.append(key, value); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | strictEqual(formData2.get('key1'), 'string value'); |
| 42 | strictEqual(formData2.get('key2') instanceof File, true); |
| 43 | }, |
| 44 | }; |