|
| 1 | +// Changing the body after it have been passed to Response/Request |
| 2 | +// should not change the outcome of the consumed body |
| 3 | + |
| 4 | +const url = 'http://a'; |
| 5 | +const method = 'post'; |
| 6 | + |
| 7 | +promise_test(async t => { |
| 8 | + const body = new FormData(); |
| 9 | + body.set('a', '1'); |
| 10 | + const res = new Response(body); |
| 11 | + const req = new Request(url, { method, body }); |
| 12 | + body.set('a', '2'); |
| 13 | + assert_true((await res.formData()).get('a') === '1'); |
| 14 | + assert_true((await req.formData()).get('a') === '1'); |
| 15 | +}, 'FormData is cloned'); |
| 16 | + |
| 17 | +promise_test(async t => { |
| 18 | + const body = new URLSearchParams({a: '1'}); |
| 19 | + const res = new Response(body); |
| 20 | + const req = new Request(url, { method, body }); |
| 21 | + body.set('a', '2'); |
| 22 | + assert_true((await res.formData()).get('a') === '1'); |
| 23 | + assert_true((await req.formData()).get('a') === '1'); |
| 24 | +}, 'URLSearchParams is cloned'); |
| 25 | + |
| 26 | +promise_test(async t => { |
| 27 | + const body = new Uint8Array([97]); // a |
| 28 | + const res = new Response(body); |
| 29 | + const req = new Request(url, { method, body }); |
| 30 | + body[0] = 98; // b |
| 31 | + assert_true(await res.text() === 'a'); |
| 32 | + assert_true(await req.text() === 'a'); |
| 33 | +}, 'TypedArray is cloned'); |
| 34 | + |
| 35 | +promise_test(async t => { |
| 36 | + const body = new Uint8Array([97]); // a |
| 37 | + const res = new Response(body.buffer); |
| 38 | + const req = new Request(url, { method, body: body.buffer }); |
| 39 | + body[0] = 98; // b |
| 40 | + assert_true(await res.text() === 'a'); |
| 41 | + assert_true(await req.text() === 'a'); |
| 42 | +}, 'ArrayBuffer is cloned'); |
| 43 | + |
| 44 | +promise_test(async t => { |
| 45 | + const body = new Blob(['a']); |
| 46 | + const res = new Response(body); |
| 47 | + const req = new Request(url, { method, body }); |
| 48 | + assert_true(await res.blob() !== body); |
| 49 | + assert_true(await req.blob() !== body); |
| 50 | +}, 'Blob is cloned'); |
0 commit comments