File
Blob: src/workerd/api/tests/request-clone-test.js
| 1 | // Copyright (c) 2025 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 | import { deepStrictEqual } from 'node:assert'; |
| 5 | |
| 6 | // This test validates tee'ing value-oriented js-backed readable streams |
| 7 | // as there is no problem with byte-oriented systems. |
| 8 | export const clone = { |
| 9 | async test() { |
| 10 | function createReadableStreamFromString(str) { |
| 11 | const encoder = new TextEncoder(); |
| 12 | const encodedData = encoder.encode(str); |
| 13 | |
| 14 | return new ReadableStream({ |
| 15 | start(controller) { |
| 16 | // Enqueue the data and close the stream |
| 17 | controller.enqueue(encodedData); |
| 18 | controller.close(); |
| 19 | }, |
| 20 | }); |
| 21 | } |
| 22 | |
| 23 | const cr = new Request(new URL('http://localhost/test'), { |
| 24 | method: 'POST', |
| 25 | body: createReadableStreamFromString('Hello stream'), |
| 26 | duplex: 'half', |
| 27 | }); |
| 28 | |
| 29 | const cr2 = cr.clone(); |
| 30 | |
| 31 | deepStrictEqual(await cr.text(), await cr2.text()); |
| 32 | }, |
| 33 | }; |