Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / cachestorage / script-tests / cache-add.js
blob2448aaf6eb92654c233b51cc7165fa4c7d622b8c
1 if (self.importScripts) {
2 importScripts('/resources/testharness.js');
3 importScripts('/resources/testharness-helpers.js');
4 importScripts('../resources/test-helpers.js');
7 cache_test(function(cache) {
8 return assert_promise_rejects(
9 cache.add(),
10 new TypeError(),
11 'Cache.add should throw a TypeError when no arguments are given.');
12 }, 'Cache.add called with no arguments');
14 cache_test(function(cache) {
15 return cache.add('../resources/simple.txt')
16 .then(function(result) {
17 assert_equals(result, undefined,
18 'Cache.add should resolve with undefined on success.');
19 return cache.match('../resources/simple.txt');
21 .then(function(response) {
22 assert_class_string(response, 'Response',
23 'Cache.add should put a resource in the cache.');
24 return response.text();
26 .then(function(body) {
27 assert_equals(body, 'a simple text file\n',
28 'Cache.add should retrieve the correct body.');
29 });
30 }, 'Cache.add called with relative URL specified as a string');
32 cache_test(function(cache) {
33 return assert_promise_rejects(
34 cache.add('javascript://this-is-not-http-mmkay'),
35 new TypeError(),
36 'Cache.add should throw a TypeError for non-HTTP/HTTPS URLs.');
37 }, 'Cache.add called with non-HTTP/HTTPS URL');
39 cache_test(function(cache) {
40 var request = new Request('../resources/simple.txt');
41 return cache.add(request)
42 .then(function(result) {
43 assert_equals(result, undefined,
44 'Cache.add should resolve with undefined on success.');
45 });
46 }, 'Cache.add called with Request object');
48 cache_test(function(cache) {
49 var request = new Request('../resources/simple.txt',
50 {method: 'POST', body: 'This is a body.'});
51 return assert_promise_rejects(
52 cache.add(request),
53 new TypeError(),
54 'Cache.add should throw a TypeError for non-GET requests.');
55 }, 'Cache.add called with POST request');
57 cache_test(function(cache) {
58 var request = new Request('../resources/simple.txt');
59 return request.text()
60 .then(function() {
61 assert_false(request.bodyUsed);
63 .then(function() {
64 return cache.add(request);
65 });
66 }, 'Cache.add called with Request object with a used body');
68 cache_test(function(cache) {
69 return cache.add('this-does-not-exist-please-dont-create-it')
70 .then(function(result) {
71 assert_equals(result, undefined,
72 'Cache.add should resolve with undefined on success.');
73 });
74 }, 'Cache.add with request that results in a status of 404');
76 cache_test(function(cache) {
77 return cache.add('../resources/fetch-status.php?status=500')
78 .then(function(result) {
79 assert_equals(result, undefined,
80 'Cache.add should resolve with undefined on success.');
81 });
82 }, 'Cache.add with request that results in a status of 500');
84 cache_test(function(cache) {
85 return assert_promise_rejects(
86 cache.addAll(),
87 new TypeError(),
88 'Cache.addAll with no arguments should throw TypeError.');
89 }, 'Cache.addAll with no arguments');
91 cache_test(function(cache) {
92 // Assumes the existence of ../resources/simple.txt and ../resources/blank.html
93 var urls = ['../resources/simple.txt', undefined, '../resources/blank.html'];
94 return assert_promise_rejects(
95 cache.addAll(),
96 new TypeError(),
97 'Cache.addAll should throw TypeError for an undefined argument.');
98 }, 'Cache.addAll with a mix of valid and undefined arguments');
100 cache_test(function(cache) {
101 return cache.addAll([])
102 .then(function(result) {
103 assert_equals(result, undefined,
104 'Cache.addAll should resolve with undefined on ' +
105 'success.');
106 return cache.keys();
108 .then(function(result) {
109 assert_equals(result.length, 0,
110 'There should be no entry in the cache.');
112 }, 'Cache.addAll with an empty array');
114 cache_test(function(cache) {
115 // Assumes the existence of ../resources/simple.txt and
116 // ../resources/blank.html
117 var urls = ['../resources/simple.txt',
118 self.location.href,
119 '../resources/blank.html'];
120 return cache.addAll(urls)
121 .then(function(result) {
122 assert_equals(result, undefined,
123 'Cache.addAll should resolve with undefined on ' +
124 'success.');
125 return Promise.all(
126 urls.map(function(url) { return cache.match(url); }));
128 .then(function(responses) {
129 assert_class_string(
130 responses[0], 'Response',
131 'Cache.addAll should put a resource in the cache.');
132 assert_class_string(
133 responses[1], 'Response',
134 'Cache.addAll should put a resource in the cache.');
135 assert_class_string(
136 responses[2], 'Response',
137 'Cache.addAll should put a resource in the cache.');
138 return Promise.all(
139 responses.map(function(response) { return response.text(); }));
141 .then(function(bodies) {
142 assert_equals(
143 bodies[0], 'a simple text file\n',
144 'Cache.add should retrieve the correct body.');
145 assert_equals(
146 bodies[2], '<!DOCTYPE html>\n<title>Empty doc</title>\n',
147 'Cache.add should retrieve the correct body.');
149 }, 'Cache.addAll with string URL arguments');
151 cache_test(function(cache) {
152 // Assumes the existence of ../resources/simple.txt and
153 // ../resources/blank.html
154 var urls = ['../resources/simple.txt',
155 self.location.href,
156 '../resources/blank.html'];
157 var requests = urls.map(function(url) { return new Request(url); });
158 return cache.addAll(requests)
159 .then(function(result) {
160 assert_equals(result, undefined,
161 'Cache.addAll should resolve with undefined on ' +
162 'success.');
163 return Promise.all(
164 urls.map(function(url) { return cache.match(url); }));
166 .then(function(responses) {
167 assert_class_string(
168 responses[0], 'Response',
169 'Cache.addAll should put a resource in the cache.');
170 assert_class_string(
171 responses[1], 'Response',
172 'Cache.addAll should put a resource in the cache.');
173 assert_class_string(
174 responses[2], 'Response',
175 'Cache.addAll should put a resource in the cache.');
176 return Promise.all(
177 responses.map(function(response) { return response.text(); }));
179 .then(function(bodies) {
180 assert_equals(
181 bodies[0], 'a simple text file\n',
182 'Cache.add should retrieve the correct body.');
183 assert_equals(
184 bodies[2], '<!DOCTYPE html>\n<title>Empty doc</title>\n',
185 'Cache.add should retrieve the correct body.');
187 }, 'Cache.addAll with Request arguments');
189 cache_test(function(cache) {
190 // Assumes that ../resources/simple.txt and ../resources/blank.html exist.
191 // The second resource does not.
192 var urls = ['../resources/simple.txt',
193 'this-resource-should-not-exist',
194 '../resources/blank.html'];
195 var requests = urls.map(function(url) { return new Request(url); });
196 return cache.addAll(requests)
197 .then(function(result) {
198 assert_equals(result, undefined,
199 'Cache.addAll should resolve with undefined on ' +
200 'success.');
201 return Promise.all(
202 urls.map(function(url) { return cache.match(url); }));
204 .then(function(responses) {
205 assert_class_string(
206 responses[0], 'Response',
207 'Cache.addAll should put a resource in the cache.');
208 assert_class_string(
209 responses[1], 'Response',
210 'Cache.addAll should put a resource in the cache.');
211 assert_equals(
212 responses[1].status, 404,
213 'Cache.addAll should put a 404 resource in the cache.');
214 assert_class_string(
215 responses[2], 'Response',
216 'Cache.addAll should put a resource in the cache.');
217 return Promise.all(
218 responses.map(function(response) { return response.text(); }));
220 .then(function(bodies) {
221 assert_equals(
222 bodies[0], 'a simple text file\n',
223 'Cache.add should retrieve the correct body.');
224 assert_equals(
225 bodies[2], '<!DOCTYPE html>\n<title>Empty doc</title>\n',
226 'Cache.add should retrieve the correct body.');
228 }, 'Cache.addAll with a mix of succeeding and failing requests');
230 done();