request-init-stream.any.js (5216B)
1 // META: global=window,worker 2 3 "use strict"; 4 5 const duplex = "half"; 6 const method = "POST"; 7 8 test(() => { 9 const body = new ReadableStream(); 10 const request = new Request("...", { method, body, duplex }); 11 assert_equals(request.body, body); 12 }, "Constructing a Request with a stream holds the original object."); 13 14 test((t) => { 15 const body = new ReadableStream(); 16 body.getReader(); 17 assert_throws_js(TypeError, 18 () => new Request("...", { method, body, duplex })); 19 }, "Constructing a Request with a stream on which getReader() is called"); 20 21 test((t) => { 22 const body = new ReadableStream(); 23 body.getReader().read(); 24 assert_throws_js(TypeError, 25 () => new Request("...", { method, body, duplex })); 26 }, "Constructing a Request with a stream on which read() is called"); 27 28 promise_test(async (t) => { 29 const body = new ReadableStream({ pull: c => c.enqueue(new Uint8Array()) }); 30 const reader = body.getReader(); 31 await reader.read(); 32 reader.releaseLock(); 33 assert_throws_js(TypeError, 34 () => new Request("...", { method, body, duplex })); 35 }, "Constructing a Request with a stream on which read() and releaseLock() are called"); 36 37 test((t) => { 38 const request = new Request("...", { method: "POST", body: "..." }); 39 request.body.getReader(); 40 assert_throws_js(TypeError, () => new Request(request)); 41 // This doesn't throw. 42 new Request(request, { body: "..." }); 43 }, "Constructing a Request with a Request on which body.getReader() is called"); 44 45 test((t) => { 46 const request = new Request("...", { method: "POST", body: "..." }); 47 request.body.getReader().read(); 48 assert_throws_js(TypeError, () => new Request(request)); 49 // This doesn't throw. 50 new Request(request, { body: "..." }); 51 }, "Constructing a Request with a Request on which body.getReader().read() is called"); 52 53 promise_test(async (t) => { 54 const request = new Request("...", { method: "POST", body: "..." }); 55 const reader = request.body.getReader(); 56 await reader.read(); 57 reader.releaseLock(); 58 assert_throws_js(TypeError, () => new Request(request)); 59 // This doesn't throw. 60 new Request(request, { body: "..." }); 61 }, "Constructing a Request with a Request on which read() and releaseLock() are called"); 62 63 test((t) => { 64 new Request("...", { method, body: null }); 65 }, "It is OK to omit .duplex when the body is null."); 66 67 test((t) => { 68 new Request("...", { method, body: "..." }); 69 }, "It is OK to omit .duplex when the body is a string."); 70 71 test((t) => { 72 new Request("...", { method, body: new Uint8Array(3) }); 73 }, "It is OK to omit .duplex when the body is a Uint8Array."); 74 75 test((t) => { 76 new Request("...", { method, body: new Blob([]) }); 77 }, "It is OK to omit .duplex when the body is a Blob."); 78 79 test((t) => { 80 const body = new ReadableStream(); 81 assert_throws_js(TypeError, 82 () => new Request("...", { method, body })); 83 }, "It is error to omit .duplex when the body is a ReadableStream."); 84 85 test((t) => { 86 new Request("...", { method, body: null, duplex: "half" }); 87 }, "It is OK to set .duplex = 'half' when the body is null."); 88 89 test((t) => { 90 new Request("...", { method, body: "...", duplex: "half" }); 91 }, "It is OK to set .duplex = 'half' when the body is a string."); 92 93 test((t) => { 94 new Request("...", { method, body: new Uint8Array(3), duplex: "half" }); 95 }, "It is OK to set .duplex = 'half' when the body is a Uint8Array."); 96 97 test((t) => { 98 new Request("...", { method, body: new Blob([]), duplex: "half" }); 99 }, "It is OK to set .duplex = 'half' when the body is a Blob."); 100 101 test((t) => { 102 const body = new ReadableStream(); 103 new Request("...", { method, body, duplex: "half" }); 104 }, "It is OK to set .duplex = 'half' when the body is a ReadableStream."); 105 106 test((t) => { 107 const body = null; 108 const duplex = "full"; 109 assert_throws_js(TypeError, 110 () => new Request("...", { method, body, duplex })); 111 }, "It is error to set .duplex = 'full' when the body is null."); 112 113 test((t) => { 114 const body = "..."; 115 const duplex = "full"; 116 assert_throws_js(TypeError, 117 () => new Request("...", { method, body, duplex })); 118 }, "It is error to set .duplex = 'full' when the body is a string."); 119 120 test((t) => { 121 const body = new Uint8Array(3); 122 const duplex = "full"; 123 assert_throws_js(TypeError, 124 () => new Request("...", { method, body, duplex })); 125 }, "It is error to set .duplex = 'full' when the body is a Uint8Array."); 126 127 test((t) => { 128 const body = new Blob([]); 129 const duplex = "full"; 130 assert_throws_js(TypeError, 131 () => new Request("...", { method, body, duplex })); 132 }, "It is error to set .duplex = 'full' when the body is a Blob."); 133 134 test((t) => { 135 const body = new ReadableStream(); 136 const duplex = "full"; 137 assert_throws_js(TypeError, 138 () => new Request("...", { method, body, duplex })); 139 }, "It is error to set .duplex = 'full' when the body is a ReadableStream."); 140 141 test((t) => { 142 const body = new ReadableStream(); 143 const duplex = "half"; 144 const req1 = new Request("...", { method, body, duplex }); 145 const req2 = new Request(req1); 146 }, "It is OK to omit duplex when init.body is not given and input.body is given.");