OLD | NEW |
1 <script src="/common/get-host-info.sub.js"></script> | 1 <script src="/common/get-host-info.sub.js"></script> |
2 <script src="test-helpers.sub.js?pipe=sub"></script> | 2 <script src="test-helpers.sub.js?pipe=sub"></script> |
3 <script> | 3 <script> |
4 var port; | 4 var port; |
5 var host_info = get_host_info(); | 5 var host_info = get_host_info(); |
6 | 6 |
7 function assert_equals(a, b) { | 7 function assert_equals(a, b) { |
8 port.postMessage({results: 'equals', got: a, expected: b}); | 8 port.postMessage({results: 'equals', got: a, expected: b}); |
9 } | 9 } |
10 | 10 |
| 11 function assert_array_equals(a, b, msg) { |
| 12 port.postMessage({results: 'array_equals', got: a, expected: b, msg: msg}); |
| 13 } |
| 14 |
11 function get_boundary(headers) { | 15 function get_boundary(headers) { |
12 var reg = new RegExp('multipart\/form-data; boundary=(.*)'); | 16 var reg = new RegExp('multipart\/form-data; boundary=(.*)'); |
13 for (var i = 0; i < headers.length; ++i) { | 17 for (var i = 0; i < headers.length; ++i) { |
14 if (headers[i][0] != 'content-type') { | 18 if (headers[i][0] != 'content-type') { |
15 continue; | 19 continue; |
16 } | 20 } |
17 var regResult = reg.exec(headers[i][1]); | 21 var regResult = reg.exec(headers[i][1]); |
18 if (!regResult) { | 22 if (!regResult) { |
19 continue; | 23 continue; |
20 } | 24 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 }; | 56 }; |
53 xhr.responseType = 'text'; | 57 xhr.responseType = 'text'; |
54 if (with_credentials) { | 58 if (with_credentials) { |
55 xhr.withCredentials = true; | 59 xhr.withCredentials = true; |
56 } | 60 } |
57 xhr.open(method, url_base + '/dummy?test', true); | 61 xhr.open(method, url_base + '/dummy?test', true); |
58 xhr.send(data); | 62 xhr.send(data); |
59 }); | 63 }); |
60 } | 64 } |
61 | 65 |
| 66 function get_sorted_header_name_list(headers) { |
| 67 var header_names = []; |
| 68 var idx, name; |
| 69 |
| 70 for (idx = 0; idx < headers.length; ++idx) { |
| 71 name = headers[idx][0]; |
| 72 // The `Accept-Language` header is optional; its presence should not |
| 73 // influence test results. |
| 74 // |
| 75 // > 4. If request’s header list does not contain `Accept-Language`, user |
| 76 // > agents should append `Accept-Language`/an appropriate value to |
| 77 // > request's header list. |
| 78 // |
| 79 // https://fetch.spec.whatwg.org/#fetching |
| 80 if (name === 'accept-language') { |
| 81 continue; |
| 82 } |
| 83 |
| 84 header_names.push(name); |
| 85 } |
| 86 header_names.sort(); |
| 87 return header_names; |
| 88 } |
| 89 |
| 90 function get_header_test() { |
| 91 return xhr_send(host_info['HTTPS_ORIGIN'], 'GET', '', false) |
| 92 .then(function(response) { |
| 93 assert_array_equals( |
| 94 get_sorted_header_name_list(response.headers), |
| 95 ["accept"], |
| 96 'event.request has the expected headers for same-origin GET.'); |
| 97 }); |
| 98 } |
| 99 |
| 100 function post_header_test() { |
| 101 return xhr_send(host_info['HTTPS_ORIGIN'], 'POST', '', false) |
| 102 .then(function(response) { |
| 103 assert_array_equals( |
| 104 get_sorted_header_name_list(response.headers), |
| 105 ["accept", "content-type"], |
| 106 'event.request has the expected headers for same-origin POST.'); |
| 107 }); |
| 108 } |
| 109 |
| 110 function cross_origin_get_header_test() { |
| 111 return xhr_send(host_info['HTTPS_REMOTE_ORIGIN'], 'GET', '', false) |
| 112 .then(function(response) { |
| 113 assert_array_equals( |
| 114 get_sorted_header_name_list(response.headers), |
| 115 ["accept"], |
| 116 'event.request has the expected headers for cross-origin GET.'); |
| 117 }); |
| 118 } |
| 119 |
| 120 function cross_origin_post_header_test() { |
| 121 return xhr_send(host_info['HTTPS_REMOTE_ORIGIN'], 'POST', '', false) |
| 122 .then(function(response) { |
| 123 assert_array_equals( |
| 124 get_sorted_header_name_list(response.headers), |
| 125 ["accept", "content-type"], |
| 126 'event.request has the expected headers for cross-origin POST.'); |
| 127 }); |
| 128 } |
| 129 |
62 function string_test() { | 130 function string_test() { |
63 return xhr_send(host_info['HTTPS_ORIGIN'], 'POST', 'test string', false) | 131 return xhr_send(host_info['HTTPS_ORIGIN'], 'POST', 'test string', false) |
64 .then(function(response) { | 132 .then(function(response) { |
65 assert_equals(response.method, 'POST'); | 133 assert_equals(response.method, 'POST'); |
66 assert_equals(response.body, 'test string'); | 134 assert_equals(response.body, 'test string'); |
67 }); | 135 }); |
68 } | 136 } |
69 | 137 |
70 function blob_test() { | 138 function blob_test() { |
71 return xhr_send(host_info['HTTPS_ORIGIN'], 'POST', new Blob(['test blob']), | 139 return xhr_send(host_info['HTTPS_ORIGIN'], 'POST', new Blob(['test blob']), |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 xhr.open('GET', 'data:text/html,Foobar', true); | 227 xhr.open('GET', 'data:text/html,Foobar', true); |
160 xhr.send(); | 228 xhr.send(); |
161 }) | 229 }) |
162 .then(function(data) { | 230 .then(function(data) { |
163 assert_equals(data, 'Foobar'); | 231 assert_equals(data, 'Foobar'); |
164 }); | 232 }); |
165 } | 233 } |
166 | 234 |
167 window.addEventListener('message', function(evt) { | 235 window.addEventListener('message', function(evt) { |
168 port = evt.ports[0]; | 236 port = evt.ports[0]; |
169 string_test() | 237 get_header_test() |
| 238 .then(post_header_test) |
| 239 .then(cross_origin_get_header_test) |
| 240 .then(cross_origin_post_header_test) |
| 241 .then(string_test) |
170 .then(blob_test) | 242 .then(blob_test) |
171 .then(custom_method_test) | 243 .then(custom_method_test) |
172 .then(options_method_test) | 244 .then(options_method_test) |
173 .then(form_data_test) | 245 .then(form_data_test) |
174 .then(mode_credentials_test) | 246 .then(mode_credentials_test) |
175 .then(data_url_test) | 247 .then(data_url_test) |
176 .then(function() { port.postMessage({results: 'finish'}); }) | 248 .then(function() { port.postMessage({results: 'finish'}); }) |
177 .catch(function(e) { port.postMessage({results: 'failure:' + e}); }); | 249 .catch(function(reason) { |
| 250 var error = String(reason.message || reason); |
| 251 port.postMessage({results: 'failure', error: error}); |
| 252 }); |
178 }); | 253 }); |
179 </script> | 254 </script> |
OLD | NEW |