summaryrefslogtreecommitdiffstats
path: root/dom/cache/test/mochitest/test_cache_overwrite.js
blob: dd6b3c5848f6eeaa0c9fec193549a0c294826cde (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
/* global context testDone:true */

var requestURL =
  "//mochi.test:8888/tests/dom/cache/test/mochitest/mirror.sjs?" + context;
var response;
var c;
var responseText;
var name = "match-mirror" + context;

function checkResponse(r) {
  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("Mirrored"),
    response.headers.get("Mirrored"),
    "Both responses should have the same Mirrored header"
  );
  return r.text().then(function (text) {
    is(text, responseText, "The response body should be correct");
  });
}

fetch(new Request(requestURL, { headers: { Mirror: "bar" } }))
  .then(function (r) {
    is(
      r.headers.get("Mirrored"),
      "bar",
      "The server should give back the correct header"
    );
    response = r;
    return response.text();
  })
  .then(function (text) {
    responseText = text;
    return caches.open(name);
  })
  .then(function (cache) {
    c = cache;
    return c.add(new Request(requestURL, { headers: { Mirror: "foo" } }));
  })
  .then(function () {
    // Overwrite the request, to replace the entry stored in response_headers
    // with a different value.
    return c.add(new Request(requestURL, { headers: { Mirror: "bar" } }));
  })
  .then(function () {
    return c.matchAll();
  })
  .then(function (r) {
    is(r.length, 1, "Only one request should be in the cache");
    return checkResponse(r[0]);
  })
  .then(function () {
    return caches.delete(name);
  })
  .then(function (deleted) {
    ok(deleted, "The cache should be deleted successfully");
    testDone();
  });