response-consume-empty.any.js (3606B)
1 // META: global=window,worker 2 // META: title=Response consume empty bodies 3 4 function checkBodyText(test, response) { 5 return response.text().then(function(bodyAsText) { 6 assert_equals(bodyAsText, "", "Resolved value should be empty"); 7 assert_false(response.bodyUsed); 8 }); 9 } 10 11 async function checkBodyBlob(test, response) { 12 const bodyAsBlob = await response.blob(); 13 const body = await bodyAsBlob.text(); 14 15 assert_equals(body, "", "Resolved value should be empty"); 16 assert_false(response.bodyUsed); 17 } 18 19 function checkBodyArrayBuffer(test, response) { 20 return response.arrayBuffer().then(function(bodyAsArrayBuffer) { 21 assert_equals(bodyAsArrayBuffer.byteLength, 0, "Resolved value should be empty"); 22 assert_false(response.bodyUsed); 23 }); 24 } 25 26 function checkBodyJSON(test, response) { 27 return response.json().then( 28 function(bodyAsJSON) { 29 assert_unreached("JSON parsing should fail"); 30 }, 31 function() { 32 assert_false(response.bodyUsed); 33 }); 34 } 35 36 function checkBodyFormData(test, response) { 37 return response.formData().then(function(bodyAsFormData) { 38 assert_true(bodyAsFormData instanceof FormData, "Should receive a FormData"); 39 assert_false(response.bodyUsed); 40 }); 41 } 42 43 function checkBodyFormDataError(test, response) { 44 return promise_rejects_js(test, TypeError, response.formData()).then(function() { 45 assert_false(response.bodyUsed); 46 }); 47 } 48 49 function checkResponseWithNoBody(bodyType, checkFunction, headers = []) { 50 promise_test(function(test) { 51 var response = new Response(undefined, { "headers": headers }); 52 assert_false(response.bodyUsed); 53 return checkFunction(test, response); 54 }, "Consume response's body as " + bodyType); 55 } 56 57 checkResponseWithNoBody("text", checkBodyText); 58 checkResponseWithNoBody("blob", checkBodyBlob); 59 checkResponseWithNoBody("arrayBuffer", checkBodyArrayBuffer); 60 checkResponseWithNoBody("json (error case)", checkBodyJSON); 61 checkResponseWithNoBody("formData with correct multipart type (error case)", checkBodyFormDataError, [["Content-Type", 'multipart/form-data; boundary="boundary"']]); 62 checkResponseWithNoBody("formData with correct urlencoded type", checkBodyFormData, [["Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"]]); 63 checkResponseWithNoBody("formData without correct type (error case)", checkBodyFormDataError); 64 65 function checkResponseWithEmptyBody(bodyType, body, asText) { 66 promise_test(function(test) { 67 var response = new Response(body); 68 assert_false(response.bodyUsed, "bodyUsed is false at init"); 69 if (asText) { 70 return response.text().then(function(bodyAsString) { 71 assert_equals(bodyAsString.length, 0, "Resolved value should be empty"); 72 assert_true(response.bodyUsed, "bodyUsed is true after being consumed"); 73 }); 74 } 75 return response.arrayBuffer().then(function(bodyAsArrayBuffer) { 76 assert_equals(bodyAsArrayBuffer.byteLength, 0, "Resolved value should be empty"); 77 assert_true(response.bodyUsed, "bodyUsed is true after being consumed"); 78 }); 79 }, "Consume empty " + bodyType + " response body as " + (asText ? "text" : "arrayBuffer")); 80 } 81 82 checkResponseWithEmptyBody("blob", new Blob([], { "type" : "text/plain" }), false); 83 checkResponseWithEmptyBody("text", "", false); 84 checkResponseWithEmptyBody("blob", new Blob([], { "type" : "text/plain" }), true); 85 checkResponseWithEmptyBody("text", "", true); 86 checkResponseWithEmptyBody("URLSearchParams", new URLSearchParams(""), true); 87 checkResponseWithEmptyBody("FormData", new FormData(), true); 88 checkResponseWithEmptyBody("ArrayBuffer", new ArrayBuffer(), true);