cloudflare/cloudflare-typescript
Publicmirrored fromhttps://github.com/cloudflare/cloudflare-typescriptAvailable
tests/form.test.ts
85lines · modecode
| 1 | import { multipartFormRequestOptions, createForm } from 'cloudflare/internal/uploads'; |
| 2 | import { toFile } from 'cloudflare/core/uploads'; |
| 3 | |
| 4 | describe('form data validation', () => { |
| 5 | test('valid values do not error', async () => { |
| 6 | await multipartFormRequestOptions( |
| 7 | { |
| 8 | body: { |
| 9 | foo: 'foo', |
| 10 | string: 1, |
| 11 | bool: true, |
| 12 | file: await toFile(Buffer.from('some-content')), |
| 13 | blob: new Blob(['Some content'], { type: 'text/plain' }), |
| 14 | }, |
| 15 | }, |
| 16 | fetch, |
| 17 | ); |
| 18 | }); |
| 19 | |
| 20 | test('null', async () => { |
| 21 | await expect(() => |
| 22 | multipartFormRequestOptions( |
| 23 | { |
| 24 | body: { |
| 25 | null: null, |
| 26 | }, |
| 27 | }, |
| 28 | fetch, |
| 29 | ), |
| 30 | ).rejects.toThrow(TypeError); |
| 31 | }); |
| 32 | |
| 33 | test('undefined is stripped', async () => { |
| 34 | const form = await createForm( |
| 35 | { |
| 36 | foo: undefined, |
| 37 | bar: 'baz', |
| 38 | }, |
| 39 | fetch, |
| 40 | ); |
| 41 | expect(form.has('foo')).toBe(false); |
| 42 | expect(form.get('bar')).toBe('baz'); |
| 43 | }); |
| 44 | |
| 45 | test('nested undefined property is stripped', async () => { |
| 46 | const form = await createForm( |
| 47 | { |
| 48 | bar: { |
| 49 | baz: undefined, |
| 50 | }, |
| 51 | }, |
| 52 | fetch, |
| 53 | ); |
| 54 | expect(Array.from(form.entries())).toEqual([]); |
| 55 | |
| 56 | const form2 = await createForm( |
| 57 | { |
| 58 | bar: { |
| 59 | foo: 'string', |
| 60 | baz: undefined, |
| 61 | }, |
| 62 | }, |
| 63 | fetch, |
| 64 | ); |
| 65 | expect(Array.from(form2.entries())).toEqual([['bar[foo]', 'string']]); |
| 66 | }); |
| 67 | |
| 68 | test('nested undefined array item is stripped', async () => { |
| 69 | const form = await createForm( |
| 70 | { |
| 71 | bar: [undefined, undefined], |
| 72 | }, |
| 73 | fetch, |
| 74 | ); |
| 75 | expect(Array.from(form.entries())).toEqual([]); |
| 76 | |
| 77 | const form2 = await createForm( |
| 78 | { |
| 79 | bar: [undefined, 'foo'], |
| 80 | }, |
| 81 | fetch, |
| 82 | ); |
| 83 | expect(Array.from(form2.entries())).toEqual([['bar[]', 'foo']]); |
| 84 | }); |
| 85 | }); |
| 86 | |