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