| <!doctype html> |
| <html> |
| <head> |
| <meta charset="utf-8"> |
| <title>Response consume</title> |
| <meta name="help" href="https://fetch.spec.whatwg.org/#response"> |
| <meta name="help" href="https://fetch.spec.whatwg.org/#body-mixin"> |
| <meta name="author" title="Canon Research France" href="https://www.crf.canon.fr"> |
| <script src="/resources/testharness.js"></script> |
| <script src="/resources/testharnessreport.js"></script> |
| <script src="../resources/utils.js"></script> |
| </head> |
| <body> |
| <script> |
| function responsePromise(body, responseInit) { |
| return new Promise(function(resolve, reject) { |
| resolve(new Response(body, responseInit)); |
| }); |
| } |
| |
| function responseStringToMultipartFormTextData(response, name, value) { |
| assert_true(response.headers.has("Content-Type"), "Response contains Content-Type header"); |
| var boundaryMatches = response.headers.get("Content-Type").match(/;\s*boundary=("?)([^";\s]*)\1/); |
| assert_true(!!boundaryMatches, "Response contains boundary parameter"); |
| return stringToMultipartFormTextData(boundaryMatches[2], name, value); |
| } |
| |
| function streamResponsePromise(streamData, responseInit) { |
| return new Promise(function(resolve, reject) { |
| var stream = new ReadableStream({ |
| start: function(controller) { |
| controller.enqueue(stringToArray(streamData)); |
| controller.close(); |
| } |
| }); |
| resolve(new Response(stream, responseInit)); |
| }); |
| } |
| |
| function stringToMultipartFormTextData(multipartBoundary, name, value) { |
| return ('--' + multipartBoundary + '\r\n' + |
| 'Content-Disposition: form-data;name="' + name + '"\r\n' + |
| '\r\n' + |
| value + '\r\n' + |
| '--' + multipartBoundary + '--\r\n'); |
| } |
| |
| function checkBodyText(test, response, expectedBody) { |
| return response.text().then( function(bodyAsText) { |
| assert_equals(bodyAsText, expectedBody, "Retrieve and verify response's body"); |
| assert_true(response.bodyUsed, "body as text: bodyUsed turned true"); |
| }); |
| } |
| |
| function checkBodyArrayBuffer(test, response, expectedBody) { |
| return response.arrayBuffer().then( function(bodyAsArrayBuffer) { |
| validateBufferFromString(bodyAsArrayBuffer, expectedBody, "Retrieve and verify response's body"); |
| assert_true(response.bodyUsed, "body as arrayBuffer: bodyUsed turned true"); |
| }); |
| } |
| |
| function checkBodyJSON(test, response, expectedBody) { |
| return response.json().then(function(bodyAsJSON) { |
| var strBody = JSON.stringify(bodyAsJSON) |
| assert_equals(strBody, expectedBody, "Retrieve and verify response's body"); |
| assert_true(response.bodyUsed, "body as json: bodyUsed turned true"); |
| }); |
| } |
| |
| function checkResponseBody(responsePromise, expectedBody, checkFunction, bodyTypes) { |
| promise_test(function(test) { |
| return responsePromise.then(function(response) { |
| assert_false(response.bodyUsed, "bodyUsed is false at init"); |
| return checkFunction(test, response, expectedBody); |
| }); |
| }, "Consume response's body: " + bodyTypes); |
| } |
| |
| var textData = JSON.stringify("This is response's body"); |
| var textResponseInit = { "headers": [["Content-Type", "text/PLAIN"]] }; |
| var blob = new Blob([stringToArray(textData).buffer], { "type": "application/octet-stream" }); |
| var multipartBoundary = "boundary-" + Math.random(); |
| var formTextResponseInit = { "headers": [["Content-Type", 'multipart/FORM-data; boundary="' + multipartBoundary + '"']] }; |
| var formTextData = stringToMultipartFormTextData(multipartBoundary, "name", textData); |
| |
| checkResponseBody(responsePromise(textData, textResponseInit), textData, checkBodyText, "from text to text"); |
| checkResponseBody(responsePromise(textData, textResponseInit), textData, checkBodyArrayBuffer, "from text to arrayBuffer"); |
| checkResponseBody(responsePromise(textData, textResponseInit), textData, checkBodyJSON, "from text to json"); |
| |
| checkResponseBody(responsePromise(blob), textData, checkBodyText, "from blob to text"); |
| checkResponseBody(responsePromise(blob), textData, checkBodyArrayBuffer, "from blob to arrayBuffer"); |
| checkResponseBody(responsePromise(blob), textData, checkBodyJSON, "from blob to json"); |
| |
| checkResponseBody(streamResponsePromise(textData), textData, checkBodyText, "from stream to text"); |
| checkResponseBody(streamResponsePromise(textData), textData, checkBodyArrayBuffer, "from stream to arrayBuffer"); |
| checkResponseBody(streamResponsePromise(textData), textData, checkBodyJSON, "from stream to json"); |
| |
| checkResponseBody(fetch("../resources/top.txt"), "top", checkBodyText, "from fetch to text"); |
| checkResponseBody(fetch("../resources/top.txt"), "top", checkBodyArrayBuffer, "from fetch to arrayBuffer"); |
| </script> |
| </body> |
| </html> |