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

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

Powered by Google App Engine
This is Rietveld 408576698