Chromium Code Reviews| 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..4729013ac19a1b66b7659c8dcf0327c4eabaa2b3 |
| --- /dev/null |
| +++ b/LayoutTests/http/tests/serviceworker/resources/cache-add-worker.js |
| @@ -0,0 +1,130 @@ |
| +importScripts('worker-testharness.js'); |
| +importScripts('/resources/testharness-helpers.js'); |
| + |
| +promise_test(function(t) { |
| + return create_temporary_cache(t) |
| + .then(function(cache) { |
| + return assert_promise_rejects( |
| + cache.add(''), |
|
jsbell
2014/10/20 18:20:40
This is an empty string, not "no arguments"; can y
asanka
2014/10/22 22:35:31
Changed to cache.add(), but shouldn't that be equi
jsbell
2014/10/22 23:54:44
No - the spec prose about delegating to addAll() a
|
| + 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)); |
|
jsbell
2014/10/20 18:20:40
You could move this .then out a level in the promi
asanka
2014/10/22 22:35:31
Transitioned to a cache_test() helper function whi
|
| + }); |
| + }, '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)); |
|
jsbell
2014/10/20 18:20:40
Ditto.
asanka
2014/10/22 22:35:31
Yup. But obsolete due to the introduction of cache
|
| + }); |
| + }, 'Cache.addAll with Request arguments'); |
| + |
| +promise_test(function(t) { |
| + // Assumes that simple.txt and blank.html are existing resources. The second |
| + // resource does not. |
|
jsbell
2014/10/20 18:20:41
Grammar nit: "are existing ... does not." -> "are
asanka
2014/10/22 22:35:31
Done.
|
| + 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; |
| +} |
| + |