Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: LayoutTests/http/tests/serviceworker/resources/cache-add-worker.js

Issue 425413002: [ServiceWorker] Tests for Cache (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: CacheStorage.{create => open} Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 importScripts('worker-testharness.js');
2 importScripts('/resources/testharness-helpers.js');
3
4 promise_test(function(t) {
5 return create_temporary_cache(t)
6 .then(function(cache) {
7 return assert_promise_rejects(
8 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
9 new TypeError(),
10 'Cache.add should throw a TypeError when no arguments are given.');
11 });
12 }, 'Cache.add with no arguments');
13
14 promise_test(function(t) {
15 return create_temporary_cache(t)
16 .then(function(cache) {
17 return cache.add('simple.txt')
18 .then(function(result) {
19 assert_true(result instanceof Response,
20 'Cache.add should return a Response on success.');
21 assert_equals(result.status, 200,
22 'Cache.add should not fulfill until response ' +
23 'headers are received.');
24 });
25 });
26 }, 'Cache.add with successful request');
27
28 promise_test(function(t) {
29 return create_temporary_cache(t)
30 .then(function(cache) {
31 return assert_promise_rejects(
32 cache.add('this-does-not-exist-please-do-not-create-it'),
33 new TypeError(),
34 'Cache.add should throw TypeError if the resource does not exist.');
35 });
36 }, 'Cache.add with resource that results in a status of 404');
37
38 promise_test(function(t) {
39 return create_temporary_cache(t)
40 .then(function(cache) {
41 return assert_promise_rejects(
42 cache.addAll(),
43 new TypeError(),
44 'Cache.addAll should throw TypeError if there are no arguments');
45 });
46 }, 'Cache.addAll with no arguments');
47
48 promise_test(function(t) {
49 // Assumes the existence of simple.txt and blank.html in the same directory
50 // as this test script.
51 var urls = ['simple.txt', self.location.href, 'blank.html'];
52 return create_temporary_cache(t)
53 .then(function(cache) {
54 return cache.addAll(urls)
55 .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
56 });
57 }, 'Cache.addAll with ScalarValueString arguments');
58
59 promise_test(function(t) {
60 // Assumes the existence of simple.txt and blank.html in the same directory
61 // as this test script.
62 var urls = ['simple.txt', self.location.href, 'blank.html'];
63 var requests = urls.map(function(url) {
64 return new Request(url, {method: 'GET'});
65 });
66 return create_temporary_cache(t)
67 .then(function(cache) {
68 return cache.addAll(requests)
69 .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
70 });
71 }, 'Cache.addAll with Request arguments');
72
73 promise_test(function(t) {
74 // Assumes that simple.txt and blank.html are existing resources. The second
75 // 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.
76 var urls = ['simple.txt', 'this-resource-should-not-exist', 'blank.html'];
77
78 var cache;
79 return create_temporary_cache(t)
80 .then(function(created_cache) {
81 cache = created_cache;
82 return assert_promise_rejects(
83 cache.addAll(urls),
84 new TypeError(),
85 'Cache.addAll should throw TypeError if any of the requests fail.');
86 })
87 .then(function() {
88 return cache.keys();
89 })
90 .then(function(result) {
91 assert_equals(result.length, 0,
92 'Cache.addAll should not add any requests in a ' +
93 'failing batch.');
94 });
95 }, 'Cache.addAll with failing request');
96
97 // Helpers ---
98
99 // Asserts that |results| is an Array of Response objects that have URLs
100 // corresponding to |urls|. Used to verify the output of Cache.addAll.
101 function verify_response_array_against_urls(urls, results) {
102 assert_true(Array.isArray(result),
103 'Cache.addAll should resolve to an array.');
104 assert_equals(result.length, urls.length,
105 'Cache.addAll should resolve to an array whose ' +
106 'length is the same as the number of requests that ' +
107 'were added.');
108
109 var result_urls =
110 response.map(function(response) {
111 assert_true(response instanceof Response,
112 'Cache.addAll should resolve to an array of ' +
113 'Responses.');
114 return response.url;
115 });
116
117 var request_urls = urls.map(absolute_url);
118
119 assert_array_equals(
120 result_urls, request_urls,
121 'Cache.addAll should resolve to an array of Response objects ' +
122 'that correspond to the input requests.');
123 }
124
125 // Returns an absolute URL if the input URL is either already an absolute URL or
126 // is a single component that should be resolved relative to self.location.href.
127 function absolute_url(url) {
128 return new URL(url, self.location.href).href;
129 }
130
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698