blob: 5b380096f402969c06760ac3581dbff79985de3e [file] [log] [blame]
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Request clone</title>
<meta name="help" href="https://fetch.spec.whatwg.org/#request">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script src="/fetch/api/resources/utils.js"></script>
<script>
var body = "This is request body";
var headersInit = { "name" : "value" };
var requestInit = { "body" : body,
"method" : "POST",
"headers" : headersInit
};
var request = new Request("", requestInit);
var clonedRequest = request.clone();
promise_test(function(test) {
return validateStreamFromString(request.body.getReader(), body);
}, "Check orginal request's body after cloning");
promise_test(function(test) {
return validateStreamFromString(clonedRequest.body.getReader(), body);
}, "Check cloned request's body");
function testReadableStreamClone(initialBuffer, bufferType)
{
promise_test(function(test) {
var request = new Request("", {"method" : "POST", "body" : new ReadableStream({start : function(controller) {
controller.enqueue(initialBuffer);
controller.close();
}})});
var clone = request.clone();
var stream1 = request.body;
var stream2 = clone.body;
var buffer;
return stream1.getReader().read().then(function(data) {
assert_false(data.done);
assert_true(data.value === initialBuffer, "Buffer of being-cloned response stream is the same as the original buffer");
return stream2.getReader().read();
}).then(function(data) {
assert_false(data.done);
assert_array_equals(data.value, initialBuffer, "Cloned buffer chunks have the same content");
assert_equals(Object.getPrototypeOf(data.value), Object.getPrototypeOf(initialBuffer), "Cloned buffers have the same type");
assert_true(data.value !== initialBuffer, "Buffer of cloned response stream is a clone of the original buffer");
});
}, "Check request clone use structureClone for teed ReadableStreams (" + bufferType + "chunk)");
}
var arrayBuffer = new ArrayBuffer(16);
testReadableStreamClone(new Int8Array(arrayBuffer, 1), "Int8Array");
testReadableStreamClone(new Int16Array(arrayBuffer, 2, 2), "Int16Array");
testReadableStreamClone(new Int32Array(arrayBuffer), "Int32Array");
testReadableStreamClone(arrayBuffer, "ArrayBuffer");
testReadableStreamClone(new Uint8Array(arrayBuffer), "Uint8Array");
testReadableStreamClone(new Uint8ClampedArray(arrayBuffer), "Uint8ClampedArray");
testReadableStreamClone(new Uint16Array(arrayBuffer, 2), "Uint16Array");
testReadableStreamClone(new Uint32Array(arrayBuffer), "Uint32Array");
testReadableStreamClone(new Float32Array(arrayBuffer), "Float32Array");
testReadableStreamClone(new Float64Array(arrayBuffer), "Float64Array");
testReadableStreamClone(new DataView(arrayBuffer, 2, 8), "DataView");
</script>
</body>
</html>