summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/sw_clients/file_blob_upload_frame.html
blob: ff9803622a50458e4d9feb29cd86753aaace9d5a (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
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
  <title>test file blob upload with SW interception</title>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
<script class="testbody" type="text/javascript">

if (!parent) {
  dump("sw_clients/file_blob_upload_frame.html shouldn't be launched directly!");
}

function makeFileBlob(obj) {
  return new Promise(function(resolve, reject) {

    var request = indexedDB.open(window.location.pathname, 1);
    request.onerror = reject;
    request.onupgradeneeded = function(evt) {
      var db = evt.target.result;
      db.onerror = reject;

      var objectStore = db.createObjectStore('test', { autoIncrement: true });
      var index = objectStore.createIndex('test', 'index');
    };

    request.onsuccess = function(evt) {
      var db = evt.target.result;
      db.onerror = reject;

      var blob = new Blob([JSON.stringify(obj)],
                          { type: 'application/json' });
      var data = { blob, index: 5 };

      objectStore = db.transaction('test', 'readwrite').objectStore('test');
      objectStore.add(data).onsuccess = function(evt1) {
        var key = evt1.target.result;
        objectStore = db.transaction('test').objectStore('test');
        objectStore.get(key).onsuccess = function(evt2) {
          resolve(evt2.target.result.blob);
        };
      };
    };
  });
}

navigator.serviceWorker.ready.then(function() {
  parent.postMessage({ status: 'READY' }, '*');
});

var URL = '/tests/dom/serviceworkers/test/redirect_post.sjs';

addEventListener('message', function(evt) {
  if (evt.data.type = 'TEST') {
    makeFileBlob(evt.data.body).then(function(blob) {
      return fetch(URL, { method: 'POST', body: blob });
    }).then(function(response) {
      return response.json();
    }).then(function(result) {
      parent.postMessage({ status: 'OK', result }, '*');
    }).catch(function(e) {
      parent.postMessage({ status: 'ERROR', result: e.toString() }, '*');
    });
  }
});

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