Bug 1145792 - Add a test for the Cache.keys method; r=bkelly
authorEhsan Akhgari <ehsan@mozilla.com>
Fri, 20 Mar 2015 16:34:43 -0400
changeset 263800 e30f286527154374d630d5e522d9b7f3dc4987bd
parent 263799 37427ecf4c277e44907af1f4f66cfe0cabb888e4
child 263801 b7eb9be5ed3f46be26fb2b0ddb677a0a10185cbc
push id4718
push userraliiev@mozilla.com
push dateMon, 11 May 2015 18:39:53 +0000
treeherdermozilla-beta@c20c4ef55f08 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly
bugs1145792
milestone39.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1145792 - Add a test for the Cache.keys method; r=bkelly
dom/cache/test/mochitest/mochitest.ini
dom/cache/test/mochitest/test_cache_keys.html
dom/cache/test/mochitest/test_cache_keys.js
--- a/dom/cache/test/mochitest/mochitest.ini
+++ b/dom/cache/test/mochitest/mochitest.ini
@@ -10,16 +10,18 @@ support-files =
   serviceworker_driver.js
   test_cache_match_request.js
   test_cache_matchAll_request.js
   test_cache_overwrite.js
   mirror.sjs
   test_cache_match_vary.js
   vary.sjs
   test_caches.js
+  test_cache_keys.js
 
 [test_cache.html]
 [test_cache_add.html]
 [test_cache_match_request.html]
 [test_cache_matchAll_request.html]
 [test_cache_overwrite.html]
 [test_cache_match_vary.html]
 [test_caches.html]
+[test_cache_keys.html]
new file mode 100644
--- /dev/null
+++ b/dom/cache/test/mochitest/test_cache_keys.html
@@ -0,0 +1,20 @@
+<!-- Any copyright is dedicated to the Public Domain.
+   - http://creativecommons.org/publicdomain/zero/1.0/ -->
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Validate the Cache.keys() method</title>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+  <script type="text/javascript" src="driver.js"></script>
+</head>
+<body>
+<iframe id="frame"></iframe>
+<script class="testbody" type="text/javascript">
+  runTests("test_cache_keys.js")
+    .then(function() {
+      SimpleTest.finish();
+    });
+</script>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/cache/test/mochitest/test_cache_keys.js
@@ -0,0 +1,71 @@
+var name = "keys" + context;
+var c;
+
+var tests = [
+  "//mochi.test:8888/?page" + context,
+  "//mochi.test:8888/?another" + context,
+];
+
+caches.open(name).then(function(cache) {
+  c = cache;
+  return c.addAll(tests);
+}).then(function() {
+  // Add another cache entry using Cache.add
+  var another = "//mochi.test:8888/?yetanother" + context;
+  tests.push(another);
+  return c.add(another);
+}).then(function() {
+  return c.keys();
+}).then(function(keys) {
+  is(keys.length, tests.length, "Same number of elements");
+  // Verify both the insertion order of the requests and their validity.
+  keys.forEach(function(r, i) {
+    ok(r instanceof Request, "Valid request object");
+    ok(r.url.indexOf(tests[i]) >= 0, "Valid URL");
+  });
+  // Try searching for just one request
+  return c.keys(tests[1]);
+}).then(function(keys) {
+  is(keys.length, 1, "One match should be found");
+  ok(keys[0].url.indexOf(tests[1]) >= 0, "Valid URL");
+  // Try to see if ignoreSearch works as expected.
+  return c.keys(new Request("//mochi.test:8888/?foo"), {ignoreSearch: true});
+}).then(function(keys) {
+  is(keys.length, tests.length, "Same number of elements");
+  keys.forEach(function(r, i) {
+    ok(r instanceof Request, "Valid request object");
+    ok(r.url.indexOf(tests[i]) >= 0, "Valid URL");
+  });
+  // Try to see if ignoreMethod works as expected
+  return Promise.all(
+    ["POST", "PUT", "DELETE", "OPTIONS"]
+      .map(function(method) {
+        var req = new Request(tests[2], {method: method});
+        return c.keys(req)
+          .then(function(keys) {
+            is(keys.length, 0, "No request should be matched without ignoreMethod");
+            return c.keys(req, {ignoreMethod: true});
+          }).then(function(keys) {
+            is(keys.length, 1, "One match should be found");
+            ok(keys[0].url.indexOf(tests[2]) >= 0, "Valid URL");
+          });
+      })
+  );
+}).then(function() {
+  // But HEAD should be allowed even without ignoreMethod
+  return c.keys(new Request(tests[0], {method: "HEAD"}));
+}).then(function(keys) {
+  is(keys.length, 1, "One match should be found");
+  ok(keys[0].url.indexOf(tests[0]) >= 0, "Valid URL");
+  // TODO: Add tests for ignoreVary
+
+  // Make sure cacheName is ignored.
+  return c.keys(tests[0], {cacheName: "non-existing-cache"});
+}).then(function(keys) {
+  is(keys.length, 1, "One match should be found");
+  ok(keys[0].url.indexOf(tests[0]) >= 0, "Valid URL");
+  return caches.delete(name);
+}).then(function(deleted) {
+  ok(deleted, "The cache should be successfully deleted");
+  testDone();
+});