summaryrefslogtreecommitdiffstats
path: root/dom/cache/test/mochitest/test_cache.js
blob: 7d6d01b34bb400996f6148910b49f27a1fc5bda2 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* global context testDone:true */

var c = null;
var request = "https://example.com/hmm?q=foobar" + context;
var response = new Response("This is some Response!");
var name = "snafu" + context;
var foobar = "foobar" + context;

ok(!!caches, "caches object should be available on global");
caches
  .open(name)
  .then(function (openCache) {
    ok(
      openCache instanceof Cache,
      "cache object should be resolved from caches.open"
    );
    return caches.has(name);
  })
  .then(function (hasResult) {
    ok(hasResult, "caches.has() should resolve true");
    return caches.keys();
  })
  .then(function (keys) {
    ok(!!keys, "caches.keys() should resolve to a truthy value");
    ok(
      keys.length >= 1,
      "caches.keys() should resolve to an array of length at least 1"
    );
    ok(
      keys.includes(name),
      "caches.keys() should resolve to an array containing key"
    );
    return caches.delete(name);
  })
  .then(function (deleteResult) {
    ok(deleteResult, "caches.delete() should resolve true");
    return caches.has(name);
  })
  .then(function (hasMissingCache) {
    ok(!hasMissingCache, "missing key should return false from has");
  })
  .then(function () {
    return caches.open(name);
  })
  .then(function (snafu) {
    return snafu.keys();
  })
  .then(function (empty) {
    is(0, empty.length, "cache.keys() should resolve to an array of length 0");
  })
  .then(function () {
    return caches.open(name);
  })
  .then(function (snafu) {
    var req = "./cachekey";
    var res = new Response("Hello world");
    return snafu
      .put("ftp://invalid", res)
      .then(function () {
        ok(false, "This should fail");
      })
      .catch(function (err) {
        is(
          err.name,
          "TypeError",
          "put() should throw TypeError for invalid scheme"
        );
        return snafu.put(req, res);
      })
      .then(function (v) {
        return snafu;
      });
  })
  .then(function (snafu) {
    return Promise.all([snafu, snafu.keys()]);
  })
  .then(function (args) {
    var snafu = args[0];
    var keys = args[1];
    is(1, keys.length, "cache.keys() should resolve to an array of length 1");
    ok(keys[0] instanceof Request, "key should be a Request");
    ok(keys[0].url.match(/cachekey$/), "Request URL should match original");
    return Promise.all([
      snafu,
      snafu.match(keys[0]),
      snafu.match("ftp://invalid"),
    ]);
  })
  .then(function (args) {
    var snafu = args[0];
    var res = args[1];
    ok(res instanceof Response, "value should be a Response");
    is(res.status, 200, "Response status should be 200");
    is(
      undefined,
      args[2],
      "Match with invalid scheme should resolve undefined"
    );
    return Promise.all([snafu, snafu.put("./cachekey2", res)]);
  })
  .then(function (args) {
    var snafu = args[0];
    return snafu.match("./cachekey2");
  })
  .then(function (res) {
    return res.text().then(function (v) {
      is(v, "Hello world", "Response body should match original");
    });
  })
  .then(function () {
    // FIXME(nsm): Can't use a Request object for now since the operations
    // consume it's 'body'. See
    // https://github.com/slightlyoff/ServiceWorker/issues/510.
    return caches.open(foobar);
  })
  .then(function (openCache) {
    c = openCache;
    return c.put(request, response);
  })
  .then(function (putResponse) {
    is(putResponse, undefined, "The promise should resolve to undefined");
    return c.keys(request);
  })
  .then(function (keys) {
    ok(keys, "Valid keys object expected");
    is(keys.length, 1, "Only one key is expected");
    return c.keys();
  })
  .then(function (keys) {
    ok(keys, "Valid keys object expected");
    is(keys.length, 1, "Only one key is expected");
    return c.matchAll(request);
  })
  .then(function (matchAllResponses) {
    ok(matchAllResponses, "matchAll should succeed");
    is(matchAllResponses.length, 1, "Only one match is expected");
    return c.match(request);
  })
  .then(function (matchResponse) {
    ok(matchResponse, "match should succeed");
    return caches.match(request);
  })
  .then(function (storageMatchResponse) {
    ok(storageMatchResponse, "storage match should succeed");
    return caches.match(request, { cacheName: foobar });
  })
  .then(function (storageMatchResponse) {
    ok(storageMatchResponse, "storage match with cacheName should succeed");
    var request2 = new Request("https://example.com/hmm?q=snafu" + context);
    return c.match(request2, { ignoreSearch: true });
  })
  .then(function (match2Response) {
    ok(match2Response, "match should succeed");
    return c.delete(request);
  })
  .then(function (deleteResult) {
    ok(deleteResult, "delete should succeed");
    return c.keys();
  })
  .then(function (keys) {
    ok(keys, "Valid keys object expected");
    is(keys.length, 0, "Zero keys is expected");
    return c.matchAll(request);
  })
  .then(function (matchAll2Responses) {
    ok(matchAll2Responses, "matchAll should succeed");
    is(matchAll2Responses.length, 0, "Zero matches is expected");
    return caches.has(foobar);
  })
  .then(function (hasResult) {
    ok(hasResult, "has should succeed");
    return caches.keys();
  })
  .then(function (keys) {
    ok(keys, "Valid keys object expected");
    ok(keys.length >= 2, "At least two keys are expected");
    ok(keys.includes(name), "snafu should exist");
    ok(
      keys.indexOf(foobar) >= keys.indexOf(name),
      "foobar should come after it"
    );
    return caches.delete(foobar);
  })
  .then(function (deleteResult) {
    ok(deleteResult, "delete should succeed");
    return caches.has(foobar);
  })
  .then(function (hasMissingCache) {
    ok(!hasMissingCache, "has should have a result");
    return caches.delete(name);
  })
  .then(function (deleteResult) {
    ok(deleteResult, "delete should succeed");
    testDone();
  });