| Index: LayoutTests/http/tests/serviceworker/resources/cache-add-worker.js
|
| diff --git a/LayoutTests/http/tests/serviceworker/resources/cache-add-worker.js b/LayoutTests/http/tests/serviceworker/resources/cache-add-worker.js
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..2555c5f17ae634eef5c2460bf7dfc45933e6b250
|
| --- /dev/null
|
| +++ b/LayoutTests/http/tests/serviceworker/resources/cache-add-worker.js
|
| @@ -0,0 +1,129 @@
|
| +importScripts('worker-test-harness.js');
|
| +
|
| +promise_test(function(t) {
|
| + return create_temporary_cache(t)
|
| + .then(function(cache) {
|
| + return assert_promise_rejects(
|
| + cache.add(''),
|
| + new TypeError(),
|
| + 'Cache.add should throw a TypeError when no arguments are given.');
|
| + });
|
| + }, 'Cache.add with no arguments');
|
| +
|
| +promise_test(function(t) {
|
| + return create_temporary_cache(t)
|
| + .then(function(cache) {
|
| + return cache.add('simple.txt')
|
| + .then(function(result) {
|
| + assert_true(result instanceof Response,
|
| + 'Cache.add should return a Response on success.');
|
| + assert_equals(result.status, 200,
|
| + 'Cache.add should not fulfill until response ' +
|
| + 'headers are received.');
|
| + });
|
| + });
|
| + }, 'Cache.add with successful request');
|
| +
|
| +promise_test(function(t) {
|
| + return create_temporary_cache(t)
|
| + .then(function(cache) {
|
| + return assert_promise_rejects(
|
| + cache.add('this-does-not-exist-please-do-not-create-it'),
|
| + new TypeError(),
|
| + 'Cache.add should throw TypeError if the resource does not exist.');
|
| + });
|
| + }, 'Cache.add with resource that results in a status of 404');
|
| +
|
| +promise_test(function(t) {
|
| + return create_temporary_cache(t)
|
| + .then(function(cache) {
|
| + return assert_promise_rejects(
|
| + cache.addAll(),
|
| + new TypeError(),
|
| + 'Cache.addAll should throw TypeError if there are no arguments');
|
| + });
|
| + }, 'Cache.addAll with no arguments');
|
| +
|
| +promise_test(function(t) {
|
| + // Assumes the existence of simple.txt and blank.html in the same directory
|
| + // as this test script.
|
| + var urls = ['simple.txt', self.location.href, 'blank.html'];
|
| + return create_temporary_cache(t)
|
| + .then(function(cache) {
|
| + return cache.addAll(urls)
|
| + .then(verify_response_array_against_urls.bind(undefined, urls));
|
| + });
|
| + }, 'Cache.addAll with ScalarValueString arguments');
|
| +
|
| +promise_test(function(t) {
|
| + // Assumes the existence of simple.txt and blank.html in the same directory
|
| + // as this test script.
|
| + var urls = ['simple.txt', self.location.href, 'blank.html'];
|
| + var requests = urls.map(function(url) {
|
| + return new Request(url, {method: 'GET'});
|
| + });
|
| + return create_temporary_cache(t)
|
| + .then(function(cache) {
|
| + return cache.addAll(requests)
|
| + .then(verify_response_array_against_urls.bind(undefined, urls));
|
| + });
|
| + }, 'Cache.addAll with Request arguments');
|
| +
|
| +promise_test(function(t) {
|
| + // Assumes that simple.txt and blank.html are existing resources. The second
|
| + // resource does not.
|
| + var urls = ['simple.txt', 'this-resource-should-not-exist', 'blank.html'];
|
| +
|
| + var cache;
|
| + return create_temporary_cache(t)
|
| + .then(function(created_cache) {
|
| + cache = created_cache;
|
| + return assert_promise_rejects(
|
| + cache.addAll(urls),
|
| + new TypeError(),
|
| + 'Cache.addAll should throw TypeError if any of the requests fail.');
|
| + })
|
| + .then(function() {
|
| + return cache.keys();
|
| + })
|
| + .then(function(result) {
|
| + assert_equals(result.length, 0,
|
| + 'Cache.addAll should not add any requests in a ' +
|
| + 'failing batch.');
|
| + });
|
| + }, 'Cache.addAll with failing request');
|
| +
|
| +// Helpers ---
|
| +
|
| +// Asserts that |results| is an Array of Response objects that have URLs
|
| +// corresponding to |urls|. Used to verify the output of Cache.addAll.
|
| +function verify_response_array_against_urls(urls, results) {
|
| + assert_true(Array.isArray(result),
|
| + 'Cache.addAll should resolve to an array.');
|
| + assert_equals(result.length, urls.length,
|
| + 'Cache.addAll should resolve to an array whose ' +
|
| + 'length is the same as the number of requests that ' +
|
| + 'were added.');
|
| +
|
| + var result_urls =
|
| + response.map(function(response) {
|
| + assert_true(response instanceof Response,
|
| + 'Cache.addAll should resolve to an array of ' +
|
| + 'Responses.');
|
| + return response.url;
|
| + });
|
| +
|
| + var request_urls = urls.map(absolute_url);
|
| +
|
| + assert_array_equals(
|
| + result_urls, request_urls,
|
| + 'Cache.addAll should resolve to an array of Response objects ' +
|
| + 'that correspond to the input requests.');
|
| +}
|
| +
|
| +// Returns an absolute URL if the input URL is either already an absolute URL or
|
| +// is a single component that should be resolved relative to self.location.href.
|
| +function absolute_url(url) {
|
| + return new URL(url, self.location.href).href;
|
| +}
|
| +
|
|
|