test_cache_matchAll_request.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. var request1 = new Request("//mochi.test:8888/?1&" + context + "#fragment");
  2. var request2 = new Request("//mochi.test:8888/?2&" + context);
  3. var request3 = new Request("//mochi.test:8888/?3&" + context);
  4. var requestWithAltQS = new Request("//mochi.test:8888/?queryString");
  5. var unknownRequest = new Request("//mochi.test:8888/non/existing/path?" + context);
  6. var response1, response3;
  7. var c;
  8. var response1Text, response3Text;
  9. var name = "matchAll-request" + context;
  10. function checkResponse(r, response, responseText) {
  11. ok(r !== response, "The objects should not be the same");
  12. is(r.url, response.url.replace("#fragment", ""),
  13. "The URLs should be the same");
  14. is(r.status, response.status, "The status codes should be the same");
  15. is(r.type, response.type, "The response types should be the same");
  16. is(r.ok, response.ok, "Both responses should have succeeded");
  17. is(r.statusText, response.statusText,
  18. "Both responses should have the same status text");
  19. return r.text().then(function(text) {
  20. // Avoid dumping out the large response text to the log if they're equal.
  21. if (text !== responseText) {
  22. is(text, responseText, "The response body should be correct");
  23. }
  24. });
  25. }
  26. fetch(new Request(request1)).then(function(r) {
  27. response1 = r;
  28. return response1.text();
  29. }).then(function(text) {
  30. response1Text = text;
  31. return fetch(new Request(request3));
  32. }).then(function(r) {
  33. response3 = r;
  34. return response3.text();
  35. }).then(function(text) {
  36. response3Text = text;
  37. return testRequest(request1, request2, request3, unknownRequest,
  38. requestWithAltQS,
  39. request1.url.replace("#fragment", "#other"));
  40. }).then(function() {
  41. return testRequest(request1.url, request2.url, request3.url,
  42. unknownRequest.url, requestWithAltQS.url,
  43. request1.url.replace("#fragment", "#other"));
  44. }).then(function() {
  45. testDone();
  46. });
  47. // The request arguments can either be a URL string, or a Request object.
  48. function testRequest(request1, request2, request3, unknownRequest,
  49. requestWithAlternateQueryString,
  50. requestWithDifferentFragment) {
  51. return caches.open(name).then(function(cache) {
  52. c = cache;
  53. return c.add(request1);
  54. }).then(function() {
  55. return c.add(request3);
  56. }).then(function() {
  57. return Promise.all(
  58. ["HEAD", "POST", "PUT", "DELETE", "OPTIONS"]
  59. .map(function(method) {
  60. var r = new Request(request1, {method: method});
  61. return c.add(r)
  62. .then(function() {
  63. ok(false, "Promise should be rejected");
  64. }, function(err) {
  65. is(err.name, "TypeError", "Adding a request with type '" + method + "' should fail");
  66. });
  67. })
  68. );
  69. }).then(function() {
  70. return c.matchAll(request1);
  71. }).then(function(r) {
  72. is(r.length, 1, "Should only find 1 item");
  73. return checkResponse(r[0], response1, response1Text);
  74. }).then(function() {
  75. return c.matchAll(new Request(request1, {method: "HEAD"}));
  76. }).then(function(r) {
  77. is(r.length, 1, "Should only find 1 item");
  78. return checkResponse(r[0], response1, "");
  79. }).then(function() {
  80. return c.matchAll(new Request(request1, {method: "HEAD"}), {ignoreMethod: true});
  81. }).then(function(r) {
  82. is(r.length, 1, "Should only find 1 item");
  83. return checkResponse(r[0], response1, response1Text);
  84. }).then(function() {
  85. return Promise.all(
  86. ["POST", "PUT", "DELETE", "OPTIONS"]
  87. .map(function(method) {
  88. var req = new Request(request1, {method: method});
  89. return c.matchAll(req)
  90. .then(function(r) {
  91. is(r.length, 0, "Searching for a request with a non-GET/HEAD method should not succeed");
  92. return c.matchAll(req, {ignoreMethod: true});
  93. }).then(function(r) {
  94. is(r.length, 1, "Should only find 1 item");
  95. return checkResponse(r[0], response1, response1Text);
  96. });
  97. })
  98. );
  99. }).then(function() {
  100. return c.matchAll(requestWithDifferentFragment);
  101. }).then(function(r) {
  102. is(r.length, 1, "Should only find 1 item");
  103. return checkResponse(r[0], response1, response1Text);
  104. }).then(function() {
  105. return c.matchAll(requestWithAlternateQueryString,
  106. {ignoreSearch: true});
  107. }).then(function(r) {
  108. is(r.length, 2, "Should find 2 items");
  109. return Promise.all([
  110. checkResponse(r[0], response1, response1Text),
  111. checkResponse(r[1], response3, response3Text)
  112. ]);
  113. }).then(function() {
  114. return c.matchAll(request3);
  115. }).then(function(r) {
  116. is(r.length, 1, "Should only find 1 item");
  117. return checkResponse(r[0], response3, response3Text);
  118. }).then(function() {
  119. return c.matchAll();
  120. }).then(function(r) {
  121. is(r.length, 2, "Should find 2 items");
  122. return Promise.all([
  123. checkResponse(r[0], response1, response1Text),
  124. checkResponse(r[1], response3, response3Text)
  125. ]);
  126. }).then(function() {
  127. return caches.match(request1, {cacheName: name + "mambojambo"})
  128. .then(function() {
  129. is(typeof r, "undefined", 'Searching in the wrong cache should resolve to undefined');
  130. return caches.has(name + "mambojambo");
  131. }).then(function(hasCache) {
  132. ok(!hasCache, 'The wrong cache should still not exist');
  133. });
  134. }).then(function() {
  135. return c.matchAll(unknownRequest);
  136. }).then(function(r) {
  137. is(r.length, 0, "Searching for an unknown request should not succeed");
  138. return caches.match(unknownRequest, {cacheName: name});
  139. }).then(function(r) {
  140. is(typeof r, "undefined", "Searching for an unknown request should not succeed");
  141. // Make sure that cacheName is ignored on Cache
  142. return c.matchAll(request1, {cacheName: name + "mambojambo"});
  143. }).then(function(r) {
  144. is(r.length, 1, "Should only find 1 item");
  145. return checkResponse(r[0], response1, response1Text);
  146. }).then(function() {
  147. return caches.delete(name);
  148. }).then(function(success) {
  149. ok(success, "We should be able to delete the cache successfully");
  150. // Make sure that the cache is still usable after deletion.
  151. return c.matchAll(request1);
  152. }).then(function(r) {
  153. is(r.length, 1, "Should only find 1 item");
  154. return checkResponse(r[0], response1, response1Text);
  155. }).then(function() {
  156. return c.matchAll(request3);
  157. }).then(function(r) {
  158. is(r.length, 1, "Should only find 1 item");
  159. return checkResponse(r[0], response3, response3Text);
  160. }).then(function() {
  161. return c.matchAll();
  162. }).then(function(r) {
  163. is(r.length, 2, "Should find 2 items");
  164. return Promise.all([
  165. checkResponse(r[0], response1, response1Text),
  166. checkResponse(r[1], response3, response3Text)
  167. ]);
  168. }).then(function() {
  169. // Now, drop the cache, reopen and verify that we can't find the request any more.
  170. c = null;
  171. return caches.open(name);
  172. }).then(function(cache) {
  173. return cache.matchAll();
  174. }).then(function(r) {
  175. is(r.length, 0, "Searching in the cache after deletion should not succeed");
  176. return caches.delete(name);
  177. }).then(function(deleted) {
  178. ok(deleted, "The cache should be deleted successfully");
  179. });
  180. }