OLD | NEW |
1 <!doctype html> | 1 <!doctype html> |
2 <html> | 2 <html> |
3 <head> | 3 <head> |
4 <meta charset="utf-8"> | 4 <meta charset="utf-8"> |
5 <title>Response consume empty bodies</title> | 5 <title>Response consume empty bodies</title> |
6 <meta name="help" href="https://fetch.spec.whatwg.org/#response"> | 6 <meta name="help" href="https://fetch.spec.whatwg.org/#response"> |
7 <meta name="help" href="https://fetch.spec.whatwg.org/#body-mixin"> | 7 <meta name="help" href="https://fetch.spec.whatwg.org/#body-mixin"> |
8 <meta name="author" title="Canon Research France" href="https://www.crf.cano
n.fr"> | 8 <meta name="author" title="Canon Research France" href="https://www.crf.cano
n.fr"> |
9 <script src="/resources/testharness.js"></script> | 9 <script src="/resources/testharness.js"></script> |
10 <script src="/resources/testharnessreport.js"></script> | 10 <script src="/resources/testharnessreport.js"></script> |
11 </head> | 11 </head> |
12 <body> | 12 <body> |
13 <script> | 13 <script> |
14 function checkBodyText(response) { | 14 function checkBodyText(test, response) { |
15 return response.text().then(function(bodyAsText) { | 15 return response.text().then(function(bodyAsText) { |
16 assert_equals(bodyAsText, "", "Resolved value should be empty"); | 16 assert_equals(bodyAsText, "", "Resolved value should be empty"); |
17 assert_false(response.bodyUsed); | 17 assert_false(response.bodyUsed); |
18 }); | 18 }); |
19 } | 19 } |
20 | 20 |
21 function checkBodyBlob(response) { | 21 function checkBodyBlob(test, response) { |
22 return response.blob().then(function(bodyAsBlob) { | 22 return response.blob().then(function(bodyAsBlob) { |
23 var promise = new Promise(function(resolve, reject) { | 23 var promise = new Promise(function(resolve, reject) { |
24 var reader = new FileReader(); | 24 var reader = new FileReader(); |
25 reader.onload = function(evt) { | 25 reader.onload = function(evt) { |
26 resolve(reader.result) | 26 resolve(reader.result) |
27 }; | 27 }; |
28 reader.onerror = function() { | 28 reader.onerror = function() { |
29 reject("Blob's reader failed"); | 29 reject("Blob's reader failed"); |
30 }; | 30 }; |
31 reader.readAsText(bodyAsBlob); | 31 reader.readAsText(bodyAsBlob); |
32 }); | 32 }); |
33 return promise.then(function(body) { | 33 return promise.then(function(body) { |
34 assert_equals(body, "", "Resolved value should be empty"); | 34 assert_equals(body, "", "Resolved value should be empty"); |
35 assert_false(response.bodyUsed); | 35 assert_false(response.bodyUsed); |
36 }); | 36 }); |
37 }); | 37 }); |
38 } | 38 } |
39 | 39 |
40 function checkBodyArrayBuffer(response) { | 40 function checkBodyArrayBuffer(test, response) { |
41 return response.arrayBuffer().then(function(bodyAsArrayBuffer) { | 41 return response.arrayBuffer().then(function(bodyAsArrayBuffer) { |
42 assert_equals(bodyAsArrayBuffer.byteLength, 0, "Resolved value should be
empty"); | 42 assert_equals(bodyAsArrayBuffer.byteLength, 0, "Resolved value should be
empty"); |
43 assert_false(response.bodyUsed); | 43 assert_false(response.bodyUsed); |
44 }); | 44 }); |
45 } | 45 } |
46 | 46 |
47 function checkBodyJSON(response) { | 47 function checkBodyJSON(test, response) { |
48 return response.json().then( | 48 return response.json().then( |
49 function(bodyAsJSON) { | 49 function(bodyAsJSON) { |
50 assert_unreached("JSON parsing should fail"); | 50 assert_unreached("JSON parsing should fail"); |
51 }, | 51 }, |
52 function() { | 52 function() { |
53 assert_false(response.bodyUsed); | 53 assert_false(response.bodyUsed); |
54 }); | 54 }); |
55 } | 55 } |
56 | 56 |
57 function checkBodyFormData(response) { | 57 function checkBodyFormData(test, response) { |
58 return response.formData().then(function(bodyAsFormData) { | 58 return response.formData().then(function(bodyAsFormData) { |
59 assert_true(bodyAsFormData instanceof FormData, "Should receive a FormDa
ta"); | 59 assert_true(bodyAsFormData instanceof FormData, "Should receive a FormDa
ta"); |
60 assert_false(response.bodyUsed); | 60 assert_false(response.bodyUsed); |
61 }); | 61 }); |
62 } | 62 } |
63 | 63 |
64 function checkResponseWithNoBody(bodyType, checkFunction) { | 64 function checkBodyFormDataError(test, response) { |
| 65 return promise_rejects(test, new TypeError(), response.formData()).then(fu
nction() { |
| 66 assert_false(response.bodyUsed); |
| 67 }); |
| 68 } |
| 69 |
| 70 function checkResponseWithNoBody(bodyType, checkFunction, headers = []) { |
65 promise_test(function(test) { | 71 promise_test(function(test) { |
66 var response = new Response(); | 72 var response = new Response(undefined, { "headers": headers }); |
67 assert_false(response.bodyUsed); | 73 assert_false(response.bodyUsed); |
68 return checkFunction(response); | 74 return checkFunction(test, response); |
69 }, "Consume response's body as " + bodyType); | 75 }, "Consume response's body as " + bodyType); |
70 } | 76 } |
71 | 77 |
72 var formData = new FormData(); | |
73 checkResponseWithNoBody("text", checkBodyText); | 78 checkResponseWithNoBody("text", checkBodyText); |
74 checkResponseWithNoBody("blob", checkBodyBlob); | 79 checkResponseWithNoBody("blob", checkBodyBlob); |
75 checkResponseWithNoBody("arrayBuffer", checkBodyArrayBuffer); | 80 checkResponseWithNoBody("arrayBuffer", checkBodyArrayBuffer); |
76 checkResponseWithNoBody("json", checkBodyJSON); | 81 checkResponseWithNoBody("json (error case)", checkBodyJSON); |
77 checkResponseWithNoBody("formData", checkBodyFormData); | 82 checkResponseWithNoBody("formData with correct multipart type (error case)",
checkBodyFormDataError, [["Content-Type", 'multipart/form-data; boundary="bound
ary"']]); |
| 83 checkResponseWithNoBody("formData with correct urlencoded type", checkBodyFo
rmData, [["Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"]]); |
| 84 checkResponseWithNoBody("formData without correct type (error case)", checkB
odyFormDataError); |
78 | 85 |
79 function checkResponseWithEmptyBody(bodyType, body, asText) { | 86 function checkResponseWithEmptyBody(bodyType, body, asText) { |
80 promise_test(function(test) { | 87 promise_test(function(test) { |
81 var response = new Response(body); | 88 var response = new Response(body); |
82 assert_false(response.bodyUsed, "bodyUsed is false at init"); | 89 assert_false(response.bodyUsed, "bodyUsed is false at init"); |
83 if (asText) { | 90 if (asText) { |
84 return response.text().then(function(bodyAsString) { | 91 return response.text().then(function(bodyAsString) { |
85 assert_equals(bodyAsString.length, 0, "Resolved value should be empt
y"); | 92 assert_equals(bodyAsString.length, 0, "Resolved value should be empt
y"); |
86 assert_true(response.bodyUsed, "bodyUsed is true after being consume
d"); | 93 assert_true(response.bodyUsed, "bodyUsed is true after being consume
d"); |
87 }); | 94 }); |
88 } | 95 } |
89 return response.arrayBuffer().then(function(bodyAsArrayBuffer) { | 96 return response.arrayBuffer().then(function(bodyAsArrayBuffer) { |
90 assert_equals(bodyAsArrayBuffer.byteLength, 0, "Resolved value should
be empty"); | 97 assert_equals(bodyAsArrayBuffer.byteLength, 0, "Resolved value should
be empty"); |
91 assert_true(response.bodyUsed, "bodyUsed is true after being consumed"
); | 98 assert_true(response.bodyUsed, "bodyUsed is true after being consumed"
); |
92 }); | 99 }); |
93 }, "Consume empty " + bodyType + " response body as " + (asText ? "text" :
"arrayBuffer")); | 100 }, "Consume empty " + bodyType + " response body as " + (asText ? "text" :
"arrayBuffer")); |
94 } | 101 } |
95 | 102 |
96 checkResponseWithEmptyBody("blob", new Blob([], { "type" : "text/plain" }),
false); | 103 checkResponseWithEmptyBody("blob", new Blob([], { "type" : "text/plain" }),
false); |
97 checkResponseWithEmptyBody("text", "", false); | 104 checkResponseWithEmptyBody("text", "", false); |
98 checkResponseWithEmptyBody("blob", new Blob([], { "type" : "text/plain" }),
true); | 105 checkResponseWithEmptyBody("blob", new Blob([], { "type" : "text/plain" }),
true); |
99 checkResponseWithEmptyBody("text", "", true); | 106 checkResponseWithEmptyBody("text", "", true); |
100 checkResponseWithEmptyBody("URLSearchParams", new URLSearchParams(""), true)
; | 107 checkResponseWithEmptyBody("URLSearchParams", new URLSearchParams(""), true)
; |
101 checkResponseWithEmptyBody("FormData", new FormData(), true); | 108 checkResponseWithEmptyBody("FormData", new FormData(), true); |
102 checkResponseWithEmptyBody("ArrayBuffer", new ArrayBuffer(), true); | 109 checkResponseWithEmptyBody("ArrayBuffer", new ArrayBuffer(), true); |
103 </script> | 110 </script> |
104 </body> | 111 </body> |
105 </html> | 112 </html> |
OLD | NEW |