test_cache_match_request.js 5.5 KB

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