OLD | NEW |
1 importScripts('worker-testharness.js'); | 1 importScripts('worker-testharness.js'); |
2 importScripts('/resources/testharness-helpers.js'); | 2 importScripts('/resources/testharness-helpers.js'); |
3 | 3 |
4 var test_url = 'https://example.com/foo'; | 4 var test_url = 'https://example.com/foo'; |
5 | 5 var test_response_body = 'Hello world!'; |
6 // Construct a generic Request object. The URL is |test_url|. All other fields | |
7 // are defaults. | |
8 function new_test_request() { | |
9 return new Request(test_url); | |
10 } | |
11 | |
12 // Construct a generic Response object. The URL is empty. If specified |body| | |
13 // will be set as the response body string. | |
14 function new_test_response(body) { | |
15 body = body || 'Hello world!'; | |
16 return new Response(body, { | |
17 status: 200, | |
18 statusText: 'OK', | |
19 headers: [['Content-Type', 'text/plain']] | |
20 }); | |
21 } | |
22 | 6 |
23 cache_test(function(cache) { | 7 cache_test(function(cache) { |
24 var request = new_test_request(); | 8 var request = new Request(test_url); |
25 var response = new_test_response(); | 9 var response = new Response(test_response_body); |
26 return cache.put(request, response) | 10 return cache.put(request, response) |
27 .then(function(result) { | 11 .then(function(result) { |
28 assert_equals(result, undefined, | 12 assert_equals(result, undefined, |
29 'Cache.put should resolve with undefined on success.'); | 13 'Cache.put should resolve with undefined on success.'); |
30 }); | 14 }); |
31 }, 'Cache.put called with simple Request and Response'); | 15 }, 'Cache.put called with simple Request and Response'); |
32 | 16 |
33 cache_test(function(cache) { | 17 cache_test(function(cache) { |
34 var test_url = new URL('simple.txt', location.href).href; | 18 var test_url = new URL('simple.txt', location.href).href; |
35 var request = new Request(test_url); | 19 var request = new Request(test_url); |
(...skipping 16 matching lines...) Expand all Loading... |
52 assert_equals(body, 'a simple text file\n', | 36 assert_equals(body, 'a simple text file\n', |
53 'Cache.put should store response body.'); | 37 'Cache.put should store response body.'); |
54 }); | 38 }); |
55 }, 'Cache.put called with Request and Response from fetch()'); | 39 }, 'Cache.put called with Request and Response from fetch()'); |
56 | 40 |
57 cache_test(function(cache) { | 41 cache_test(function(cache) { |
58 var request = new Request(test_url, { | 42 var request = new Request(test_url, { |
59 method: 'GET', | 43 method: 'GET', |
60 body: 'Hello' | 44 body: 'Hello' |
61 }); | 45 }); |
62 var response = new_test_response(); | 46 var response = new Response(test_response_body); |
63 assert_false(request.bodyUsed, | 47 assert_false(request.bodyUsed, |
64 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' + | 48 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' + |
65 'Request.bodyUsed should be initially false.'); | 49 'Request.bodyUsed should be initially false.'); |
66 return cache.put(request, response) | 50 return cache.put(request, response) |
67 .then(function() { | 51 .then(function() { |
68 assert_false(request.bodyUsed, | 52 assert_false(request.bodyUsed, |
69 'Cache.put should not consume Request body.'); | 53 'Cache.put should not consume Request body.'); |
70 }) | 54 }) |
71 .then(function() { | 55 .then(function() { |
72 return cache.match(request); | 56 return cache.match(request); |
73 }) | 57 }) |
74 .then(function(result) { | 58 .then(function(result) { |
75 assert_object_equals(result, response, | 59 assert_object_equals(result, response, |
76 'Cache.put should store response body.'); | 60 'Cache.put should store response body.'); |
77 }); | 61 }); |
78 }, 'Cache.put with Request containing a body'); | 62 }, 'Cache.put with Request containing a body'); |
79 | 63 |
80 cache_test(function(cache) { | 64 cache_test(function(cache) { |
81 var request = new_test_request(); | 65 var request = new Request(test_url); |
82 var response = new_test_response(); | 66 var response = new Response(test_response_body); |
83 return cache.put(request, response) | 67 return cache.put(request, response) |
84 .then(function() { | 68 .then(function() { |
85 return cache.match(test_url); | 69 return cache.match(test_url); |
86 }) | 70 }) |
87 .then(function(result) { | 71 .then(function(result) { |
88 assert_object_equals(result, response, | 72 assert_object_equals(result, response, |
89 'Cache.put should update the cache with ' + | 73 'Cache.put should update the cache with ' + |
90 'new Request and Response.'); | 74 'new Request and Response.'); |
91 }); | 75 }); |
92 }, 'Cache.put with a Response containing an empty URL'); | 76 }, 'Cache.put with a Response containing an empty URL'); |
93 | 77 |
94 cache_test(function(cache) { | 78 cache_test(function(cache) { |
95 var request = new_test_request(); | 79 var request = new Request(test_url); |
96 var response = new Response('', { | 80 var response = new Response('', { |
97 status: 200, | 81 status: 200, |
98 headers: [['Content-Type', 'text/plain']] | 82 headers: [['Content-Type', 'text/plain']] |
99 }); | 83 }); |
100 return cache.put(request, response) | 84 return cache.put(request, response) |
101 .then(function() { | 85 .then(function() { |
102 return cache.match(test_url); | 86 return cache.match(test_url); |
103 }) | 87 }) |
104 .then(function(result) { | 88 .then(function(result) { |
105 assert_equals(result.status, 200, 'Cache.put should store status.'); | 89 assert_equals(result.status, 200, 'Cache.put should store status.'); |
106 assert_equals(result.headers.get('Content-Type'), 'text/plain', | 90 assert_equals(result.headers.get('Content-Type'), 'text/plain', |
107 'Cache.put should store headers.'); | 91 'Cache.put should store headers.'); |
108 return result.text(); | 92 return result.text(); |
109 }) | 93 }) |
110 .then(function(body) { | 94 .then(function(body) { |
111 assert_equals(body, '', | 95 assert_equals(body, '', |
112 'Cache.put should store response body.'); | 96 'Cache.put should store response body.'); |
113 }); | 97 }); |
114 }, 'Cache.put with an empty response body'); | 98 }, 'Cache.put with an empty response body'); |
115 | 99 |
116 cache_test(function(cache) { | 100 cache_test(function(cache) { |
117 var test_url = new URL('fetch-status.php?status=500', location.href).href; | 101 var test_url = new URL('fetch-status.php?status=500', location.href).href; |
118 var request = new Request(test_url); | 102 var request = new Request(test_url); |
119 var response; | 103 var response; |
120 return fetch(test_url) | 104 return fetch(test_url) |
121 .then(function(fetch_result) { | 105 .then(function(fetch_result) { |
| 106 assert_equals(fetch_result.status, 500, |
| 107 'Test framework error: The status code should be 500.'); |
122 response = fetch_result.clone(); | 108 response = fetch_result.clone(); |
123 return cache.put(request, fetch_result); | 109 return cache.put(request, fetch_result); |
124 }) | 110 }) |
125 .then(function() { | 111 .then(function() { |
126 return cache.match(test_url); | 112 return cache.match(test_url); |
127 }) | 113 }) |
128 .then(function(result) { | 114 .then(function(result) { |
129 assert_object_equals(result, response, | 115 assert_object_equals(result, response, |
130 'Cache.put should update the cache with ' + | 116 'Cache.put should update the cache with ' + |
131 'new request and response.'); | 117 'new request and response.'); |
132 return result.text(); | 118 return result.text(); |
133 }) | 119 }) |
134 .then(function(body) { | 120 .then(function(body) { |
135 assert_equals(body, '', | 121 assert_equals(body, '', |
136 'Cache.put should store response body.'); | 122 'Cache.put should store response body.'); |
137 }); | 123 }); |
138 }, 'Cache.put with HTTP 500 response'); | 124 }, 'Cache.put with HTTP 500 response'); |
139 | 125 |
140 cache_test(function(cache) { | 126 cache_test(function(cache) { |
141 var alternate_response = new_test_response('Lorem ipsum'); | 127 var alternate_response_body = 'New body'; |
142 return cache.put(new_test_request(), new_test_response()) | 128 var alternate_response = new Response(alternate_response_body, |
| 129 { statusText: 'New status' }); |
| 130 return cache.put(new Request(test_url), |
| 131 new Response('Old body', { statusText: 'Old status' })) |
143 .then(function() { | 132 .then(function() { |
144 return cache.put(new_test_request(), alternate_response); | 133 return cache.put(new Request(test_url), alternate_response); |
145 }) | 134 }) |
146 .then(function() { | 135 .then(function() { |
147 return cache.match(test_url); | 136 return cache.match(test_url); |
148 }) | 137 }) |
149 .then(function(result) { | 138 .then(function(result) { |
150 assert_object_equals(result, alternate_response, | 139 assert_object_equals(result, alternate_response, |
151 'Cache.put should replace existing ' + | 140 'Cache.put should replace existing ' + |
152 'response with new response.'); | 141 'response with new response.'); |
| 142 return result.text(); |
| 143 }) |
| 144 .then(function(body) { |
| 145 assert_equals(body, alternate_response_body, |
| 146 'Cache put should store new response body.'); |
153 }); | 147 }); |
154 }, 'Cache.put called twice with same Request and different Responses'); | 148 }, 'Cache.put called twice with matching Requests and different Responses'); |
| 149 |
| 150 cache_test(function(cache) { |
| 151 var first_url = test_url; |
| 152 var second_url = first_url + '#(O_o)'; |
| 153 var alternate_response_body = 'New body'; |
| 154 var alternate_response = new Response(alternate_response_body, |
| 155 { statusText: 'New status' }); |
| 156 return cache.put(new Request(first_url), |
| 157 new Response('Old body', { statusText: 'Old status' })) |
| 158 .then(function() { |
| 159 return cache.put(new Request(second_url), alternate_response); |
| 160 }) |
| 161 .then(function() { |
| 162 return cache.match(test_url); |
| 163 }) |
| 164 .then(function(result) { |
| 165 assert_object_equals(result, alternate_response, |
| 166 'Cache.put should replace existing ' + |
| 167 'response with new response.'); |
| 168 return result.text(); |
| 169 }) |
| 170 .then(function(body) { |
| 171 assert_equals(body, alternate_response_body, |
| 172 'Cache put should store new response body.'); |
| 173 }); |
| 174 }, 'Cache.put called twice with request URLs that differ only by a fragment'); |
| 175 |
| 176 cache_test(function(cache) { |
| 177 var entries = { |
| 178 dark: { |
| 179 url: 'http://darkhelmet:12345@example.com/spaceballs', |
| 180 body: 'Moranis' |
| 181 }, |
| 182 |
| 183 skroob: { |
| 184 url: 'http://skroob:12345@example.com/spaceballs', |
| 185 body: 'Brooks' |
| 186 }, |
| 187 |
| 188 control: { |
| 189 url: 'http://example.com/spaceballs', |
| 190 body: 'v(o.o)v' |
| 191 } |
| 192 }; |
| 193 |
| 194 return Promise.all(Object.keys(entries).map(function(key) { |
| 195 return cache.put(new Request(entries[key].url), |
| 196 new Response(entries[key].body)); |
| 197 })) |
| 198 .then(function() { |
| 199 return Promise.all(Object.keys(entries).map(function(key) { |
| 200 return cache.match(entries[key].url) |
| 201 .then(function(result) { |
| 202 return result.text(); |
| 203 }) |
| 204 .then(function(body) { |
| 205 assert_equals(body, entries[key].body, |
| 206 'Cache put should store response body.'); |
| 207 }); |
| 208 })); |
| 209 }); |
| 210 }, 'Cache.put with request URLs containing embedded credentials'); |
155 | 211 |
156 cache_test(function(cache) { | 212 cache_test(function(cache) { |
157 var url = 'http://example.com/foo'; | 213 var url = 'http://example.com/foo'; |
158 return cache.put(url, new_test_response('some body')) | 214 return cache.put(url, new Response('some body')) |
159 .then(function() { return cache.match(url); }) | 215 .then(function() { return cache.match(url); }) |
160 .then(function(response) { return response.text(); }) | 216 .then(function(response) { return response.text(); }) |
161 .then(function(body) { | 217 .then(function(body) { |
162 assert_equals(body, 'some body', | 218 assert_equals(body, 'some body', |
163 'Cache.put should accept a string as request.'); | 219 'Cache.put should accept a string as request.'); |
164 }); | 220 }); |
165 }, 'Cache.put with an string request'); | 221 }, 'Cache.put with a string request'); |
166 | 222 |
167 cache_test(function(cache) { | 223 cache_test(function(cache) { |
168 return assert_promise_rejects( | 224 return assert_promise_rejects( |
169 cache.put(new_test_request(), 'Hello world!'), | 225 cache.put(new Request(test_url), 'Hello world!'), |
170 new TypeError(), | 226 new TypeError(), |
171 'Cache.put should only accept a Response object as the response.'); | 227 'Cache.put should only accept a Response object as the response.'); |
172 }, 'Cache.put with an invalid response'); | 228 }, 'Cache.put with an invalid response'); |
173 | 229 |
174 cache_test(function(cache) { | 230 cache_test(function(cache) { |
175 return assert_promise_rejects( | 231 return assert_promise_rejects( |
176 cache.put(new Request('file:///etc/passwd'), new_test_response()), | 232 cache.put(new Request('file:///etc/passwd'), |
| 233 new Response(test_response_body)), |
177 new TypeError(), | 234 new TypeError(), |
178 'Cache.put should reject non-HTTP/HTTPS requests with a TypeError.'); | 235 'Cache.put should reject non-HTTP/HTTPS requests with a TypeError.'); |
179 }, 'Cache.put with a non-HTTP/HTTPS request'); | 236 }, 'Cache.put with a non-HTTP/HTTPS request'); |
180 | 237 |
181 cache_test(function(cache) { | 238 cache_test(function(cache) { |
182 var response = new_test_response(); | 239 var response = new Response(test_response_body); |
183 return cache.put(new Request('relative-url'), response) | 240 return cache.put(new Request('relative-url'), response) |
184 .then(function() { | 241 .then(function() { |
185 return cache.match(new URL('relative-url', location.href).href); | 242 return cache.match(new URL('relative-url', location.href).href); |
186 }) | 243 }) |
187 .then(function(result) { | 244 .then(function(result) { |
188 assert_object_equals(result, response, | 245 assert_object_equals(result, response, |
189 'Cache.put should accept a relative URL ' + | 246 'Cache.put should accept a relative URL ' + |
190 'as the request.'); | 247 'as the request.'); |
191 }); | 248 }); |
192 }, 'Cache.put with a relative URL'); | 249 }, 'Cache.put with a relative URL'); |
193 | 250 |
194 cache_test(function(cache) { | 251 cache_test(function(cache) { |
195 var request = new Request('http://example.com/foo', {method: 'HEAD'}); | 252 var request = new Request('http://example.com/foo', { method: 'HEAD' }); |
196 return assert_promise_rejects( | 253 return assert_promise_rejects( |
197 cache.put(request, new_test_response()), | 254 cache.put(request, new Response(test_response_body)), |
198 new TypeError(), | 255 new TypeError(), |
199 'Cache.put should throw a TypeError for non-GET requests.'); | 256 'Cache.put should throw a TypeError for non-GET requests.'); |
200 }, 'Cache.put with a non-GET request'); | 257 }, 'Cache.put with a non-GET request'); |
201 | 258 |
202 cache_test(function(cache) { | 259 cache_test(function(cache) { |
203 return assert_promise_rejects( | 260 return assert_promise_rejects( |
204 cache.put(new_test_request(), null), | 261 cache.put(new Request(test_url), null), |
205 new TypeError(), | 262 new TypeError(), |
206 'Cache.put should throw a TypeError for an empty response.'); | 263 'Cache.put should throw a TypeError for an empty response.'); |
207 }, 'Cache.put with an empty response'); | 264 }, 'Cache.put with an empty response'); |
OLD | NEW |