2015-03-16 22:43:09 +03:00
|
|
|
var request1 = new Request("//mochi.test:8888/?1&" + context + "#fragment");
|
2015-03-13 02:27:31 +03:00
|
|
|
var request2 = new Request("//mochi.test:8888/?2&" + context);
|
|
|
|
var request3 = new Request("//mochi.test:8888/?3&" + context);
|
2015-03-16 23:13:13 +03:00
|
|
|
var requestWithAltQS = new Request("//mochi.test:8888/?queryString");
|
2015-03-14 02:12:16 +03:00
|
|
|
var unknownRequest = new Request("//mochi.test:8888/non/existing/path?" + context);
|
2015-03-13 02:27:31 +03:00
|
|
|
var response1, response3;
|
|
|
|
var c;
|
|
|
|
var response1Text, response3Text;
|
|
|
|
var name = "matchAll-request" + context;
|
|
|
|
|
|
|
|
function checkResponse(r, response, responseText) {
|
|
|
|
ok(r !== response, "The objects should not be the same");
|
2015-03-16 22:43:09 +03:00
|
|
|
is(r.url, response.url.replace("#fragment", ""),
|
|
|
|
"The URLs should be the same");
|
2015-03-13 02:27:31 +03:00
|
|
|
is(r.status, response.status, "The status codes should be the same");
|
|
|
|
is(r.type, response.type, "The response types should be the same");
|
|
|
|
is(r.ok, response.ok, "Both responses should have succeeded");
|
|
|
|
is(r.statusText, response.statusText,
|
|
|
|
"Both responses should have the same status text");
|
|
|
|
return r.text().then(function(text) {
|
2015-03-17 03:40:01 +03:00
|
|
|
// Avoid dumping out the large response text to the log if they're equal.
|
|
|
|
if (text !== responseText) {
|
|
|
|
is(text, responseText, "The response body should be correct");
|
|
|
|
}
|
2015-03-13 02:27:31 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fetch(new Request(request1)).then(function(r) {
|
|
|
|
response1 = r;
|
|
|
|
return response1.text();
|
|
|
|
}).then(function(text) {
|
|
|
|
response1Text = text;
|
|
|
|
return fetch(new Request(request3));
|
|
|
|
}).then(function(r) {
|
|
|
|
response3 = r;
|
|
|
|
return response3.text();
|
|
|
|
}).then(function(text) {
|
|
|
|
response3Text = text;
|
2015-03-16 22:43:09 +03:00
|
|
|
return testRequest(request1, request2, request3, unknownRequest,
|
2015-03-16 23:13:13 +03:00
|
|
|
requestWithAltQS,
|
2015-03-16 22:43:09 +03:00
|
|
|
request1.url.replace("#fragment", "#other"));
|
2015-03-13 02:27:31 +03:00
|
|
|
}).then(function() {
|
2015-03-14 02:12:16 +03:00
|
|
|
return testRequest(request1.url, request2.url, request3.url,
|
2015-03-16 23:13:13 +03:00
|
|
|
unknownRequest.url, requestWithAltQS.url,
|
2015-03-16 22:43:09 +03:00
|
|
|
request1.url.replace("#fragment", "#other"));
|
2015-03-13 02:27:31 +03:00
|
|
|
}).then(function() {
|
|
|
|
testDone();
|
|
|
|
});
|
2015-03-14 01:15:20 +03:00
|
|
|
|
|
|
|
// The request arguments can either be a URL string, or a Request object.
|
2015-03-16 22:43:09 +03:00
|
|
|
function testRequest(request1, request2, request3, unknownRequest,
|
2015-03-16 23:13:13 +03:00
|
|
|
requestWithAlternateQueryString,
|
2015-03-16 22:43:09 +03:00
|
|
|
requestWithDifferentFragment) {
|
2015-03-14 01:15:20 +03:00
|
|
|
return caches.open(name).then(function(cache) {
|
|
|
|
c = cache;
|
|
|
|
return c.add(request1);
|
|
|
|
}).then(function() {
|
|
|
|
return c.add(request3);
|
2015-03-16 22:17:58 +03:00
|
|
|
}).then(function() {
|
|
|
|
return Promise.all(
|
|
|
|
["HEAD", "POST", "PUT", "DELETE", "OPTIONS"]
|
|
|
|
.map(function(method) {
|
|
|
|
var r = new Request(request1, {method: method});
|
|
|
|
return c.add(r)
|
|
|
|
.then(function() {
|
|
|
|
ok(false, "Promise should be rejected");
|
|
|
|
}, function(err) {
|
|
|
|
is(err.name, "TypeError", "Adding a request with type '" + method + "' should fail");
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
2015-03-14 01:15:20 +03:00
|
|
|
}).then(function() {
|
|
|
|
return c.matchAll(request1);
|
|
|
|
}).then(function(r) {
|
|
|
|
is(r.length, 1, "Should only find 1 item");
|
|
|
|
return checkResponse(r[0], response1, response1Text);
|
2015-03-17 12:58:00 +03:00
|
|
|
}).then(function() {
|
|
|
|
return c.matchAll(new Request(request1, {method: "HEAD"}));
|
2015-04-23 16:17:37 +03:00
|
|
|
}).then(function(r) {
|
2016-12-14 09:25:40 +03:00
|
|
|
is(r.length, 0, "Should be an empty array when match a HEAD request.");
|
2015-04-23 16:17:37 +03:00
|
|
|
return c.matchAll(new Request(request1, {method: "HEAD"}), {ignoreMethod: true});
|
2015-03-17 12:58:00 +03:00
|
|
|
}).then(function(r) {
|
|
|
|
is(r.length, 1, "Should only find 1 item");
|
|
|
|
return checkResponse(r[0], response1, response1Text);
|
|
|
|
}).then(function() {
|
|
|
|
return Promise.all(
|
2016-12-14 09:25:40 +03:00
|
|
|
["HEAD", "POST", "PUT", "DELETE", "OPTIONS"]
|
2015-03-17 12:58:00 +03:00
|
|
|
.map(function(method) {
|
|
|
|
var req = new Request(request1, {method: method});
|
|
|
|
return c.matchAll(req)
|
|
|
|
.then(function(r) {
|
2016-12-14 09:25:40 +03:00
|
|
|
is(r.length, 0, "Searching for a request with a non-GET method should not succeed");
|
2015-03-17 12:58:00 +03:00
|
|
|
return c.matchAll(req, {ignoreMethod: true});
|
|
|
|
}).then(function(r) {
|
|
|
|
is(r.length, 1, "Should only find 1 item");
|
|
|
|
return checkResponse(r[0], response1, response1Text);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
2015-03-14 01:15:20 +03:00
|
|
|
}).then(function() {
|
2015-03-16 22:43:09 +03:00
|
|
|
return c.matchAll(requestWithDifferentFragment);
|
|
|
|
}).then(function(r) {
|
|
|
|
is(r.length, 1, "Should only find 1 item");
|
|
|
|
return checkResponse(r[0], response1, response1Text);
|
|
|
|
}).then(function() {
|
2015-03-16 23:13:13 +03:00
|
|
|
return c.matchAll(requestWithAlternateQueryString,
|
2015-03-17 23:38:59 +03:00
|
|
|
{ignoreSearch: true});
|
2015-03-16 23:13:13 +03:00
|
|
|
}).then(function(r) {
|
|
|
|
is(r.length, 2, "Should find 2 items");
|
|
|
|
return Promise.all([
|
|
|
|
checkResponse(r[0], response1, response1Text),
|
|
|
|
checkResponse(r[1], response3, response3Text)
|
|
|
|
]);
|
|
|
|
}).then(function() {
|
2015-03-14 01:15:20 +03:00
|
|
|
return c.matchAll(request3);
|
|
|
|
}).then(function(r) {
|
|
|
|
is(r.length, 1, "Should only find 1 item");
|
|
|
|
return checkResponse(r[0], response3, response3Text);
|
|
|
|
}).then(function() {
|
|
|
|
return c.matchAll();
|
|
|
|
}).then(function(r) {
|
|
|
|
is(r.length, 2, "Should find 2 items");
|
|
|
|
return Promise.all([
|
|
|
|
checkResponse(r[0], response1, response1Text),
|
|
|
|
checkResponse(r[1], response3, response3Text)
|
|
|
|
]);
|
|
|
|
}).then(function() {
|
2015-03-17 23:38:59 +03:00
|
|
|
return caches.match(request1, {cacheName: name + "mambojambo"})
|
|
|
|
.then(function() {
|
2016-05-11 04:52:48 +03:00
|
|
|
is(typeof r, "undefined", 'Searching in the wrong cache should resolve to undefined');
|
|
|
|
return caches.has(name + "mambojambo");
|
|
|
|
}).then(function(hasCache) {
|
|
|
|
ok(!hasCache, 'The wrong cache should still not exist');
|
2015-03-17 23:38:59 +03:00
|
|
|
});
|
2015-03-14 01:15:20 +03:00
|
|
|
}).then(function() {
|
2015-03-14 02:12:16 +03:00
|
|
|
return c.matchAll(unknownRequest);
|
|
|
|
}).then(function(r) {
|
|
|
|
is(r.length, 0, "Searching for an unknown request should not succeed");
|
2015-03-17 23:38:59 +03:00
|
|
|
return caches.match(unknownRequest, {cacheName: name});
|
2015-03-14 02:12:16 +03:00
|
|
|
}).then(function(r) {
|
2015-03-17 23:38:59 +03:00
|
|
|
is(typeof r, "undefined", "Searching for an unknown request should not succeed");
|
|
|
|
// Make sure that cacheName is ignored on Cache
|
|
|
|
return c.matchAll(request1, {cacheName: name + "mambojambo"});
|
|
|
|
}).then(function(r) {
|
|
|
|
is(r.length, 1, "Should only find 1 item");
|
|
|
|
return checkResponse(r[0], response1, response1Text);
|
|
|
|
}).then(function() {
|
2015-03-14 01:15:20 +03:00
|
|
|
return caches.delete(name);
|
|
|
|
}).then(function(success) {
|
|
|
|
ok(success, "We should be able to delete the cache successfully");
|
|
|
|
// Make sure that the cache is still usable after deletion.
|
|
|
|
return c.matchAll(request1);
|
|
|
|
}).then(function(r) {
|
|
|
|
is(r.length, 1, "Should only find 1 item");
|
|
|
|
return checkResponse(r[0], response1, response1Text);
|
|
|
|
}).then(function() {
|
|
|
|
return c.matchAll(request3);
|
|
|
|
}).then(function(r) {
|
|
|
|
is(r.length, 1, "Should only find 1 item");
|
|
|
|
return checkResponse(r[0], response3, response3Text);
|
|
|
|
}).then(function() {
|
|
|
|
return c.matchAll();
|
|
|
|
}).then(function(r) {
|
|
|
|
is(r.length, 2, "Should find 2 items");
|
|
|
|
return Promise.all([
|
|
|
|
checkResponse(r[0], response1, response1Text),
|
|
|
|
checkResponse(r[1], response3, response3Text)
|
|
|
|
]);
|
|
|
|
}).then(function() {
|
|
|
|
// Now, drop the cache, reopen and verify that we can't find the request any more.
|
|
|
|
c = null;
|
|
|
|
return caches.open(name);
|
|
|
|
}).then(function(cache) {
|
|
|
|
return cache.matchAll();
|
2015-03-16 02:36:39 +03:00
|
|
|
}).then(function(r) {
|
|
|
|
is(r.length, 0, "Searching in the cache after deletion should not succeed");
|
2015-03-20 03:19:11 +03:00
|
|
|
return caches.delete(name);
|
|
|
|
}).then(function(deleted) {
|
|
|
|
ok(deleted, "The cache should be deleted successfully");
|
2015-03-14 01:15:20 +03:00
|
|
|
});
|
|
|
|
}
|