OLD | NEW |
---|---|
(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_throws( | |
7 cache.add(''), | |
8 '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 assert_promise_throws( | |
17 cache.add('http://example.com/cow-goes-moo', | |
18 'http://example.com/another-cow-goes-moo'), | |
19 'TypeError', | |
20 '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.
| |
21 'is given.'); | |
22 }); | |
23 }, 'Cache.add with more than one argument'); | |
24 | |
25 promise_test(function(t) { | |
26 return create_temporary_cache(t) | |
27 .then(function(cache) { | |
28 return cache.add('simple.txt') | |
29 .then(function(result) { | |
30 assert_true(result instanceof Response, | |
31 'Cache.add should return a Response on success.'); | |
32 assert_equals(result.status, 200, | |
33 'Cache.add should not fulfill until response ' + | |
34 'headers are received.'); | |
35 }); | |
36 }); | |
37 }, 'Cache.add with successful request'); | |
38 | |
39 promise_test(function(t) { | |
40 return create_temporary_cache(t) | |
41 .then(function(cache) { | |
42 return assert_promise_throws( | |
43 cache.add('this-does-not-exist-please-do-not-create-it'), | |
44 'TypeError', | |
45 'Cache.add should throw TypeError if the resource does not exist.'); | |
46 }); | |
47 }, 'Cache.add with resource that results in a status of 404'); | |
48 | |
49 promise_test(function(t) { | |
50 return create_temporary_cache(t) | |
51 .then(function(cache) { | |
52 return assert_promise_throws( | |
53 cache.addAll(), | |
54 'TypeError', | |
55 'Cache.addAll should throw TypeError if there are no arguments'); | |
56 }); | |
57 }, 'Cache.addAll with no 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 return create_temporary_cache(t) | |
64 .then(function(cache) { | |
65 return cache.addAll(urls) | |
66 .then(verify_response_array_against_urls.bind(undefined, urls)); | |
67 }); | |
68 }, 'Cache.addAll with ScalarValueString arguments'); | |
69 | |
70 promise_test(function(t) { | |
71 // Assumes the existence of simple.txt and blank.html in the same directory | |
72 // as this test script. | |
73 var urls = ['simple.txt', self.location.href, 'blank.html']; | |
74 var requests = urls.map(function(url) { | |
75 return new Request(url, {method: 'GET'}); | |
76 }); | |
77 return create_temporary_cache(t) | |
78 .then(function(cache) { | |
79 return cache.addAll(requests) | |
80 .then(verify_response_array_against_urls.bind(undefined, urls)); | |
81 }); | |
82 }, 'Cache.addAll with Request arguments'); | |
83 | |
84 promise_test(function(t) { | |
85 // Assumes that simple.txt and blank.html are existing resources. The second | |
86 // resource does not. | |
87 var urls = ['simple.txt', 'this-resource-should-not-exist', 'blank.html']; | |
88 var created_cache; | |
89 | |
90 return create_temporary_cache(t) | |
91 .then(function(cache) { | |
92 created_cache = cache; | |
93 return assert_promise_throws( | |
94 cache.addAll(urls), | |
95 'TypeError', | |
96 'Cache.addAll should throw TypeError if any of the requests fail.'); | |
97 }) | |
98 | |
jsbell
2014/08/20 18:18:08
nit: remove blank line
asanka
2014/08/20 21:24:58
Done.
| |
99 .then(function() { | |
100 return cache.keys(); | |
101 }) | |
102 .then(function(result) { | |
103 assert_equals(result.length, 0, | |
104 'Cache.addAll should not add any requests in a ' + | |
105 'failing batch.'); | |
106 }); | |
107 }, 'Cache.addAll with failing request'); | |
108 | |
109 // Helpers --- | |
110 | |
111 // Asserts that |results| is an Array of Response objects that have URLs | |
112 // corresponding to |urls|. Used to verify the output of Cache.addAll. | |
113 function verify_response_array_against_urls(urls, results) { | |
114 assert_true(Array.isArray(result), | |
115 'Cache.addAll should resolve to an array.'); | |
116 assert_equals(result.length, urls.length, | |
117 'Cache.addAll should resolve to an array whose ' + | |
118 'length is the same as the number of requests that ' + | |
119 'were added.'); | |
120 | |
121 var result_urls = | |
122 response.map(function(response) { | |
123 assert_true(response instanceof Response, | |
124 'Cache.addAll should resolve to an array of ' + | |
125 'Responses.'); | |
126 return response.url; | |
127 }); | |
128 | |
129 var request_urls = urls.map(absolute_url); | |
130 | |
131 assert_array_equals( | |
132 result_urls, request_urls, | |
133 'Cache.addAll should resolve to an array of Response objects ' + | |
134 'that correspond to the input requests.'); | |
135 } | |
136 | |
137 // Returns an absolute URL if the input URL is either already an absolute URL or | |
138 // is a single component that should be resolved relative to self.location.href. | |
139 function absolute_url(url) { | |
140 if (url.match(/:\/\//)) { | |
141 return url; | |
142 } else { | |
143 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
| |
144 } | |
145 } | |
146 | |
OLD | NEW |