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

var url = "test_cache.js";
var cache;
var fetchResponse;
Promise.all([fetch(url), caches.open("putter" + context)])
  .then(function (results) {
    fetchResponse = results[0];
    cache = results[1];
    return cache.put(url, fetchResponse.clone());
  })
  .then(function (result) {
    is(undefined, result, "Successful put() should resolve undefined");
    return cache.match(url);
  })
  .then(function (response) {
    ok(response, "match() should find resppnse that was previously put()");
    ok(
      response.url.endsWith(url),
      "matched response should match original url"
    );
    return Promise.all([fetchResponse.text(), response.text()]);
  })
  .then(function (results) {
    // suppress large assert spam unless it's relevent
    if (results[0] !== results[1]) {
      is(results[0], results[1], "stored response body should match original");
    }

    // Now, try to overwrite the request with a different response object.
    return cache.put(url, new Response("overwritten"));
  })
  .then(function () {
    return cache.matchAll(url);
  })
  .then(function (result) {
    is(result.length, 1, "Only one entry should exist");
    return result[0].text();
  })
  .then(function (body) {
    is(
      body,
      "overwritten",
      "The cache entry should be successfully overwritten"
    );

    // Now, try to write a URL with a fragment
    return cache.put(url + "#fragment", new Response("more overwritten"));
  })
  .then(function () {
    return cache.matchAll(url + "#differentFragment");
  })
  .then(function (result) {
    is(result.length, 1, "Only one entry should exist");
    return result[0].text();
  })
  .then(function (body) {
    is(
      body,
      "more overwritten",
      "The cache entry should be successfully overwritten"
    );

    // TODO: Verify that trying to store a response with an error raises a TypeError
    // when bug 1147178 is fixed.

    return caches.delete("putter" + context);
  })
  .then(function (deleted) {
    ok(deleted, "The cache should be deleted successfully");
    testDone();
  });