Skip to content

Commit

Permalink
Cache Storage API tests: Fix WPT test bugs, remove redundant local co…
Browse files Browse the repository at this point in the history
…pies

* Use promise_rejects consistently and fix parameters.
* Don't use .php in upstreamed tests.
* Add additional test cases from blink's copies.
* Tightened up assertions about result array ordering.
* Remove blink's redundant copies of tests.

I left the credentials tests alone since Blink is mid-deprecation.

BUG=655479
[email protected]

Review-Url: https://codereview.chromium.org/2790143003
Cr-Commit-Position: refs/heads/master@{#462593}
  • Loading branch information
inexorabletash authored and chromium-wpt-export-bot committed Apr 10, 2017
1 parent 8fbf3d4 commit c041e12
Show file tree
Hide file tree
Showing 12 changed files with 662 additions and 44 deletions.
24 changes: 24 additions & 0 deletions service-workers/cache-storage/resources/test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,30 @@ function assert_response_in_array(actual, expected_array, description) {
}), description);
}

// Helper for testing with Request objects. Compares simple
// attributes defined on the interfaces, as well as the headers.
function assert_request_equals(actual, expected, description) {
assert_class_string(actual, "Request", description);
["url"].forEach(function(attribute) {
assert_equals(actual[attribute], expected[attribute],
description + " Attributes differ: " + attribute + ".");
});
assert_header_equals(actual.headers, expected.headers, description);
}

// Asserts that two arrays |actual| and |expected| contain the same
// set of Requests as determined by assert_request_equals(). The
// corresponding elements must occupy corresponding indices in their
// respective arrays.
function assert_request_array_equals(actual, expected, description) {
assert_true(Array.isArray(actual), description);
assert_equals(actual.length, expected.length, description);
actual.forEach(function(value, index) {
assert_request_equals(value, expected[index],
description + " : object[" + index + "]");
});
}

// Deletes all caches, returning a promise indicating success.
function delete_all_caches() {
return self.caches.keys()
Expand Down
5 changes: 3 additions & 2 deletions service-workers/cache-storage/script-tests/cache-add.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ cache_test(function(cache) {
}, 'Cache.add with request with null body (not consumed)');

cache_test(function(cache, test) {
return assert_promise_rejects(
return promise_rejects(
test,
new TypeError(),
cache.add('../resources/fetch-status.py?status=206'),
Expand All @@ -98,6 +98,7 @@ cache_test(function(cache, test) {
return new Request(url);
});
return promise_rejects(
test,
new TypeError(),
cache.addAll(requests),
'Cache.addAll should reject with TypeError if any request fails');
Expand All @@ -116,7 +117,7 @@ cache_test(function(cache, test) {
return promise_rejects(
test,
new TypeError(),
cache.add('../resources/fetch-status.php?status=500'),
cache.add('../resources/fetch-status.py?status=500'),
'Cache.add should reject if response is !ok');
}, 'Cache.add with request that results in a status of 500');

Expand Down
99 changes: 74 additions & 25 deletions service-workers/cache-storage/script-tests/cache-delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,39 @@ cache_test(function(cache) {
.then(function(result) {
assert_response_equals(result, response,
'Cache.delete should leave non-matching response in the cache.');
return cache.delete(new Request(test_url, {method: 'HEAD'}),
{ignoreMethod: true});
})
.then(function(result) {
assert_true(result,
'Cache.delete should match a non-GET request ' +
' if ignoreMethod is true.');
});
}, 'Cache.delete called with a HEAD request');

cache_test(function(cache) {
var vary_request = new Request('http://example.com/c',
{headers: {'Cookies': 'is-for-cookie'}});
var vary_response = new Response('', {headers: {'Vary': 'Cookies'}});
var mismatched_vary_request = new Request('http://example.com/c');

return cache.put(vary_request.clone(), vary_response.clone())
.then(function() {
return cache.delete(mismatched_vary_request.clone());
})
.then(function(result) {
assert_false(result,
'Cache.delete should not delete if vary does not ' +
'match unless ignoreVary is true');
return cache.delete(mismatched_vary_request.clone(),
{ignoreVary: true});
})
.then(function(result) {
assert_true(result,
'Cache.delete should ignore vary if ignoreVary is true');
});
}, 'Cache.delete supports ignoreVary');

cache_test(function(cache) {
return cache.delete(test_url)
.then(function(result) {
Expand All @@ -83,33 +113,52 @@ cache_test(function(cache) {
});
}, 'Cache.delete with a non-existent entry');

var cache_entries = {
a: {
request: new Request('http://example.com/abc'),
response: new Response('')
prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.matchAll(entries.a_with_query.request,
{ ignoreSearch: true })
.then(function(result) {
assert_response_array_equals(
result,
[
entries.a.response,
entries.a_with_query.response
]);
return cache.delete(entries.a_with_query.request,
{ ignoreSearch: true });
})
.then(function(result) {
return cache.matchAll(entries.a_with_query.request,
{ ignoreSearch: true });
})
.then(function(result) {
assert_response_array_equals(result, []);
});
},
'Cache.delete with ignoreSearch option (request with search parameters)');

b: {
request: new Request('http://example.com/b'),
response: new Response('')
prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.matchAll(entries.a_with_query.request,
{ ignoreSearch: true })
.then(function(result) {
assert_response_array_equals(
result,
[
entries.a.response,
entries.a_with_query.response
]);
// cache.delete()'s behavior should be the same if ignoreSearch is
// not provided or if ignoreSearch is false.
return cache.delete(entries.a_with_query.request,
{ ignoreSearch: false });
})
.then(function(result) {
return cache.matchAll(entries.a_with_query.request,
{ ignoreSearch: true });
})
.then(function(result) {
assert_response_array_equals(result, [ entries.a.response ]);
});
},

a_with_query: {
request: new Request('http://example.com/abc?q=r'),
response: new Response('')
}
};

function prepopulated_cache_test(test_function, description) {
cache_test(function(cache) {
return Promise.all(Object.keys(cache_entries).map(function(k) {
return cache.put(cache_entries[k].request.clone(),
cache_entries[k].response.clone());
}))
.then(function() {
return test_function(cache);
});
}, description);
}
'Cache.delete with ignoreSearch option (when it is specified as false)');

done();
192 changes: 192 additions & 0 deletions service-workers/cache-storage/script-tests/cache-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
if (self.importScripts) {
importScripts('/resources/testharness.js');
importScripts('../resources/test-helpers.js');
}

cache_test(cache => {
return cache.keys()
.then(requests => {
assert_equals(
requests.length, 0,
'Cache.keys should resolve to an empty array for an empty cache');
});
}, 'Cache.keys() called on an empty cache');

prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.keys('not-present-in-the-cache')
.then(function(result) {
assert_request_array_equals(
result, [],
'Cache.keys should resolve with an empty array on failure.');
});
}, 'Cache.keys with no matching entries');

prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.keys(entries.a.request.url)
.then(function(result) {
assert_request_array_equals(result, [entries.a.request],
'Cache.keys should match by URL.');
});
}, 'Cache.keys with URL');

prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.keys(entries.a.request)
.then(function(result) {
assert_request_array_equals(
result, [entries.a.request],
'Cache.keys should match by Request.');
});
}, 'Cache.keys with Request');

prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.keys(new Request(entries.a.request.url))
.then(function(result) {
assert_request_array_equals(
result, [entries.a.request],
'Cache.keys should match by Request.');
});
}, 'Cache.keys with new Request');

prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.keys(entries.a.request, {ignoreSearch: true})
.then(function(result) {
assert_request_array_equals(
result,
[
entries.a.request,
entries.a_with_query.request
],
'Cache.keys with ignoreSearch should ignore the ' +
'search parameters of cached request.');
});
},
'Cache.keys with ignoreSearch option (request with no search ' +
'parameters)');

prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.keys(entries.a_with_query.request, {ignoreSearch: true})
.then(function(result) {
assert_request_array_equals(
result,
[
entries.a.request,
entries.a_with_query.request
],
'Cache.keys with ignoreSearch should ignore the ' +
'search parameters of request.');
});
},
'Cache.keys with ignoreSearch option (request with search parameters)');

cache_test(function(cache) {
var request = new Request('http://example.com/');
var head_request = new Request('http://example.com/', {method: 'HEAD'});
var response = new Response('foo');
return cache.put(request.clone(), response.clone())
.then(function() {
return cache.keys(head_request.clone());
})
.then(function(result) {
assert_request_array_equals(
result, [],
'Cache.keys should resolve with an empty array with a ' +
'mismatched method.');
return cache.keys(head_request.clone(),
{ignoreMethod: true});
})
.then(function(result) {
assert_request_array_equals(
result,
[
request,
],
'Cache.keys with ignoreMethod should ignore the ' +
'method of request.');
});
}, 'Cache.keys supports ignoreMethod');

cache_test(function(cache) {
var vary_request = new Request('http://example.com/c',
{headers: {'Cookies': 'is-for-cookie'}});
var vary_response = new Response('', {headers: {'Vary': 'Cookies'}});
var mismatched_vary_request = new Request('http://example.com/c');

return cache.put(vary_request.clone(), vary_response.clone())
.then(function() {
return cache.keys(mismatched_vary_request.clone());
})
.then(function(result) {
assert_request_array_equals(
result, [],
'Cache.keys should resolve with an empty array with a ' +
'mismatched vary.');
return cache.keys(mismatched_vary_request.clone(),
{ignoreVary: true});
})
.then(function(result) {
assert_request_array_equals(
result,
[
vary_request,
],
'Cache.keys with ignoreVary should ignore the ' +
'vary of request.');
});
}, 'Cache.keys supports ignoreVary');

prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.keys(entries.cat.request.url + '#mouse')
.then(function(result) {
assert_request_array_equals(
result,
[
entries.cat.request,
],
'Cache.keys should ignore URL fragment.');
});
}, 'Cache.keys with URL containing fragment');

prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.keys('http')
.then(function(result) {
assert_request_array_equals(
result, [],
'Cache.keys should treat query as a URL and not ' +
'just a string fragment.');
});
}, 'Cache.keys with string fragment "http" as query');

prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.keys()
.then(function(result) {
assert_request_array_equals(
result,
[
entries.a.request,
entries.b.request,
entries.a_with_query.request,
entries.A.request,
entries.a_https.request,
entries.a_org.request,
entries.cat.request,
entries.catmandu.request,
entries.cat_num_lives.request,
entries.cat_in_the_hat.request,
entries.non_2xx_response.request,
entries.error_response.request
],
'Cache.keys without parameters should match all entries.');
});
}, 'Cache.keys without parameters');

prepopulated_cache_test(simple_entries, function(cache, entries) {
return cache.keys(new Request(entries.cat.request.url, {method: 'HEAD'}))
.then(function(result) {
assert_request_array_equals(
result, [],
'Cache.keys should not match HEAD request unless ignoreMethod ' +
'option is set.');
});
}, 'Cache.keys with a HEAD Request');

done();
Loading

0 comments on commit c041e12

Please sign in to comment.