<html>
<head></head>
<body>
<script src="/js-test-resources/js-test.js"></script>
<script>

description('Test for array buffer and data view POST in XMLHttpRequest async send.');

window.jsTestIsAsync = true;

function runTest(type) {
    return new Promise(function(resolve, reject) {
        var req = new XMLHttpRequest();
        req.open('POST', 'resources/post-echo-as-ascii.cgi', true);
        req.onreadystatechange = function() {
            if (req.readyState == req.DONE) {
                debug('req.readyState = ' + req.readyState + ': responseURL = ' + req.responseURL);
                resolve(req);
            }
        }
	
        if (type == 'dataView') {
           var array = new Uint8Array([0, 1, 2, 48, 49, 50, 128, 129, 130, 253, 254, 255]);
           req.send(new DataView(array.buffer));
        } else {
           var array = new Uint8Array([0, 1, 2, 48, 49, 50, 128, 129, 130, 253, 254, 255]);
           req.send(array);
        }
    });
}

runTest('dataView').then(function(req) {
    window.status = req.status;
    shouldBeEqualToString('status', '200');
    responseText = req.responseText;
    shouldBeEqualToString('responseText', '0 1 2 48 49 50 128 129 130 253 254 255');

    return runTest('bufferView');
}).then(function(req) {
    window.status = req.status;
    shouldBeEqualToString('status', '200');
    responseText = req.responseText;
    shouldBeEqualToString('responseText', '0 1 2 48 49 50 128 129 130 253 254 255');
}).catch(function(reason) {
    testFailed(String(reason));
}).then(finishJSTest, finishJSTest);

</script>
</body>
</html>
