<!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();

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);
            if (data.value instanceof ArrayBuffer)
                assert_array_equals(new Uint8Array(data.value), new Uint8Array(initialBuffer), "Cloned buffer chunks have the same content");
            else if (data.value instanceof DataView)
                assert_array_equals(new Uint8Array(data.value.buffer), new Uint8Array(initialBuffer.buffer), "Cloned buffer chunks have the same content");
            else
                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");
            if (data.value instanceof ArrayBuffer)
                assert_true(data.value.resizable, "Buffer of cloned response stream is resizable");
            else
                assert_true(data.value.buffer.resizable, "Buffer of cloned response stream is resizable");
        });
    }, "Check request clone use structureClone for teed ReadableStreams (" + bufferType  + "chunk)");
}

var arrayBuffer = new ArrayBuffer(16, { maxByteLength: 1024 });
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>
