Chromium Code Reviews| Index: LayoutTests/http/tests/serviceworker/resources/cache-add-test-worker.js |
| diff --git a/LayoutTests/http/tests/serviceworker/resources/cache-add-test-worker.js b/LayoutTests/http/tests/serviceworker/resources/cache-add-test-worker.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fab6d90403c4c598ac9c02c37eee53f872342e1b |
| --- /dev/null |
| +++ b/LayoutTests/http/tests/serviceworker/resources/cache-add-test-worker.js |
| @@ -0,0 +1,124 @@ |
| +importScripts('worker-test-harness.js') |
| + |
| +promise_test(function(t) { |
| + return expect_promise_throws( |
| + t, new Cache().add(''), |
| + 'TypeError', |
| + 'Cache.add should throw a TypeError when no arguments are given.'); |
| + }, 'Cache.add with no arguments'); |
| + |
| +promise_test(function(t) { |
| + return expect_promise_throws( |
| + t, new 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 is ' + |
|
jsbell
2014/08/15 20:49:32
Interesting... it's unusual for a Web API to throw
|
| + 'given.'); |
| + }, 'Cache.add with more than one argument'); |
| + |
| +promise_test(function(t) { |
| + return new Cache().add('simple.txt') |
| + .then(t.step_func(function(result) { |
|
jsbell
2014/08/15 20:49:33
t.step_func should be unnecessary here, since a th
asanka
2014/08/20 03:11:57
Done.
|
| + assert_true(result instanceof Response, |
| + 'Cache.add should return a Response on success.'); |
| + assert_equals(result.status, 200, |
| + 'Cache.add should not fulfil until response ' + |
|
jsbell
2014/08/15 20:49:33
Nit: 'fulfill' ?
Apparently this is an American v
asanka
2014/08/20 03:11:57
Changed to fulfill :)
|
| + 'headers are received.'); |
| + })); |
| + }, 'Cache.add with successful request'); |
| + |
| +promise_test(function(t) { |
| + return expect_promise_throws( |
| + t, |
| + new 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 expect_promise_throws( |
| + t, |
| + new 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 |
|
jsbell
2014/08/15 20:49:33
nit: line length (80 cols)
asanka
2014/08/20 03:11:57
Done.
|
| + // this test script. |
| + var urls = ['simple.txt', self.location.href, 'blank.html']; |
| + return new Cache().addAll(urls) |
| + .then(t.step_func(verify_response_array_against_urls.bind(undefined, urls))); |
|
jsbell
2014/08/15 20:49:32
t.step_func should be unnecessary
asanka
2014/08/20 03:11:58
Removed.
|
| + }, 'Cache.addAll with ScalarValueString arguments'); |
| + |
| +promise_test(function(t) { |
| + // Assumes the existence of simple.txt and blank.html in the same directory as |
|
jsbell
2014/08/15 20:49:33
nit: line length (80 cols)
asanka
2014/08/20 03:11:57
Done.
|
| + // 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 new Cache().addAll(requests) |
| + .then(t.step_func(verify_response_array_against_urls.bind(undefined, urls))); |
|
jsbell
2014/08/15 20:49:32
t.step_func should be unnecessary
jsbell
2014/08/15 20:49:33
nit: line length (80 cols)
asanka
2014/08/20 03:11:57
Done. Also fixed line length.
|
| + }, '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 = new Cache(); |
| + |
| + return expect_promise_throws( |
| + t, |
| + cache.addAll(urls), |
| + 'TypeError', |
| + 'Cache.addAll should throw TypeError if any of the requests fail.') |
| + |
|
jsbell
2014/08/15 20:49:32
nit: remove blank line
asanka
2014/08/20 03:11:57
Done.
|
| + .then(function() { |
| + return cache.keys(); |
| + }) |
| + .then(t.step_func(function(result) { |
|
jsbell
2014/08/15 20:49:33
t.step_func should be unnecessary
asanka
2014/08/20 03:11:57
Removed.
|
| + 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) { |
|
jsbell
2014/08/15 20:49:33
This can just be:
return new URL(url, self.locati
asanka
2014/08/20 03:11:58
Done. I wasn't sure whether to use URL since it's
jsbell
2014/08/20 18:18:08
Paranoia is good. :) But any browser that supports
|
| + if (url.match(/:\/\//)) { |
| + return url; |
| + } else { |
| + return self.location.href.replace(/[^/]+$/, url); |
| + } |
| +} |
| + |