summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/test/test_blob_worker_xhr_post_multifile.html
blob: a2861f1e5c9893b5cf57b09aab6427ec978925c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<html>
<head>
  <title>Indexed Database Property Test</title>

  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>

  <script type="text/javascript">
  /**
   * Create a composite/multi-file Blob on the worker, then post it as an XHR
   * payload and ensure that we don't hang/generate an assertion/etc. but
   * instead generate the expected 404.  This test is basically the same as
   * test_blob_worker_xhr_post.html except for the composite Blob.
   */
  function* testSteps()
  {
    const BLOB_DATA = ["fun ", "times ", "all ", "around!"];
    const BLOB_TYPE = "text/plain";
    const BLOB_SIZE = BLOB_DATA.join("").length;

    info("Setting up");

    let request = indexedDB.open(window.location.pathname, 1);
    request.onerror = errorHandler;
    request.onupgradeneeded = grabEventAndContinueHandler;
    request.onsuccess = unexpectedSuccessHandler;
    let event = yield undefined;

    let db = event.target.result;
    db.onerror = errorHandler;

    ok(db, "Created database");

    info("Creating objectStore");

    let objectStore = db.createObjectStore("foo", { autoIncrement: true });

    request.onupgradeneeded = unexpectedSuccessHandler;
    request.onsuccess = grabEventAndContinueHandler;
    event = yield undefined;

    ok(true, "Opened database");

    let blob = new Blob(BLOB_DATA, { type: BLOB_TYPE });

    info("Adding blob to database");

    objectStore = db.transaction("foo", "readwrite").objectStore("foo");
    objectStore.add(blob).onsuccess = grabEventAndContinueHandler;
    event = yield undefined;

    let blobKey = event.target.result;
    ok(blobKey, "Got a key for the blob");

    info("Getting blob from the database");

    objectStore = db.transaction("foo").objectStore("foo");
    objectStore.get(blobKey).onsuccess = grabEventAndContinueHandler;
    event = yield undefined;

    blob = event.target.result;

    ok(blob instanceof Blob, "Got a blob");
    is(blob.size, BLOB_SIZE, "Correct size");
    is(blob.type, BLOB_TYPE, "Correct type");

    function workerScript() {
      /* eslint-env worker */
      onmessage = function(event) {
        var blob = event.data;
        var compositeBlob = new Blob(["preceding string. ", blob],
                                     { type: "text/plain" });
        var xhr = new XMLHttpRequest();
        // We just want to make sure the error case doesn't fire; it's fine for
        // us to just want a 404.
        xhr.open("POST", "http://mochi.test:8888/does-not-exist", true);
        xhr.onload = function() {
          postMessage({ status: xhr.status });
        };
        xhr.onerror = function() {
          postMessage({ status: "error" });
        };
        xhr.send(compositeBlob);
      };
    }

    let workerScriptUrl =
      URL.createObjectURL(new Blob(["(", workerScript.toString(), ")()"]));

    let xhrWorker = new Worker(workerScriptUrl);
    xhrWorker.postMessage(blob);
    xhrWorker.onmessage = grabEventAndContinueHandler;
    event = yield undefined;

    is(event.data.status, 404, "XHR generated the expected 404");
    xhrWorker.terminate();

    URL.revokeObjectURL(workerScriptUrl);

    finishTest();
  }
  </script>
  <script type="text/javascript" src="helpers.js"></script>

</head>

<body onload="runTest();"></body>

</html>