summaryrefslogtreecommitdiffstats
path: root/dom/cache/test/mochitest/test_cache_delete.js
blob: 67ded1affac2e4132a69c80f8a9b0bb7d8d62746 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* global context testDone:true */

var name = "delete" + context;

function setupTest(reqs) {
  return new Promise(function (resolve, reject) {
    var cache;
    caches
      .open(name)
      .then(function (c) {
        cache = c;
        return c.addAll(reqs);
      })
      .then(function () {
        resolve(cache);
      })
      .catch(function (err) {
        reject(err);
      });
  });
}

function testBasics() {
  var tests = [
    "//mochi.test:8888/?foo" + context,
    "//mochi.test:8888/?bar" + context,
  ];
  var cache;
  return setupTest(tests)
    .then(function (c) {
      cache = c;
      return cache.delete("//mochi.test:8888/?baz");
    })
    .then(function (deleted) {
      ok(!deleted, "Deleting a non-existing entry should fail");
      return cache.keys();
    })
    .then(function (keys) {
      is(keys.length, 2, "No entries from the cache should be deleted");
      return cache.delete(tests[0]);
    })
    .then(function (deleted) {
      ok(deleted, "Deleting an existing entry should succeed");
      return cache.keys();
    })
    .then(function (keys) {
      is(keys.length, 1, "Only one entry should exist now");
      ok(keys[0].url.includes(tests[1]), "The correct entry must be deleted");
    });
}

function testFragment() {
  var tests = [
    "//mochi.test:8888/?foo" + context,
    "//mochi.test:8888/?bar" + context,
    "//mochi.test:8888/?baz" + context + "#fragment",
  ];
  var cache;
  return setupTest(tests)
    .then(function (c) {
      cache = c;
      return cache.delete(tests[0] + "#fragment");
    })
    .then(function (deleted) {
      ok(deleted, "Deleting an existing entry should succeed");
      return cache.keys();
    })
    .then(function (keys) {
      is(keys.length, 2, "Only one entry should exist now");
      ok(keys[0].url.includes(tests[1]), "The correct entry must be deleted");
      ok(
        keys[1].url.includes(tests[2].replace("#fragment", "")),
        "The correct entry must be deleted"
      );
      // Now, delete a request that was added with a fragment
      return cache.delete("//mochi.test:8888/?baz" + context);
    })
    .then(function (deleted) {
      ok(deleted, "Deleting an existing entry should succeed");
      return cache.keys();
    })
    .then(function (keys) {
      is(keys.length, 1, "Only one entry should exist now");
      ok(keys[0].url.includes(tests[1]), "3The correct entry must be deleted");
    });
}

function testInterleaved() {
  var tests = [
    "//mochi.test:8888/?foo" + context,
    "//mochi.test:8888/?bar" + context,
  ];
  var newURL = "//mochi.test:8888/?baz" + context;
  var cache;
  return setupTest(tests)
    .then(function (c) {
      cache = c;
      // Simultaneously add and delete a request
      return Promise.all([cache.delete(newURL), cache.add(newURL)]);
    })
    .then(function (result) {
      ok(!result[1], "deletion should fail");
      return cache.keys();
    })
    .then(function (keys) {
      is(keys.length, 3, "Tree entries should still exist");
      ok(keys[0].url.includes(tests[0]), "The correct entry must be deleted");
      ok(keys[1].url.includes(tests[1]), "The correct entry must be deleted");
      ok(
        keys[2].url.includes(newURL),
        "The new entry should be correctly inserted"
      );
    });
}

// Make sure to clean up after each test step.
function step(testPromise) {
  return testPromise.then(function () {
    caches.delete(name);
  });
}

step(testBasics())
  .then(function () {
    return step(testFragment());
  })
  .then(function () {
    return step(testInterleaved());
  })
  .then(function () {
    testDone();
  });