OLD | NEW |
---|---|
(Empty) | |
1 importScripts('worker-test-harness.js'); | |
2 // | |
3 // Variations: | |
4 // - ignoreSearch : Ignores search parameter. | |
5 // - prefixMatch : only matches a prefix of the URL. | |
6 // - ignoreVary : ignores a 'Vary' header if there is one. | |
7 var entries = { | |
8 a: { | |
9 request: new Request('http://example.com/a'), | |
10 response: new Response('') | |
11 }, | |
12 | |
13 b: { | |
14 request: new Request('http://example.com/b'), | |
15 response: new Response('') | |
16 }, | |
17 | |
18 a_with_query: { | |
19 request: new Request('http://example.com/a?q=r'), | |
20 response: new Response('') | |
21 }, | |
22 | |
23 A: { | |
24 request: new Request('http://example.com/A'), | |
25 response: new Response('') | |
26 }, | |
27 | |
28 a_https: { | |
29 request: new Request('https://example.com/a'), | |
30 response: new Response('') | |
31 }, | |
32 | |
33 a_org: { | |
34 request: new Request('http://example.org/a'), | |
35 response: new Response('') | |
36 }, | |
37 | |
38 cat: { | |
39 request: new Request('http://example.com/cat'), | |
40 response: new Response('') | |
41 }, | |
42 | |
43 cat_with_fragment: { | |
44 request: new Request('http://example.com/cat#mouse'), | |
45 response: new Response('') | |
46 }, | |
47 | |
48 cat_in_the_hat: { | |
49 request: new Request('http://example.com/cat/in/the/hat'), | |
50 response: new Response('') | |
51 }, | |
52 | |
53 c_is_for_cookie: { | |
54 request: new Request('http://example.com/c', | |
55 { headers: { 'Cookies': 'is-for-cookie' } }), | |
jsbell
2014/08/15 20:49:34
That's good enough for me!
| |
56 response: new Response('', | |
57 { headers: { 'Vary': 'Cookies' } }) | |
jsbell
2014/08/15 20:49:34
But actually.. per style guide, no space after { o
| |
58 }, | |
59 | |
60 c_is_for_cake: { | |
61 request: new Request('http://example.com/c', | |
62 { headers: { 'Cookies': 'is-for-cake' } }), | |
63 response: new Response('', | |
64 { headers: { 'Vary': 'Cookies' } }) | |
65 }, | |
66 | |
67 c_x_key_is_1: { | |
68 request: new Request('http://example.com/c', | |
69 { headers: { 'Cookies': 'x', 'X-Key': '1'} }), | |
70 response: new Response('', | |
71 { headers: { 'Vary': '*' } }) | |
72 }, | |
73 | |
74 c_y_key_is_1: { | |
75 request: new Request('http://example.com/c', | |
76 { headers: { 'Cookies': 'y', 'X-Key': '1' } }), | |
77 response: new Response('', | |
78 { headers: { 'Vary': 'Cookies,X-Key'} }) | |
79 }, | |
80 | |
81 c_y_key_is_2: { | |
82 request: new Request('http://example.com/c', | |
83 { headers: { 'Cookies': 'y', 'X-Key': '2' } }), | |
84 response: new Response('', | |
85 { headers: { 'Vary': 'Cookies,X-Key'} }) | |
86 } | |
87 }; | |
88 | |
89 promise_test(function(t) { | |
90 var cache = new Cache(); | |
jsbell
2014/08/15 20:49:33
Just noticing that the constructor isn't actually
asanka
2014/08/20 03:11:58
I replaced the Cache() constructor with a helper t
jsbell
2014/08/20 18:18:08
Great idea!
| |
91 | |
92 put_all_requests_in_cache(cache) | |
93 | |
94 .then(function() { | |
95 return cache.matchAll(entries.a.request.url); | |
96 }) | |
97 .then(t.step_func(function(result) { | |
jsbell
2014/08/15 20:49:33
t.step_func shouldn't be necessary in a promise_te
asanka
2014/08/20 03:11:58
Removed.
| |
98 assert_array_equals(result, [entries.a.response], | |
99 'Cache.matchAll should match by URL.'); | |
100 })) | |
101 | |
102 .then(function() { | |
jsbell
2014/08/15 20:49:33
Since these cases are independent, can we split th
asanka
2014/08/20 03:11:58
Done.
| |
103 return cache.matchAll(entries.a.request); | |
104 }) | |
105 .then(t.step_func(function(result) { | |
106 assert_array_equals(result, [entries.a.response], | |
107 'Cache.matchAll should match by Request.'); | |
108 })) | |
109 | |
110 .then(function() { | |
111 return cache.matchAll(new Request(entries.a.request.url)); | |
112 }) | |
113 .then(t.step_func(function(result) { | |
114 assert_array_equals(result, [entries.a.response], | |
115 'Cache.matchAll should match by Request.'); | |
116 })) | |
117 | |
118 .then(function() { | |
119 return cache.match(entries.a.request); | |
120 }) | |
121 .then(t.step_func(function(result) { | |
122 assert_equals(result, entries.a.response, | |
123 'Cache.match should match by Request.'); | |
124 })) | |
125 | |
126 .then(function() { | |
127 return cache.matchAll(entries.a.request, { ignoreSearch: true }); | |
128 }) | |
129 .then(t.step_func(function(result) { | |
130 assert_array_equivalent( | |
131 result, [entries.a.response, entries.a_with_query.response], | |
132 'Cache.matchAll with ignoreSearch should ignore the search ' + | |
133 'parameters of cached request.'); | |
134 })) | |
135 | |
136 .then(function() { | |
137 return cache.matchAll(entries.a_with_query.request, | |
138 { ignoreSearch: true }); | |
139 }) | |
140 .then(t.step_func(function(result) { | |
141 assert_array_equivalent( | |
142 result, [entries.a.response, entries.a_with_query.response], | |
143 'Cache.matchAll with ignoreSearch should ignore the search ' + | |
144 'parameters of request.'); | |
145 })) | |
146 | |
147 .then(function() { | |
148 return cache.matchAll(entries.cat.request); | |
149 }) | |
150 .then(t.step_func(function(result) { | |
151 assert_array_equivalent( | |
152 result, [entries.cat.response, entries.cat_with_fragment.response], | |
153 'Cache.matchAll should ignore URL hash.'); | |
154 })) | |
155 | |
156 .then(function() { | |
157 return cache.matchAll('http'); | |
158 }) | |
159 .then(t.step_func(function(result) { | |
160 assert_array_equivalent( | |
161 result, [], | |
162 'Cache.matchAll should treat query as a URL and not just a string fr agment.'); | |
jsbell
2014/08/15 20:49:34
nit: line length (80 cols)
asanka
2014/08/20 03:11:58
Done.
| |
163 })) | |
164 | |
165 .then(function() { | |
166 return cache.matchAll('http://example.com/cat', { prefixMatch: true }) ; | |
jsbell
2014/08/15 20:49:34
nit: line length (80 cols)
asanka
2014/08/20 03:11:58
Done.
| |
167 }) | |
168 .then(t.step_func(function(result) { | |
169 assert_array_equivalent( | |
170 result, | |
171 [ entries.cat.response, | |
jsbell
2014/08/15 20:49:33
nit: break after [ and before ] for multi-line ini
asanka
2014/08/20 03:11:58
Done.
| |
172 entries.cat_with_fragment.response, | |
173 entries.cat_in_the_hat.response ], | |
174 'Cache.matchAll should honor prefixMatch.'); | |
175 })) | |
176 | |
177 .then(function() { | |
178 return cache.matchAll('http://example.com/cat/', { prefixMatch: true } ); | |
jsbell
2014/08/15 20:49:34
nit: line length (80 cols)
asanka
2014/08/20 03:11:58
Done.
| |
179 }) | |
180 .then(t.step_func(function(result) { | |
181 assert_array_equivalent( | |
182 result, [ entries.cat_in_the_hat.response ], | |
183 'Cache.matchAll should honor prefixMatch.'); | |
184 })); | |
185 | |
186 }, 'Cache.match'); | |
187 | |
188 // Helpers --- | |
189 | |
190 function put_all_requests_in_cache(cache) { | |
jsbell
2014/08/15 20:49:34
Could be written as:
return Promise.all(entries.m
asanka
2014/08/20 03:11:58
Ah. Thanks! Changed.
| |
191 var promise_array = []; | |
192 for (v in entries) { | |
193 promise_array.push(cache.put(entries[v].request, | |
194 entries[v].response)); | |
195 } | |
196 return Promise.all(promise_array); | |
197 } | |
198 | |
199 function assert_array_equivalent(actual, expected, description) { | |
jsbell
2014/08/15 20:49:34
For consistency with assert_array_equals(), rename
jsbell
2014/08/15 20:49:34
Can you add a comment that this is comparing the c
asanka
2014/08/20 03:11:58
Added a comment.
asanka
2014/08/20 03:11:58
Hmm. Were you going to suggest a different name?
jsbell
2014/08/20 18:18:08
I must have been misreading it. Name is fine, sorr
| |
200 assert_true(Array.isArray(actual), description); | |
201 assert_equals(actual.length, expected.length, description); | |
202 for (var index = 0; index < expected.length; ++index) { | |
203 assert_greater_than_equal(actual.indexOf(expected[index]), 0, description); | |
jsbell
2014/08/15 20:49:34
This could use assert_in_array()
asanka
2014/08/20 03:11:58
Done.
| |
204 } | |
205 } | |
OLD | NEW |