File
Blob: src/workerd/api/tests/form-data-legacy-test.js
| 1 | // Copyright (c) 2023 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 | export const sendFilesInFormdata = { |
| 5 | async test() { |
| 6 | const INPUT = `--2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a |
| 7 | Content-Disposition: form-data; name="foo"; filename="foo.txt" |
| 8 | Content-Type: application/octet-stream |
| 9 | |
| 10 | foo-content |
| 11 | --2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a |
| 12 | Content-Disposition: form-data; name="bar"; filename="bar-renamed.txt" |
| 13 | Content-Type: application/octet-stream |
| 14 | |
| 15 | bar1-content |
| 16 | --2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a |
| 17 | Content-Disposition: form-data; name="bar"; filename="bar2.txt" |
| 18 | Content-Type: text/bary |
| 19 | |
| 20 | bar2-content |
| 21 | --2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a |
| 22 | Content-Disposition: form-data; name="baz"; filename="baz" |
| 23 | Content-Type: text/bazzy |
| 24 | |
| 25 | baz-content |
| 26 | --2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a |
| 27 | Content-Disposition: form-data; name="qux"; filename="qux%0A%22\\.txt" |
| 28 | Content-Type: application/octet-stream |
| 29 | |
| 30 | qux-content |
| 31 | --2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a-- |
| 32 | `; |
| 33 | |
| 34 | const req = new Request('https://example.org', { |
| 35 | method: 'POST', |
| 36 | body: INPUT, |
| 37 | headers: { |
| 38 | 'content-type': |
| 39 | 'multipart/form-data;boundary=2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a', |
| 40 | }, |
| 41 | }); |
| 42 | |
| 43 | const form = await req.formData(); |
| 44 | |
| 45 | form.set('foo', new File(['foo-content'], 'foo.txt')); |
| 46 | form.append( |
| 47 | 'bar', |
| 48 | new File(['bar1-content'], 'bar1.txt'), |
| 49 | 'bar-renamed.txt' |
| 50 | ); |
| 51 | form.append( |
| 52 | 'bar', |
| 53 | new File(['bar2-content'], 'bar2.txt', { type: 'text/bary' }) |
| 54 | ); |
| 55 | form.append('baz', new Blob(['baz-content'], { type: 'text/bazzy' })); |
| 56 | form.set('qux', new Blob(['qux-content']), 'qux\n"\\.txt'); |
| 57 | |
| 58 | { |
| 59 | let resp = new Response(form); |
| 60 | let text = await resp.text(); |
| 61 | let roundtrip = await new Response(text, { |
| 62 | headers: resp.headers, |
| 63 | }).formData(); |
| 64 | if (roundtrip.get('foo') != 'foo-content') { |
| 65 | throw new Error( |
| 66 | 'expected round-trip turns into string (wrong, but backwards-compatible)' |
| 67 | ); |
| 68 | } |
| 69 | } |
| 70 | }, |
| 71 | }; |