test_cache_put.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. var url = 'test_cache.js';
  2. var cache;
  3. var fetchResponse;
  4. Promise.all([fetch(url),
  5. caches.open('putter' + context)]).then(function(results) {
  6. fetchResponse = results[0];
  7. cache = results[1];
  8. return cache.put(url, fetchResponse.clone());
  9. }).then(function(result) {
  10. is(undefined, result, 'Successful put() should resolve undefined');
  11. return cache.match(url);
  12. }).then(function(response) {
  13. ok(response, 'match() should find resppnse that was previously put()');
  14. ok(response.url.endsWith(url), 'matched response should match original url');
  15. return Promise.all([fetchResponse.text(),
  16. response.text()]);
  17. }).then(function(results) {
  18. // suppress large assert spam unless it's relevent
  19. if (results[0] !== results[1]) {
  20. is(results[0], results[1], 'stored response body should match original');
  21. }
  22. // Now, try to overwrite the request with a different response object.
  23. return cache.put(url, new Response("overwritten"));
  24. }).then(function() {
  25. return cache.matchAll(url);
  26. }).then(function(result) {
  27. is(result.length, 1, "Only one entry should exist");
  28. return result[0].text();
  29. }).then(function(body) {
  30. is(body, "overwritten", "The cache entry should be successfully overwritten");
  31. // Now, try to write a URL with a fragment
  32. return cache.put(url + "#fragment", new Response("more overwritten"));
  33. }).then(function() {
  34. return cache.matchAll(url + "#differentFragment");
  35. }).then(function(result) {
  36. is(result.length, 1, "Only one entry should exist");
  37. return result[0].text();
  38. }).then(function(body) {
  39. is(body, "more overwritten", "The cache entry should be successfully overwritten");
  40. // TODO: Verify that trying to store a response with an error raises a TypeError
  41. // when bug 1147178 is fixed.
  42. return caches.delete('putter' + context);
  43. }).then(function(deleted) {
  44. ok(deleted, "The cache should be deleted successfully");
  45. testDone();
  46. });