summaryrefslogtreecommitdiffstats
path: root/dom/cache/test/mochitest/test_cache_match_vary.js
blob: 9b4ad538a206c7fea16f34fd6b108d0970f606ab (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
var requestURL = "//mochi.test:8888/tests/dom/cache/test/mochitest/vary.sjs?" + context;
var name = "match-vary" + context;

function checkResponse(r, response, responseText) {
  ok(r !== response, "The objects should not be the same");
  is(r.url, response.url.replace("#fragment", ""),
     "The URLs should be the same");
  is(r.status, response.status, "The status codes should be the same");
  is(r.type, response.type, "The response types should be the same");
  is(r.ok, response.ok, "Both responses should have succeeded");
  is(r.statusText, response.statusText,
     "Both responses should have the same status text");
  is(r.headers.get("Vary"), response.headers.get("Vary"),
     "Both responses should have the same Vary header");
  return r.text().then(function(text) {
    is(text, responseText, "The response body should be correct");
  });
}

// Returns a Promise that will be resolved to an object with the following
// properties:
// * cache: A Cache object that contains one entry fetched with headers.
// * response: A Response object which is the result of fetching a request
//             with the specified headers.
// * responseText: The body of the above response object.
function setupTest(headers) {
  return setupTestMultipleEntries([headers]).then(function(test) {
    return {response: test.response[0],
            responseText: test.responseText[0],
            cache: test.cache};
  });
}
function setupTestMultipleEntries(headers) {
  ok(Array.isArray(headers), "headers should be an array");
  return new Promise(function(resolve, reject) {
    var response, responseText, cache;
    Promise.all(headers.map(function(h) {
      return fetch(requestURL, {headers: h});
    })).then(function(r) {
        response = r;
        return Promise.all(response.map(function(r) {
          return r.text();
        }));
      }).then(function(text) {
        responseText = text;
        return caches.open(name);
      }).then(function(c) {
        cache = c;
        return Promise.all(headers.map(function(h) {
          return c.add(new Request(requestURL, {headers: h}));
        }));
      }).then(function() {
        resolve({response: response, responseText: responseText, cache: cache});
      }).catch(function(err) {
        reject(err);
      });
  });
}

function testBasics() {
  var test;
  return setupTest({"WhatToVary": "Custom"})
    .then(function(t) {
      test = t;
      // Ensure that searching without specifying a Custom header succeeds.
      return test.cache.match(requestURL);
    }).then(function(r) {
      return checkResponse(r, test.response, test.responseText);
    }).then(function() {
      // Ensure that searching with a non-matching value for the Custom header fails.
      return test.cache.match(new Request(requestURL, {headers: {"Custom": "foo=bar"}}));
    }).then(function(r) {
      is(typeof r, "undefined", "Searching for a request with an unknown Vary header should not succeed");
      // Ensure that searching with a non-matching value for the Custom header but with ignoreVary set succeeds.
      return test.cache.match(new Request(requestURL, {headers: {"Custom": "foo=bar"}}),
                              {ignoreVary: true});
    }).then(function(r) {
      return checkResponse(r, test.response, test.responseText);
    });
}

function testBasicKeys() {
  function checkRequest(reqs) {
    is(reqs.length, 1, "One request expected");
    ok(reqs[0].url.indexOf(requestURL) >= 0, "The correct request expected");
    ok(reqs[0].headers.get("WhatToVary"), "Custom", "The correct request headers expected");
  }
  var test;
  return setupTest({"WhatToVary": "Custom"})
    .then(function(t) {
      test = t;
      // Ensure that searching without specifying a Custom header succeeds.
      return test.cache.keys(requestURL);
    }).then(function(r) {
      return checkRequest(r);
    }).then(function() {
      // Ensure that searching with a non-matching value for the Custom header fails.
      return test.cache.keys(new Request(requestURL, {headers: {"Custom": "foo=bar"}}));
    }).then(function(r) {
      is(r.length, 0, "Searching for a request with an unknown Vary header should not succeed");
      // Ensure that searching with a non-matching value for the Custom header but with ignoreVary set succeeds.
      return test.cache.keys(new Request(requestURL, {headers: {"Custom": "foo=bar"}}),
                             {ignoreVary: true});
    }).then(function(r) {
      return checkRequest(r);
    });
}

function testStar() {
  function ensurePromiseRejected(promise) {
    return promise
      .then(function() {
        ok(false, "Promise should be rejected");
      }, function(err) {
        is(err.name, "TypeError", "Attempting to store a Response with a Vary:* header must fail");
      });
  }
  var test;
  return new Promise(function(resolve, reject) {
    var cache;
    caches.open(name).then(function(c) {
      cache = c;
      Promise.all([
        ensurePromiseRejected(
          cache.add(new Request(requestURL + "1", {headers: {"WhatToVary": "*"}}))),
        ensurePromiseRejected(
          cache.addAll([
            new Request(requestURL + "2", {headers: {"WhatToVary": "*"}}),
            requestURL + "3",
          ])),
        ensurePromiseRejected(
          fetch(new Request(requestURL + "4", {headers: {"WhatToVary": "*"}}))
            .then(function(response) {
              return cache.put(requestURL + "4", response);
            })),
        ensurePromiseRejected(
          cache.add(new Request(requestURL + "5", {headers: {"WhatToVary": "*,User-Agent"}}))),
        ensurePromiseRejected(
          cache.addAll([
            new Request(requestURL + "6", {headers: {"WhatToVary": "*,User-Agent"}}),
            requestURL + "7",
          ])),
        ensurePromiseRejected(
          fetch(new Request(requestURL + "8", {headers: {"WhatToVary": "*,User-Agent"}}))
            .then(function(response) {
              return cache.put(requestURL + "8", response);
            })),
        ensurePromiseRejected(
          cache.add(new Request(requestURL + "9", {headers: {"WhatToVary": "User-Agent,*"}}))),
        ensurePromiseRejected(
          cache.addAll([
            new Request(requestURL + "10", {headers: {"WhatToVary": "User-Agent,*"}}),
            requestURL + "10",
          ])),
        ensurePromiseRejected(
          fetch(new Request(requestURL + "11", {headers: {"WhatToVary": "User-Agent,*"}}))
            .then(function(response) {
              return cache.put(requestURL + "11", response);
            })),
      ]).then(reject, resolve);
    });
  });
}

function testMatch() {
  var test;
  return setupTest({"WhatToVary": "Custom", "Custom": "foo=bar"})
    .then(function(t) {
      test = t;
      // Ensure that searching with a different Custom header fails.
      return test.cache.match(new Request(requestURL, {headers: {"Custom": "bar=baz"}}));
    }).then(function(r) {
      is(typeof r, "undefined", "Searching for a request with a non-matching Custom header should not succeed");
      // Ensure that searching with the same Custom header succeeds.
      return test.cache.match(new Request(requestURL, {headers: {"Custom": "foo=bar"}}));
    }).then(function(r) {
      return checkResponse(r, test.response, test.responseText);
    });
}

function testInvalidHeaderName() {
  var test;
  return setupTest({"WhatToVary": "Foo/Bar, Custom-User-Agent"})
    .then(function(t) {
      test = t;
      // Ensure that searching with a different User-Agent header fails.
      return test.cache.match(new Request(requestURL, {headers: {"Custom-User-Agent": "MyUA"}}));
    }).then(function(r) {
      is(typeof r, "undefined", "Searching for a request with a non-matching Custom-User-Agent header should not succeed");
      // Ensure that searching with a different Custom-User-Agent header but with ignoreVary succeeds.
      return test.cache.match(new Request(requestURL, {headers: {"Custom-User-Agent": "MyUA"}}),
                              {ignoreVary: true});
    }).then(function(r) {
      return checkResponse(r, test.response, test.responseText);
    }).then(function() {
      // Ensure that we do not mistakenly recognize the tokens in the invalid header name.
      return test.cache.match(new Request(requestURL, {headers: {"Foo": "foobar"}}));
    }).then(function(r) {
      return checkResponse(r, test.response, test.responseText);
    });
}

function testMultipleHeaders() {
  var test;
  return setupTest({"WhatToVary": "Custom-Referer,\tCustom-Accept-Encoding"})
    .then(function(t) {
      test = t;
      // Ensure that searching with a different Referer header fails.
      return test.cache.match(new Request(requestURL, {headers: {"Custom-Referer": "https://somesite.com/"}}));
    }).then(function(r) {
      is(typeof r, "undefined", "Searching for a request with a non-matching Custom-Referer header should not succeed");
      // Ensure that searching with a different Custom-Referer header but with ignoreVary succeeds.
      return test.cache.match(new Request(requestURL, {headers: {"Custom-Referer": "https://somesite.com/"}}),
                              {ignoreVary: true});
    }).then(function(r) {
      return checkResponse(r, test.response, test.responseText);
    }).then(function() {
      // Ensure that searching with a different Custom-Accept-Encoding header fails.
      return test.cache.match(new Request(requestURL, {headers: {"Custom-Accept-Encoding": "myencoding"}}));
    }).then(function(r) {
      is(typeof r, "undefined", "Searching for a request with a non-matching Custom-Accept-Encoding header should not succeed");
      // Ensure that searching with a different Custom-Accept-Encoding header but with ignoreVary succeeds.
      return test.cache.match(new Request(requestURL, {headers: {"Custom-Accept-Encoding": "myencoding"}}),
                              {ignoreVary: true});
    }).then(function(r) {
      return checkResponse(r, test.response, test.responseText);
    }).then(function() {
      // Ensure that searching with an empty Custom-Referer header succeeds.
      return test.cache.match(new Request(requestURL, {headers: {"Custom-Referer": ""}}));
    }).then(function(r) {
      return checkResponse(r, test.response, test.responseText);
    }).then(function() {
      // Ensure that searching with an empty Custom-Accept-Encoding header succeeds.
      return test.cache.match(new Request(requestURL, {headers: {"Custom-Accept-Encoding": ""}}));
    }).then(function(r) {
      return checkResponse(r, test.response, test.responseText);
    }).then(function() {
      // Ensure that searching with an empty Custom-Referer header but with a different Custom-Accept-Encoding header fails.
      return test.cache.match(new Request(requestURL, {headers: {"Custom-Referer": "",
                                                                 "Custom-Accept-Encoding": "myencoding"}}));
    }).then(function(r) {
      is(typeof r, "undefined", "Searching for a request with a non-matching Custom-Accept-Encoding header should not succeed");
      // Ensure that searching with an empty Custom-Referer header but with a different Custom-Accept-Encoding header and ignoreVary succeeds.
      return test.cache.match(new Request(requestURL, {headers: {"Custom-Referer": "",
                                                                 "Custom-Accept-Encoding": "myencoding"}}),
                              {ignoreVary: true});
    }).then(function(r) {
      return checkResponse(r, test.response, test.responseText);
    });
}

function testMultipleCacheEntries() {
  var test;
  return setupTestMultipleEntries([
    {"WhatToVary": "Accept-Language", "Accept-Language": "en-US"},
    {"WhatToVary": "Accept-Language", "Accept-Language": "en-US, fa-IR"},
  ]).then(function(t) {
    test = t;
    return test.cache.matchAll();
  }).then(function (r) {
    is(r.length, 2, "Two cache entries should be stored in the DB");
    // Ensure that searching without specifying an Accept-Language header fails.
    return test.cache.matchAll(requestURL);
  }).then(function(r) {
    is(r.length, 0, "Searching for a request without specifying an Accept-Language header should not succeed");
    // Ensure that searching without specifying an Accept-Language header but with ignoreVary succeeds.
    return test.cache.matchAll(requestURL, {ignoreVary: true});
  }).then(function(r) {
    return Promise.all([
      checkResponse(r[0], test.response[0], test.responseText[0]),
      checkResponse(r[1], test.response[1], test.responseText[1]),
    ]);
  }).then(function() {
    // Ensure that searching with Accept-Language: en-US succeeds.
    return test.cache.matchAll(new Request(requestURL, {headers: {"Accept-Language": "en-US"}}));
  }).then(function(r) {
    is(r.length, 1, "One cache entry should be found");
    return checkResponse(r[0], test.response[0], test.responseText[0]);
  }).then(function() {
    // Ensure that searching with Accept-Language: en-US,fa-IR succeeds.
    return test.cache.matchAll(new Request(requestURL, {headers: {"Accept-Language": "en-US, fa-IR"}}));
  }).then(function(r) {
    is(r.length, 1, "One cache entry should be found");
    return checkResponse(r[0], test.response[1], test.responseText[1]);
  }).then(function() {
    // Ensure that searching with a valid Accept-Language header but with ignoreVary returns both entries.
    return test.cache.matchAll(new Request(requestURL, {headers: {"Accept-Language": "en-US"}}),
                               {ignoreVary: true});
  }).then(function(r) {
    return Promise.all([
      checkResponse(r[0], test.response[0], test.responseText[0]),
      checkResponse(r[1], test.response[1], test.responseText[1]),
    ]);
  }).then(function() {
    // Ensure that searching with Accept-Language: fa-IR fails.
    return test.cache.matchAll(new Request(requestURL, {headers: {"Accept-Language": "fa-IR"}}));
  }).then(function(r) {
    is(r.length, 0, "Searching for a request with a different Accept-Language header should not succeed");
    // Ensure that searching with Accept-Language: fa-IR but with ignoreVary should succeed.
    return test.cache.matchAll(new Request(requestURL, {headers: {"Accept-Language": "fa-IR"}}),
                               {ignoreVary: true});
  }).then(function(r) {
    is(r.length, 2, "Two cache entries should be found");
    return Promise.all([
      checkResponse(r[0], test.response[0], test.responseText[0]),
      checkResponse(r[1], test.response[1], test.responseText[1]),
    ]);
  });
}

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

step(testBasics()).then(function() {
  return step(testBasicKeys());
}).then(function() {
  return step(testStar());
}).then(function() {
  return step(testMatch());
}).then(function() {
  return step(testInvalidHeaderName());
}).then(function() {
  return step(testMultipleHeaders());
}).then(function() {
  return step(testMultipleCacheEntries());
}).then(function() {
  testDone();
});