summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/cache-storage/resources/credentials-worker.js
blob: 43965b5fe4c9c662560cc37ea356cc3687e88ac5 (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
var cache_name = 'credentials';

function assert_equals(actual, expected, message) {
  if (!Object.is(actual, expected))
    throw Error(message + ': expected: ' + expected + ', actual: ' + actual);
}

self.onfetch = function(e) {
  if (!/\.txt$/.test(e.request.url)) return;
  var content = e.request.url;
  var cache;
  e.respondWith(
    self.caches.open(cache_name)
      .then(function(result) {
        cache = result;
        return cache.put(e.request, new Response(content));
      })

      .then(function() { return cache.match(e.request); })
      .then(function(result) { return result.text(); })
      .then(function(text) {
        assert_equals(text, content, 'Cache.match() body should match');
      })

      .then(function() { return cache.matchAll(e.request); })
      .then(function(results) {
        assert_equals(results.length, 1, 'Should have one response');
        return results[0].text();
      })
      .then(function(text) {
        assert_equals(text, content, 'Cache.matchAll() body should match');
      })

      .then(function() { return self.caches.match(e.request); })
      .then(function(result) { return result.text(); })
      .then(function(text) {
        assert_equals(text, content, 'CacheStorage.match() body should match');
      })

     .then(function() {
        return new Response('dummy');
      })
  );
};

self.onmessage = function(e) {
  if (e.data === 'keys') {
    self.caches.open(cache_name)
      .then(function(cache) { return cache.keys(); })
      .then(function(requests) {
        var urls = requests.map(function(request) { return request.url; });
        self.clients.matchAll().then(function(clients) {
          clients.forEach(function(client) {
            client.postMessage(urls);
          });
        });
      });
  }
};