<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Fetching the same resource with different options</title>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
  </head>
  <body>
    <script>
promise_test(function(t) {
    var url1 = "http://localhost:8000/resources/download-json-with-delay.py?iteration=2&delay=100&cors=true&allowCache=true&test=1";
    var url2 = "http://localhost:8000/resources/download-json-with-delay.py?iteration=2&delay=100&cors=true&allowCache=true&test=2";
    return Promise.all([
            fetch(url1, {"mode": "no-cors"}).then((response) => {
                assert_equals(response.type, "opaque");
            }),
            fetch(url1, {"mode": "cors"}).then((response) => {
                assert_equals(response.type, "cors");
                return response.arrayBuffer().then((arrayBuffer) => {
                    assert_true(arrayBuffer.byteLength > 0);
                });
            }),
            promise_rejects_js(t, TypeError, fetch(url1, {"mode": "cors", "credentials": "include"})),
            promise_rejects_js(t, TypeError, fetch(url2, {"mode": "cors", "credentials": "include"})),
            fetch(url2, {"mode": "cors"}).then((response) => {
                assert_equals(response.type, "cors");
                return response.arrayBuffer().then((arrayBuffer) => {
                    assert_true(arrayBuffer.byteLength > 0);
                });
            }),
            fetch(url2, {"mode": "no-cors"}).then((response) => {
                assert_equals(response.type, "opaque");
            })
    ]);
}, 'Ensure cors checks work when loading in parallel the same resource with different modes');

promise_test(function(t) {
    var url3 = "http://localhost:8000/resources/download-json-with-delay.py?iteration=2&delay=100&cors=true&allowCache=true&test=3";
    var url4 = "http://localhost:8000/resources/download-json-with-delay.py?iteration=2&delay=100&cors=true&allowCache=true&test=4";
    return fetch(url3, {"mode": "cors"}).then((response) => {
        assert_equals(response.type, "cors");
        return response.arrayBuffer().then((arrayBuffer) => {
            assert_true(arrayBuffer.byteLength > 0);
        });
    }).then(() => {
        return fetch(url3, {"mode": "no-cors"}).then((response) => {
            assert_equals(response.type, "opaque");
        });
    }).then(() => {
        return promise_rejects_js(t, TypeError, fetch(url3, {"mode": "cors", "credentials": "include"}));
    }).then(() => {
        return promise_rejects_js(t, TypeError, fetch(url4, {"mode": "cors", "credentials": "include"}));
    }).then(() => {
        return fetch(url4, {"mode": "no-cors"}).then((response) => {
            assert_equals(response.type, "opaque");
        });
    }).then(() => {
        return fetch(url4, {"mode": "cors"}).then((response) => {
            assert_equals(response.type, "cors");
            return response.arrayBuffer().then((arrayBuffer) => {
                assert_true(arrayBuffer.byteLength > 0);
            });
        });
    });
}, 'Ensure cors checks work when loading successively the same resource with different modes');
    </script>
  </body>
</html>
