1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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>
|