cloudflare/cloudflare-typescript
Publicmirrored fromhttps://github.com/cloudflare/cloudflare-typescriptAvailable
tests/form.test.ts
65lines · modecode
| 1 | import { multipartFormRequestOptions, createForm } from 'cloudflare/core'; |
| 2 | import { Blob } from 'cloudflare/_shims/index'; |
| 3 | import { toFile } from 'cloudflare'; |
| 4 | |
| 5 | describe('form data validation', () => { |
| 6 | test('valid values do not error', async () => { |
| 7 | await multipartFormRequestOptions({ |
| 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 | }); |
| 17 | |
| 18 | test('null', async () => { |
| 19 | await expect(() => |
| 20 | multipartFormRequestOptions({ |
| 21 | body: { |
| 22 | null: null, |
| 23 | }, |
| 24 | }), |
| 25 | ).rejects.toThrow(TypeError); |
| 26 | }); |
| 27 | |
| 28 | test('undefined is stripped', async () => { |
| 29 | const form = await createForm({ |
| 30 | foo: undefined, |
| 31 | bar: 'baz', |
| 32 | }); |
| 33 | expect(form.has('foo')).toBe(false); |
| 34 | expect(form.get('bar')).toBe('baz'); |
| 35 | }); |
| 36 | |
| 37 | test('nested undefined property is stripped', async () => { |
| 38 | const form = await createForm({ |
| 39 | bar: { |
| 40 | baz: undefined, |
| 41 | }, |
| 42 | }); |
| 43 | expect(Array.from(form.entries())).toEqual([]); |
| 44 | |
| 45 | const form2 = await createForm({ |
| 46 | bar: { |
| 47 | foo: 'string', |
| 48 | baz: undefined, |
| 49 | }, |
| 50 | }); |
| 51 | expect(Array.from(form2.entries())).toEqual([['bar[foo]', 'string']]); |
| 52 | }); |
| 53 | |
| 54 | test('nested undefined array item is stripped', async () => { |
| 55 | const form = await createForm({ |
| 56 | bar: [undefined, undefined], |
| 57 | }); |
| 58 | expect(Array.from(form.entries())).toEqual([]); |
| 59 | |
| 60 | const form2 = await createForm({ |
| 61 | bar: [undefined, 'foo'], |
| 62 | }); |
| 63 | expect(Array.from(form2.entries())).toEqual([['bar[]', 'foo']]); |
| 64 | }); |
| 65 | }); |
| 66 | |