mirror of
https://github.com/servo/servo.git
synced 2025-07-03 13:33:39 +01:00
131 lines
5.5 KiB
HTML
131 lines
5.5 KiB
HTML
<!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>
|