summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/api/response/response-consume.html
diff options
context:
space:
mode:
Diffstat (limited to 'testing/web-platform/tests/fetch/api/response/response-consume.html')
-rw-r--r--testing/web-platform/tests/fetch/api/response/response-consume.html131
1 files changed, 131 insertions, 0 deletions
diff --git a/testing/web-platform/tests/fetch/api/response/response-consume.html b/testing/web-platform/tests/fetch/api/response/response-consume.html
new file mode 100644
index 000000000..56e234248
--- /dev/null
+++ b/testing/web-platform/tests/fetch/api/response/response-consume.html
@@ -0,0 +1,131 @@
+<!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 checkBodyText(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 checkBodyBlob(response, expectedBody, checkContentType) {
+ return response.blob().then(function(bodyAsBlob) {
+ if (checkContentType)
+ assert_equals(bodyAsBlob.type, "text/plain", "Blob body type should be computed from the response Content-Type");
+
+ var promise = new Promise( function (resolve, reject) {
+ var reader = new FileReader();
+ reader.onload = function(evt) {
+ resolve(reader.result)
+ };
+ reader.onerror = function () {
+ reject("Blob's reader failed");
+ };
+ reader.readAsText(bodyAsBlob);
+ });
+ return promise.then(function(body) {
+ assert_equals(body, expectedBody, "Retrieve and verify response's body");
+ assert_true(response.bodyUsed, "body as blob: bodyUsed turned true");
+ });
+ });
+ }
+
+ function checkBodyArrayBuffer(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(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 checkBodyFormData(response, expectedBody) {
+ return response.formData().then(function(bodyAsFormData) {
+ assert_true(bodyAsFormData instanceof FormData, "Should receive a FormData");
+ assert_true(response.bodyUsed, "body as formData: bodyUsed turned true");
+ });
+ }
+
+ function checkResponseBody(body, bodyType, checkFunction) {
+ promise_test(function(test) {
+ var response = new Response(body, { "headers": [["Content-Type", "text/PLAIN"]] });
+ assert_false(response.bodyUsed, "bodyUsed is false at init");
+ return checkFunction(response, body);
+ }, "Consume response's body as " + bodyType);
+ }
+
+ var formData = new FormData();
+ formData.append("name", "value");
+ var textData = JSON.stringify("This is response's body");
+ var blob = new Blob([textData], { "type" : "text/plain" });
+
+ checkResponseBody(textData, "text", checkBodyText);
+ checkResponseBody(textData, "blob", function(response, body) { checkBodyBlob(response, body, true); });
+ checkResponseBody(textData, "arrayBuffer", checkBodyArrayBuffer);
+ checkResponseBody(textData, "json", checkBodyJSON);
+ checkResponseBody(formData, "formData", checkBodyFormData);
+
+ function checkBlobResponseBody(blobBody, blobData, bodyType, checkFunction) {
+ promise_test(function(test) {
+ var response = new Response(blobBody);
+ assert_false(response.bodyUsed, "bodyUsed is false at init");
+ return checkFunction(response, blobData);
+ }, "Consume blob response's body as " + bodyType);
+ }
+
+ checkBlobResponseBody(blob, textData, "blob", checkBodyBlob);
+ checkBlobResponseBody(blob, textData, "text", checkBodyText);
+ checkBlobResponseBody(blob, textData, "json", checkBodyJSON);
+ checkBlobResponseBody(blob, textData, "arrayBuffer", checkBodyArrayBuffer);
+
+ function checkReadableStreamResponseBody(streamData, bodyType, checkFunction) {
+ promise_test(function(test) {
+ var stream = new ReadableStream({
+ start: function(controller) {
+ controller.enqueue((stringToArray(streamData)));
+ controller.close();
+ }
+ });
+ var response = new Response(stream);
+ assert_false(response.bodyUsed, "bodyUsed is false at init");
+ return checkFunction(response, streamData);
+ }, "Consume stream response's body as " + bodyType);
+ }
+
+ checkReadableStreamResponseBody(textData, "blob", checkBodyBlob);
+ checkReadableStreamResponseBody(textData, "text", checkBodyText);
+ checkReadableStreamResponseBody(textData, "json", checkBodyJSON);
+ checkReadableStreamResponseBody(textData, "arrayBuffer", checkBodyArrayBuffer);
+
+ function checkFetchedResponseBody(bodyType, checkFunction) {
+ return promise_test(function(test) {
+ return fetch("../resources/top.txt").then(function(response) {
+ assert_false(response.bodyUsed, "bodyUsed is false at init");
+ return checkFunction(response, "top");
+ });
+ }, "Consume fetched response's body as " + bodyType);
+ }
+ checkFetchedResponseBody("blob", checkBodyBlob);
+ checkFetchedResponseBody("text", checkBodyText);
+ checkFetchedResponseBody("arrayBuffer", checkBodyArrayBuffer);
+
+ </script>
+ </body>
+</html>