test_cache_keys.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. var name = "keys" + context;
  2. var c;
  3. var tests = [
  4. "//mochi.test:8888/?page" + context,
  5. "//mochi.test:8888/?another" + context,
  6. ];
  7. caches.open(name).then(function(cache) {
  8. c = cache;
  9. return c.addAll(tests);
  10. }).then(function() {
  11. // Add another cache entry using Cache.add
  12. var another = "//mochi.test:8888/?yetanother" + context;
  13. tests.push(another);
  14. return c.add(another);
  15. }).then(function() {
  16. // Add another cache entry with URL fragment using Cache.add
  17. var anotherWithFragment = "//mochi.test:8888/?fragment" + context + "#fragment";
  18. tests.push(anotherWithFragment);
  19. return c.add(anotherWithFragment);
  20. }).then(function() {
  21. return c.keys();
  22. }).then(function(keys) {
  23. is(keys.length, tests.length, "Same number of elements");
  24. // Verify both the insertion order of the requests and their validity.
  25. keys.forEach(function(r, i) {
  26. ok(r instanceof Request, "Valid request object");
  27. ok(r.url.indexOf(tests[i]) >= 0, "Valid URL");
  28. });
  29. // Try searching for just one request
  30. return c.keys(tests[1]);
  31. }).then(function(keys) {
  32. is(keys.length, 1, "One match should be found");
  33. ok(keys[0].url.indexOf(tests[1]) >= 0, "Valid URL");
  34. // Try to see if ignoreSearch works as expected.
  35. return c.keys(new Request("//mochi.test:8888/?foo"), {ignoreSearch: true});
  36. }).then(function(keys) {
  37. is(keys.length, tests.length, "Same number of elements");
  38. keys.forEach(function(r, i) {
  39. ok(r instanceof Request, "Valid request object");
  40. ok(r.url.indexOf(tests[i]) >= 0, "Valid URL");
  41. });
  42. // Try to see if ignoreMethod works as expected
  43. return Promise.all(
  44. ["POST", "PUT", "DELETE", "OPTIONS"]
  45. .map(function(method) {
  46. var req = new Request(tests[2], {method: method});
  47. return c.keys(req)
  48. .then(function(keys) {
  49. is(keys.length, 0, "No request should be matched without ignoreMethod");
  50. return c.keys(req, {ignoreMethod: true});
  51. }).then(function(keys) {
  52. is(keys.length, 1, "One match should be found");
  53. ok(keys[0].url.indexOf(tests[2]) >= 0, "Valid URL");
  54. });
  55. })
  56. );
  57. }).then(function() {
  58. // But HEAD should be allowed even without ignoreMethod
  59. return c.keys(new Request(tests[0], {method: "HEAD"}));
  60. }).then(function(keys) {
  61. is(keys.length, 1, "One match should be found");
  62. ok(keys[0].url.indexOf(tests[0]) >= 0, "Valid URL");
  63. // Make sure cacheName is ignored.
  64. return c.keys(tests[0], {cacheName: "non-existing-cache"});
  65. }).then(function(keys) {
  66. is(keys.length, 1, "One match should be found");
  67. ok(keys[0].url.indexOf(tests[0]) >= 0, "Valid URL");
  68. return caches.delete(name);
  69. }).then(function(deleted) {
  70. ok(deleted, "The cache should be successfully deleted");
  71. testDone();
  72. });