|
2 | 2 |
|
3 | 3 | "use strict";
|
4 | 4 |
|
| 5 | +const duplex = "half"; |
| 6 | +const method = "POST"; |
| 7 | + |
5 | 8 | test(() => {
|
6 |
| - const stream = new ReadableStream(); |
7 |
| - const request = new Request("...", { method:"POST", body: stream }); |
| 9 | + const body = new ReadableStream({}); |
| 10 | + const request = new Request("...", { method, body, duplex }); |
8 | 11 | assert_equals(request.body, stream);
|
9 | 12 | }, "Constructing a Request with a stream holds the original object.");
|
10 | 13 |
|
11 |
| -async function assert_request(test, input, init) { |
12 |
| - assert_throws_js(TypeError, () => new Request(input, init), "new Request()"); |
13 |
| - await promise_rejects_js(test, TypeError, fetch(input, init), "fetch()"); |
14 |
| -} |
15 |
| - |
16 | 14 | promise_test(async (t) => {
|
17 |
| - const stream = new ReadableStream(); |
18 |
| - stream.getReader(); |
19 |
| - await assert_request(t, "...", { method:"POST", body: stream }); |
| 15 | + const body = new ReadableStream(); |
| 16 | + body.getReader(); |
| 17 | + assert_throws_js(TypeError, |
| 18 | + () => new Request("...", { method, body, duplex })); |
20 | 19 | }, "Constructing a Request with a stream on which getReader() is called");
|
21 | 20 |
|
22 | 21 | promise_test(async (t) => {
|
23 |
| - const stream = new ReadableStream(); |
| 22 | + const body = new ReadableStream({}); |
24 | 23 | stream.getReader().read();
|
25 |
| - await assert_request(t, "...", { method:"POST", body: stream }); |
| 24 | + assert_throws_js(TypeError, |
| 25 | + () => new Request("...", { method, body, duplex })); |
26 | 26 | }, "Constructing a Request with a stream on which read() is called");
|
27 | 27 |
|
28 | 28 | promise_test(async (t) => {
|
29 |
| - const stream = new ReadableStream({ pull: c => c.enqueue(new Uint8Array()) }), |
30 |
| - reader = stream.getReader(); |
| 29 | + const body = new ReadableStream({ pull: c => c.enqueue(new Uint8Array()) }); |
| 30 | + const reader = body.getReader(); |
31 | 31 | await reader.read();
|
32 | 32 | reader.releaseLock();
|
33 |
| - await assert_request(t, "...", { method:"POST", body: stream }); |
| 33 | + assert_throws_js(TypeError, |
| 34 | + () => "...", { method, body, duplex }); |
34 | 35 | }, "Constructing a Request with a stream on which read() and releaseLock() are called");
|
35 | 36 |
|
36 | 37 | promise_test(async (t) => {
|
37 | 38 | const request = new Request("...", { method: "POST", body: "..." });
|
38 | 39 | request.body.getReader();
|
39 |
| - await assert_request(t, request); |
40 |
| - assert_class_string(new Request(request, { body: "..." }), "Request"); |
| 40 | + assert_throws_js(TypeError, () => new Request(request)); |
| 41 | + // This doesn't throw. |
| 42 | + const r = new Request(request, { body: "..." }); |
41 | 43 | }, "Constructing a Request with a Request on which body.getReader() is called");
|
42 | 44 |
|
43 | 45 | promise_test(async (t) => {
|
44 | 46 | const request = new Request("...", { method: "POST", body: "..." });
|
45 | 47 | request.body.getReader().read();
|
46 |
| - await assert_request(t, request); |
47 |
| - assert_class_string(new Request(request, { body: "..." }), "Request"); |
| 48 | + assert_throws_js(TypeError, () => new Request(request)); |
| 49 | + // This doesn't throw. |
| 50 | + const r = new Request(request, { body: "..." }); |
48 | 51 | }, "Constructing a Request with a Request on which body.getReader().read() is called");
|
49 | 52 |
|
50 | 53 | promise_test(async (t) => {
|
51 |
| - const request = new Request("...", { method: "POST", body: "..." }), |
52 |
| - reader = request.body.getReader(); |
| 54 | + const request = new Request("...", { method: "POST", body: "..." }); |
| 55 | + const reader = request.body.getReader(); |
53 | 56 | await reader.read();
|
54 | 57 | reader.releaseLock();
|
55 |
| - await assert_request(t, request); |
56 |
| - assert_class_string(new Request(request, { body: "..." }), "Request"); |
| 58 | + assert_throws_js(TypeError, () => new Request(request)); |
| 59 | + // This doesn't throw. |
| 60 | + const r = new Request(request, { body: "..." }); |
57 | 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."); |
| 147 | + |
0 commit comments