OLD | NEW |
(Empty) | |
| 1 <!doctype html> |
| 2 <html> |
| 3 <head> |
| 4 <meta charset="utf-8"> |
| 5 <title>Request consume empty bodies</title> |
| 6 <meta name="help" href="https://fetch.spec.whatwg.org/#request"> |
| 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(request) { |
| 15 return request.text().then(function(bodyAsText) { |
| 16 assert_equals(bodyAsText, "", "Resolved value should be empty"); |
| 17 assert_false(request.bodyUsed); |
| 18 }); |
| 19 } |
| 20 |
| 21 function checkBodyBlob(request) { |
| 22 return request.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(request.bodyUsed); |
| 36 }); |
| 37 }); |
| 38 } |
| 39 |
| 40 function checkBodyArrayBuffer(request) { |
| 41 return request.arrayBuffer().then(function(bodyAsArrayBuffer) { |
| 42 assert_equals(bodyAsArrayBuffer.byteLength, 0, "Resolved value should be
empty"); |
| 43 assert_false(request.bodyUsed); |
| 44 }); |
| 45 } |
| 46 |
| 47 function checkBodyJSON(request) { |
| 48 return request.json().then( |
| 49 function(bodyAsJSON) { |
| 50 assert_unreached("JSON parsing should fail"); |
| 51 }, |
| 52 function() { |
| 53 assert_false(request.bodyUsed); |
| 54 }); |
| 55 } |
| 56 |
| 57 function checkBodyFormData(request) { |
| 58 return request.formData().then(function(bodyAsFormData) { |
| 59 assert_true(bodyAsFormData instanceof FormData, "Should receive a FormDa
ta"); |
| 60 assert_false(request.bodyUsed); |
| 61 }); |
| 62 } |
| 63 |
| 64 function checkRequestWithNoBody(bodyType, checkFunction) { |
| 65 promise_test(function(test) { |
| 66 var request = new Request("", {"method": "POST"}); |
| 67 assert_false(request.bodyUsed); |
| 68 return checkFunction(request); |
| 69 }, "Consume request's body as " + bodyType); |
| 70 } |
| 71 |
| 72 var formData = new FormData(); |
| 73 checkRequestWithNoBody("text", checkBodyText); |
| 74 checkRequestWithNoBody("blob", checkBodyBlob); |
| 75 checkRequestWithNoBody("arrayBuffer", checkBodyArrayBuffer); |
| 76 checkRequestWithNoBody("json", checkBodyJSON); |
| 77 checkRequestWithNoBody("formData", checkBodyFormData); |
| 78 |
| 79 function checkRequestWithEmptyBody(bodyType, body, asText) { |
| 80 promise_test(function(test) { |
| 81 var request = new Request("", {"method": "POST", "body": body}); |
| 82 assert_false(request.bodyUsed, "bodyUsed is false at init"); |
| 83 if (asText) { |
| 84 return request.text().then(function(bodyAsString) { |
| 85 assert_equals(bodyAsString.length, 0, "Resolved value should be empt
y"); |
| 86 assert_true(request.bodyUsed, "bodyUsed is true after being consumed
"); |
| 87 }); |
| 88 } |
| 89 return request.arrayBuffer().then(function(bodyAsArrayBuffer) { |
| 90 assert_equals(bodyAsArrayBuffer.byteLength, 0, "Resolved value should
be empty"); |
| 91 assert_true(request.bodyUsed, "bodyUsed is true after being consumed")
; |
| 92 }); |
| 93 }, "Consume empty " + bodyType + " request body as " + (asText ? "text" :
"arrayBuffer")); |
| 94 } |
| 95 |
| 96 // FIXME: Add BufferSource, FormData and URLSearchParams. |
| 97 checkRequestWithEmptyBody("blob", new Blob([], { "type" : "text/plain" }), f
alse); |
| 98 checkRequestWithEmptyBody("text", "", false); |
| 99 checkRequestWithEmptyBody("blob", new Blob([], { "type" : "text/plain" }), t
rue); |
| 100 checkRequestWithEmptyBody("text", "", true); |
| 101 checkRequestWithEmptyBody("URLSearchParams", new URLSearchParams(""), true); |
| 102 // FIXME: This test assumes that the empty string be returned but it is not
clear whether that is right. See https://github.com/w3c/web-platform-tests/pull/
3950. |
| 103 checkRequestWithEmptyBody("FormData", new FormData(), true); |
| 104 checkRequestWithEmptyBody("ArrayBuffer", new ArrayBuffer(), true); |
| 105 </script> |
| 106 </body> |
| 107 </html> |
OLD | NEW |