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..dfc23eab9c387d13eb924faa1d705e19ddbc9b29 |
| --- /dev/null |
| +++ b/LayoutTests/http/tests/serviceworker/resources/cache-add-worker.js |
| @@ -0,0 +1,146 @@ |
| +importScripts('worker-test-harness.js'); |
| + |
| +promise_test(function(t) { |
| + return create_temporary_cache(t) |
| + .then(function(cache) { |
| + return assert_promise_throws( |
| + cache.add(''), |
| + '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 assert_promise_throws( |
| + cache.add('http://example.com/cow-goes-moo', |
| + 'http://example.com/another-cow-goes-moo'), |
| + 'TypeError', |
| + 'Cache.add should throw a TypeError when more than one argument ' + |
|
jsbell
2014/08/20 18:18:08
Per https://github.com/slightlyoff/ServiceWorker/i
asanka
2014/08/20 21:24:58
Makes sense. Removed.
|
| + 'is given.'); |
| + }); |
| + }, 'Cache.add with more than one argument'); |
| + |
| +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_throws( |
| + cache.add('this-does-not-exist-please-do-not-create-it'), |
| + '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_throws( |
| + cache.addAll(), |
| + '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 created_cache; |
| + |
| + return create_temporary_cache(t) |
| + .then(function(cache) { |
| + created_cache = cache; |
| + return assert_promise_throws( |
| + cache.addAll(urls), |
| + 'TypeError', |
| + 'Cache.addAll should throw TypeError if any of the requests fail.'); |
| + }) |
| + |
|
jsbell
2014/08/20 18:18:08
nit: remove blank line
asanka
2014/08/20 21:24:58
Done.
|
| + .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) { |
| + if (url.match(/:\/\//)) { |
| + return url; |
| + } else { |
| + return self.location.href.replace(/[^/]+$/, url); |
|
jsbell
2014/08/20 18:18:08
Hrm, looks like it's still not using URL()?
asanka
2014/08/20 21:24:58
Done. Now I'll have to go hunt down what I remembe
|
| + } |
| +} |
| + |