|
5 | 5 |
|
6 | 6 | const duplex = "half";
|
7 | 7 |
|
8 |
| -function testUpload(desc, url, method, createBody, expectedBody) { |
| 8 | +async function assertUpload(url, method, createBody, expectedBody) { |
9 | 9 | const requestInit = {method};
|
| 10 | + const body = createBody(); |
| 11 | + if (body) { |
| 12 | + requestInit["body"] = body; |
| 13 | + requestInit.duplex = "half"; |
| 14 | + } |
| 15 | + const resp = await fetch(url, requestInit); |
| 16 | + const text = await resp.text(); |
| 17 | + assert_equals(text, expectedBody); |
| 18 | +} |
| 19 | + |
| 20 | +function testUpload(desc, url, method, createBody, expectedBody) { |
10 | 21 | promise_test(async () => {
|
11 |
| - const body = createBody(); |
12 |
| - if (body) { |
13 |
| - requestInit["body"] = body; |
14 |
| - requestInit.duplex = "half"; |
15 |
| - } |
16 |
| - const resp = await fetch(url, requestInit); |
17 |
| - const text = await resp.text(); |
18 |
| - assert_equals(text, expectedBody); |
| 22 | + await assertUpload(url, method, createBody, expectedBody); |
19 | 23 | }, desc);
|
20 | 24 | }
|
21 | 25 |
|
@@ -98,6 +102,63 @@ promise_test(async (test) => {
|
98 | 102 | assert_equals(await response.text(), 'test', `Response has correct body`);
|
99 | 103 | }, "Feature detect for POST with ReadableStream, using request object");
|
100 | 104 |
|
| 105 | +test(() => { |
| 106 | + let duplexAccessed = false; |
| 107 | + |
| 108 | + const request = new Request("", { |
| 109 | + body: new ReadableStream(), |
| 110 | + method: "POST", |
| 111 | + get duplex() { |
| 112 | + duplexAccessed = true; |
| 113 | + return "half"; |
| 114 | + }, |
| 115 | + }); |
| 116 | + |
| 117 | + assert_equals( |
| 118 | + request.headers.get("Content-Type"), |
| 119 | + null, |
| 120 | + `Request should not have a content-type set` |
| 121 | + ); |
| 122 | + assert_true(duplexAccessed, `duplex dictionary property should be accessed`); |
| 123 | +}, "Synchronous feature detect"); |
| 124 | + |
| 125 | +// The asserts the synchronousFeatureDetect isn't broken by a partial implementation. |
| 126 | +// An earlier feature detect was broken by Safari implementing streaming bodies as part of Request, |
| 127 | +// but it failed when passed to fetch(). |
| 128 | +// This tests ensures that UAs must not implement RequestInit.duplex and streaming request bodies without also implementing the fetch() parts. |
| 129 | +promise_test(async () => { |
| 130 | + let duplexAccessed = false; |
| 131 | + |
| 132 | + const request = new Request("", { |
| 133 | + body: new ReadableStream(), |
| 134 | + method: "POST", |
| 135 | + get duplex() { |
| 136 | + duplexAccessed = true; |
| 137 | + return "half"; |
| 138 | + }, |
| 139 | + }); |
| 140 | + |
| 141 | + const supported = |
| 142 | + request.headers.get("Content-Type") === null && duplexAccessed; |
| 143 | + |
| 144 | + // If the feature detect fails, assume the browser is being truthful (other tests pick up broken cases here) |
| 145 | + if (!supported) return false; |
| 146 | + |
| 147 | + await assertUpload( |
| 148 | + url, |
| 149 | + "POST", |
| 150 | + () => |
| 151 | + new ReadableStream({ |
| 152 | + start: (controller) => { |
| 153 | + const encoder = new TextEncoder(); |
| 154 | + controller.enqueue(encoder.encode("Test")); |
| 155 | + controller.close(); |
| 156 | + }, |
| 157 | + }), |
| 158 | + "Test" |
| 159 | + ); |
| 160 | +}, "Synchronous feature detect fails if feature unsupported"); |
| 161 | + |
101 | 162 | promise_test(async (t) => {
|
102 | 163 | const body = createStream(["hello"]);
|
103 | 164 | const method = "POST";
|
|
0 commit comments