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