OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <script src="../resources/testharness.js"></script> | 2 <title>Service Worker: Foreign Fetch CORS functionality</title> |
3 <script src="../resources/testharnessreport.js"></script> | 3 <script src="/resources/testharness.js"></script> |
4 <script src="../resources/get-host-info.js"></script> | 4 <script src="/resources/testharnessreport.js"></script> |
5 <script src="resources/test-helpers.js"></script> | 5 <script src="resources/get-host-info.sub.js"></script> |
| 6 <script src="resources/test-helpers.sub.js"></script> |
6 <script src="resources/foreign-fetch-helpers.js"></script> | 7 <script src="resources/foreign-fetch-helpers.js"></script> |
7 <body> | 8 <body> |
8 <script> | 9 <script> |
9 var host_info = get_host_info(); | 10 var host_info = get_host_info(); |
10 var origin = new URL(self.location).origin; | 11 var origin = new URL(self.location).origin; |
11 var wrong_origin = 'https://example.com/'; | 12 var wrong_origin = 'https://example.com/'; |
12 var test_header = 'X-ServiceWorker-ServerHeader'; | 13 var test_header = 'X-ServiceWorker-ServerHeader'; |
13 | 14 |
14 function url_to_fetch(scope) { | 15 function url_to_fetch(scope) { |
15 return host_info.HTTPS_REMOTE_ORIGIN + '/serviceworker/resources/' + scope; | 16 return host_info.HTTPS_REMOTE_ORIGIN + new URL('resources/', location).pathnam
e + scope; |
16 } | 17 } |
17 | 18 |
18 function worker_for_response(response) { | 19 function worker_for_response(response) { |
19 return 'foreign-fetch-cors-worker.js?' + | 20 return 'foreign-fetch-cors-worker.js?' + |
20 encodeURIComponent(JSON.stringify(response)); | 21 encodeURIComponent(JSON.stringify(response)); |
21 } | 22 } |
22 | 23 |
23 function scope_for_params(params) { | 24 function scope_for_params(params) { |
24 return 'simple.txt?' + encodeURIComponent(JSON.stringify(params)); | 25 return 'simple.txt?' + encodeURIComponent(JSON.stringify(params)); |
25 } | 26 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 var response; | 70 var response; |
70 var cache; | 71 var cache; |
71 return fetch(url) | 72 return fetch(url) |
72 .then(r => { | 73 .then(r => { |
73 response = r.clone(); | 74 response = r.clone(); |
74 assert_equals(r.type, 'cors'); | 75 assert_equals(r.type, 'cors'); |
75 assert_equals(r.headers.get(test_header), header_value, 'From fetch'); | 76 assert_equals(r.headers.get(test_header), header_value, 'From fetch'); |
76 return r.text(); | 77 return r.text(); |
77 }) | 78 }) |
78 .then(response_text => { | 79 .then(response_text => { |
79 assert_true(response_text.startsWith('report(')); | 80 assert_true(response_text.startsWith('report('), |
| 81 'Correct content via fetch'); |
80 return self.caches.open(url); | 82 return self.caches.open(url); |
81 }) | 83 }) |
82 .then(c => { | 84 .then(c => { |
83 cache = c; | 85 cache = c; |
84 return cache.put(url, response); | 86 return cache.put(url, response); |
85 }) | 87 }) |
86 .then(() => cache.match(url)) | 88 .then(() => cache.match(url)) |
87 .then(r => { | 89 .then(r => { |
88 assert_equals(r.type, 'cors'); | 90 assert_equals(r.type, 'cors'); |
89 assert_equals(r.headers.get(test_header), header_value, 'From cache'); | 91 assert_equals(r.headers.get(test_header), header_value, 'From cache'); |
90 return r.text(); | 92 return r.text(); |
91 }) | 93 }) |
92 .then(response_text => { | 94 .then(response_text => { |
93 assert_true(response_text.startsWith('report(')); | 95 assert_true(response_text.startsWith('report('), |
| 96 'Correct content via cache'); |
94 return self.caches.delete(url); | 97 return self.caches.delete(url); |
95 }) | 98 }) |
96 .then(() => new Promise(resolve => { | 99 .then(() => new Promise(resolve => { |
97 var request = new XMLHttpRequest(); | 100 var request = new XMLHttpRequest(); |
98 request.open('GET', url); | 101 request.open('GET', url); |
99 request.onreadystatechange = () => { | 102 request.onreadystatechange = () => { |
100 if (request.readyState == 4) resolve(request); | 103 if (request.readyState == 4) resolve(request); |
101 }; | 104 }; |
102 request.send(); | 105 request.send(); |
103 })) | 106 })) |
104 .then(xhr => { | 107 .then(xhr => { |
105 assert_true(xhr.responseText.startsWith('report(')); | 108 assert_true(xhr.responseText.startsWith('report('), |
| 109 'Correct content via xhr'); |
106 assert_equals(xhr.getResponseHeader(test_header), header_value); | 110 assert_equals(xhr.getResponseHeader(test_header), header_value); |
107 var headers = xhr.getAllResponseHeaders().toLowerCase(); | 111 var headers = xhr.getAllResponseHeaders().toLowerCase(); |
108 if (header_value) { | 112 if (header_value) { |
109 assert_true(headers.includes(test_header.toLowerCase() + ': ' + | 113 assert_true(headers.includes(test_header.toLowerCase() + ': ' + |
110 header_value.toLowerCase())); | 114 header_value.toLowerCase())); |
111 } else { | 115 } else { |
112 assert_false(headers.includes(test_header.toLowerCase())); | 116 assert_false(headers.includes(test_header.toLowerCase())); |
113 } | 117 } |
114 }); | 118 }); |
115 } | 119 } |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 for (var i = 0; i < tests.length; ++i) (data => { | 297 for (var i = 0; i < tests.length; ++i) (data => { |
294 promise_test(t => { | 298 promise_test(t => { |
295 var scope = scope_for_params(data.params); | 299 var scope = scope_for_params(data.params); |
296 var worker = worker_for_response(data.response); | 300 var worker = worker_for_response(data.response); |
297 return install_cross_origin_worker(t, worker, scope) | 301 return install_cross_origin_worker(t, worker, scope) |
298 .then(() => data.expectation(url_to_fetch(scope), t)); | 302 .then(() => data.expectation(url_to_fetch(scope), t)); |
299 }, data.description); | 303 }, data.description); |
300 })(tests[i]); | 304 })(tests[i]); |
301 </script> | 305 </script> |
302 </body> | 306 </body> |
OLD | NEW |