chromium/third_party/blink/web_tests/http/tests/xmlhttprequest/response-document.html

<!DOCTYPE html>
<script src="/js-test-resources/js-test.js"></script>
<script>

description("Request a file as the document type with asynchronous XMLHttpRequest in a document context.");
window.jsTestIsAsync = true;

var global = window;

function rejectIfThrown(func, reject) {
    return function() {
        try {
            func();
        } catch (e) {
            reject(e);
        }
    };
}

new Promise(function(resolve, reject) {
    global.req = new XMLHttpRequest;
    req.responseType = 'document';
    req.open('GET', 'resources/test.html', true);
    req.onreadystatechange = rejectIfThrown(function() {
        if (req.readyState != req.DONE) {
            return;
        }

        shouldBe('req.status', '200');
        shouldBeNonNull('req.response');
        global.result = req.response.querySelector('p').innerText;
        shouldBeEqualToString('result', 'PASS');

        resolve();
    }, reject);
    req.send(null);
}).catch(function(r) {
    testFailed('Set responseType to document before open(): Failed: ' + r);
}).then(function() {
    return new Promise(function(resolve, reject)
    {
        global.req = new XMLHttpRequest;
        req.responseType = 'blob';
        req.open('GET', 'resources/test.html', true);
        req.onreadystatechange = rejectIfThrown(function() {
            if (req.readyState != req.DONE) {
                return;
            }

            shouldBe('req.status', '200');
            shouldBeNonNull('req.response');
            global.result = req.response.querySelector('p').innerText;
            shouldBeEqualToString('result', 'PASS');

            resolve();
        }, reject);
        req.responseType = 'document';
        req.send(null);
    });
}).catch(function(r) {
    testFailed('Change responseType from blob to document between open() and send(): Failed: ' + r);
}).then(function() {
    return new Promise(function(resolve, reject)
    {
        global.req = new XMLHttpRequest;
        req.responseType = 'blob';
        req.open('GET', 'resources/test.html', true);
        req.onreadystatechange = rejectIfThrown(function() {
            if (req.readyState == req.HEADERS_RECEIVED) {
                req.responseType = 'document';
                return;
            } else if (req.readyState != req.DONE) {
                return;
            }

            shouldBe('req.status', '200');
            shouldBeNonNull('req.response');
            global.result = req.response.querySelector('p').innerText;
            shouldBeEqualToString('result', 'PASS');

            resolve();
        }, reject);
        req.send(null);
    });
}).catch(function(r) {
    testFailed('Change responseType from blob to document in HEADERS_RECEIVED: Failed: ' + r);
}).then(finishJSTest, finishJSTest);

</script>