test_cache.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. var c = null
  2. var request = "http://example.com/hmm?q=foobar" + context;
  3. var response = new Response("This is some Response!");
  4. var name = 'snafu' + context;
  5. var foobar = 'foobar' + context;
  6. ok(!!caches, 'caches object should be available on global');
  7. caches.open(name).then(function(openCache) {
  8. ok(openCache instanceof Cache, 'cache object should be resolved from caches.open');
  9. return caches.has(name);
  10. }).then(function(hasResult) {
  11. ok(hasResult, 'caches.has() should resolve true');
  12. return caches.keys();
  13. }).then(function(keys) {
  14. ok(!!keys, 'caches.keys() should resolve to a truthy value');
  15. ok(keys.length >= 1, 'caches.keys() should resolve to an array of length at least 1');
  16. ok(keys.indexOf(name) >= 0, 'caches.keys() should resolve to an array containing key');
  17. return caches.delete(name);
  18. }).then(function(deleteResult) {
  19. ok(deleteResult, 'caches.delete() should resolve true');
  20. return caches.has(name);
  21. }).then(function(hasMissingCache) {
  22. ok(!hasMissingCache, 'missing key should return false from has');
  23. }).then(function() {
  24. return caches.open(name);
  25. }).then(function(snafu) {
  26. return snafu.keys();
  27. }).then(function(empty) {
  28. is(0, empty.length, 'cache.keys() should resolve to an array of length 0');
  29. }).then(function() {
  30. return caches.open(name);
  31. }).then(function(snafu) {
  32. var req = './cachekey';
  33. var res = new Response("Hello world");
  34. return snafu.put('ftp://invalid', res).then(function() {
  35. ok(false, 'This should fail');
  36. }).catch(function (err) {
  37. is(err.name, 'TypeError', 'put() should throw TypeError for invalid scheme');
  38. return snafu.put(req, res);
  39. }).then(function(v) {
  40. return snafu;
  41. });
  42. }).then(function(snafu) {
  43. return Promise.all([snafu, snafu.keys()]);
  44. }).then(function(args) {
  45. var snafu = args[0];
  46. var keys = args[1];
  47. is(1, keys.length, 'cache.keys() should resolve to an array of length 1');
  48. ok(keys[0] instanceof Request, 'key should be a Request');
  49. ok(keys[0].url.match(/cachekey$/), 'Request URL should match original');
  50. return Promise.all([snafu, snafu.match(keys[0]), snafu.match('ftp://invalid')]);
  51. }).then(function(args) {
  52. var snafu = args[0];
  53. var response = args[1];
  54. ok(response instanceof Response, 'value should be a Response');
  55. is(response.status, 200, 'Response status should be 200');
  56. is(undefined, args[2], 'Match with invalid scheme should resolve undefined');
  57. return Promise.all([snafu, snafu.put('./cachekey2', response)]);
  58. }).then(function(args) {
  59. var snafu = args[0]
  60. return snafu.match('./cachekey2');
  61. }).then(function(response) {
  62. return response.text().then(function(v) {
  63. is(v, "Hello world", "Response body should match original");
  64. });
  65. }).then(function() {
  66. // FIXME(nsm): Can't use a Request object for now since the operations
  67. // consume it's 'body'. See
  68. // https://github.com/slightlyoff/ServiceWorker/issues/510.
  69. return caches.open(foobar);
  70. }).then(function(openCache) {
  71. c = openCache;
  72. return c.put(request, response);
  73. }).then(function(putResponse) {
  74. is(putResponse, undefined, 'The promise should resolve to undefined');
  75. return c.keys(request);
  76. }).then(function(keys) {
  77. ok(keys, 'Valid keys object expected');
  78. is(keys.length, 1, 'Only one key is expected');
  79. return c.keys();
  80. }).then(function(keys) {
  81. ok(keys, 'Valid keys object expected');
  82. is(keys.length, 1, 'Only one key is expected');
  83. return c.matchAll(request);
  84. }).then(function(matchAllResponses) {
  85. ok(matchAllResponses, 'matchAll should succeed');
  86. is(matchAllResponses.length, 1, 'Only one match is expected');
  87. return c.match(request);
  88. }).then(function(matchResponse) {
  89. ok(matchResponse, 'match should succeed');
  90. return caches.match(request);
  91. }).then(function(storageMatchResponse) {
  92. ok(storageMatchResponse, 'storage match should succeed');
  93. return caches.match(request, {cacheName:foobar});
  94. }).then(function(storageMatchResponse) {
  95. ok(storageMatchResponse, 'storage match with cacheName should succeed');
  96. var request2 = new Request("http://example.com/hmm?q=snafu" + context);
  97. return c.match(request2, {ignoreSearch:true});
  98. }).then(function(match2Response) {
  99. ok(match2Response, 'match should succeed');
  100. return c.delete(request);
  101. }).then(function(deleteResult) {
  102. ok(deleteResult, 'delete should succeed');
  103. return c.keys();
  104. }).then(function(keys) {
  105. ok(keys, 'Valid keys object expected');
  106. is(keys.length, 0, 'Zero keys is expected');
  107. return c.matchAll(request);
  108. }).then(function(matchAll2Responses) {
  109. ok(matchAll2Responses, 'matchAll should succeed');
  110. is(matchAll2Responses.length, 0, 'Zero matches is expected');
  111. return caches.has(foobar);
  112. }).then(function(hasResult) {
  113. ok(hasResult, 'has should succeed');
  114. return caches.keys();
  115. }).then(function(keys) {
  116. ok(keys, 'Valid keys object expected');
  117. ok(keys.length >= 2, 'At least two keys are expected');
  118. ok(keys.indexOf(name) >= 0, 'snafu should exist');
  119. ok(keys.indexOf(foobar) >= keys.indexOf(name), 'foobar should come after it');
  120. return caches.delete(foobar);
  121. }).then(function(deleteResult) {
  122. ok(deleteResult, 'delete should succeed');
  123. return caches.has(foobar);
  124. }).then(function(hasMissingCache) {
  125. ok(!hasMissingCache, 'has should have a result');
  126. return caches.delete(name);
  127. }).then(function(deleteResult) {
  128. ok(deleteResult, 'delete should succeed');
  129. testDone();
  130. })