<!DOCTYPE html>
<html>
<head>
  <script src="/js-test-resources/js-test-pre.js"></script>
</head>
<body>
  <script src="post-blob-content-type-tests.js"></script>
  <script type="text/javascript">
    description("Test verifies that content MIME type is set correctly " +
                "when Blob is sent using " +
                "<a href='http://www.w3.org/TR/XMLHttpRequest/#the-send-method'>XMLHttpRequest asynchronously.</a>");

    var xhr;
    var expectedMimeType;
    window.jsTestIsAsync = true;
    var asyncTestCase = 0;

    function runNextAsyncTest() {
        asyncTestCase++;
        runAsyncTests();
    }

    function reportResult(e) {
        if (xhr.status === 200) {
            expectedMimeType = JSON.parse(xhr.response)['content-type'] || "";
            shouldBeEqualToString("expectedMimeType", xhrBlobTestCases[asyncTestCase].expectedMime);
        } else if (xhr.status === 0 && xhrBlobTestCases[asyncTestCase].shouldThrow){
            testPassed("Cross-origin request without CORS headers should fail.");
        } else {
            testFailed("Unknown error");
        }

        runNextAsyncTest();
    }

    function runAsyncTests() {
        if (asyncTestCase >= xhrBlobTestCases.length) {
            finishJSTest();
            return;
        } else {
            var mime = xhrBlobTestCases[asyncTestCase].mime;
            var url = xhrBlobTestCases[asyncTestCase].url !== undefined ? xhrBlobTestCases[asyncTestCase].url + xhrBlobTestUrl : xhrBlobTestUrl;
            url += xhrBlobTestCases[asyncTestCase].allowOrigin || "";
            if (xhrBlobTestCases[asyncTestCase].shouldThrow !== undefined) {
                try {
                    testBlobContentTypeAsync(url, mime);
                } catch (e) {
                    testPassed("Exception should be thrown.")
                    runNextAsyncTest();
                }
            } else
                testBlobContentTypeAsync(url, mime);
        }
    }

    function testBlobContentTypeAsync(url, mimeType) {
        var blob;
        if (mimeType !== "")
            blob = new Blob(["Test content"], {type: mimeType});
        else
            blob = new Blob(["Test content"]);

        xhr = new XMLHttpRequest();
        xhr.onloadend = reportResult;
        xhr.open("POST", url, true);
        xhr.send(blob);
    }

    runAsyncTests();

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