| OLD | NEW |
| 1 <script src="../../resources/testharness.js"></script> | 1 <script src="../resources/get-host-info.sub.js"></script> |
| 2 <script src="../../resources/get-host-info.js?pipe=sub"></script> | 2 <script src="test-helpers.sub.js?pipe=sub"></script> |
| 3 <script src="test-helpers.js"></script> | |
| 4 <script> | 3 <script> |
| 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) { |
| 8 port.postMessage({results: 'equals', got: a, expected: b}); |
| 9 } |
| 10 |
| 7 function get_boundary(headers) { | 11 function get_boundary(headers) { |
| 8 var reg = new RegExp('multipart\/form-data; boundary=(.*)'); | 12 var reg = new RegExp('multipart\/form-data; boundary=(.*)'); |
| 9 for (var i = 0; i < headers.length; ++i) { | 13 for (var i = 0; i < headers.length; ++i) { |
| 10 if (headers[i][0] != 'content-type') { | 14 if (headers[i][0] != 'content-type') { |
| 11 continue; | 15 continue; |
| 12 } | 16 } |
| 13 var regResult = reg.exec(headers[i][1]); | 17 var regResult = reg.exec(headers[i][1]); |
| 14 if (!regResult) { | 18 if (!regResult) { |
| 15 continue; | 19 continue; |
| 16 } | 20 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 }; | 52 }; |
| 49 xhr.responseType = 'text'; | 53 xhr.responseType = 'text'; |
| 50 if (with_credentials) { | 54 if (with_credentials) { |
| 51 xhr.withCredentials = true; | 55 xhr.withCredentials = true; |
| 52 } | 56 } |
| 53 xhr.open(method, url_base + '/dummy?test', true); | 57 xhr.open(method, url_base + '/dummy?test', true); |
| 54 xhr.send(data); | 58 xhr.send(data); |
| 55 }); | 59 }); |
| 56 } | 60 } |
| 57 | 61 |
| 58 function get_sorted_header_name_list(headers) { | |
| 59 const header_names = []; | |
| 60 for (const [name, value] of headers) { | |
| 61 header_names.push(name); | |
| 62 } | |
| 63 header_names.sort(); | |
| 64 return header_names; | |
| 65 } | |
| 66 | |
| 67 function get_header_test() { | |
| 68 return xhr_send(host_info['HTTP_ORIGIN'], 'GET', '', false) | |
| 69 .then(function(response) { | |
| 70 assert_array_equals( | |
| 71 get_sorted_header_name_list(response.headers), | |
| 72 ["accept", "user-agent"], | |
| 73 'event.request has the expected headers for same-origin GET.'); | |
| 74 }); | |
| 75 } | |
| 76 | |
| 77 // TODO(tyoshino): Fix the stack not to include the Origin header as specified | |
| 78 // in the spec. | |
| 79 function post_header_test() { | |
| 80 return xhr_send(host_info['HTTP_ORIGIN'], 'POST', '', false) | |
| 81 .then(function(response) { | |
| 82 assert_array_equals( | |
| 83 get_sorted_header_name_list(response.headers), | |
| 84 ["accept", "content-type", "origin", "user-agent"], | |
| 85 'event.request has the expected headers for same-origin POST.'); | |
| 86 }); | |
| 87 } | |
| 88 | |
| 89 function cross_origin_get_header_test() { | |
| 90 return xhr_send(host_info['HTTP_REMOTE_ORIGIN'], 'GET', '', false) | |
| 91 .then(function(response) { | |
| 92 assert_array_equals( | |
| 93 get_sorted_header_name_list(response.headers), | |
| 94 ["accept", "user-agent"], | |
| 95 'event.request has the expected headers for cross-origin GET.'); | |
| 96 }); | |
| 97 } | |
| 98 | |
| 99 // TODO(tyoshino): Fix the stack not to include the Origin header as specified | |
| 100 // in the spec. | |
| 101 function cross_origin_post_header_test() { | |
| 102 return xhr_send(host_info['HTTP_REMOTE_ORIGIN'], 'POST', '', false) | |
| 103 .then(function(response) { | |
| 104 assert_array_equals( | |
| 105 get_sorted_header_name_list(response.headers), | |
| 106 ["accept", "content-type", "origin", "user-agent"], | |
| 107 'event.request has the expected headers for cross-origin POST.'); | |
| 108 }); | |
| 109 } | |
| 110 | |
| 111 function string_test() { | 62 function string_test() { |
| 112 return xhr_send(host_info['HTTP_ORIGIN'], 'POST', 'test string', false) | 63 return xhr_send(host_info['HTTPS_ORIGIN'], 'POST', 'test string', false) |
| 113 .then(function(response) { | 64 .then(function(response) { |
| 114 assert_equals(response.method, 'POST'); | 65 assert_equals(response.method, 'POST'); |
| 115 assert_equals(response.body, 'test string'); | 66 assert_equals(response.body, 'test string'); |
| 116 }); | 67 }); |
| 117 } | 68 } |
| 118 | 69 |
| 119 function blob_test() { | 70 function blob_test() { |
| 120 return xhr_send(host_info['HTTP_ORIGIN'], 'POST', new Blob(['test blob']), | 71 return xhr_send(host_info['HTTPS_ORIGIN'], 'POST', new Blob(['test blob']), |
| 121 false) | 72 false) |
| 122 .then(function(response) { | 73 .then(function(response) { |
| 123 assert_equals(response.method, 'POST'); | 74 assert_equals(response.method, 'POST'); |
| 124 assert_equals(response.body, 'test blob'); | 75 assert_equals(response.body, 'test blob'); |
| 125 }); | 76 }); |
| 126 } | 77 } |
| 127 | 78 |
| 128 function custom_method_test() { | 79 function custom_method_test() { |
| 129 return xhr_send(host_info['HTTP_ORIGIN'], 'XXX', 'test string xxx', false) | 80 return xhr_send(host_info['HTTPS_ORIGIN'], 'XXX', 'test string xxx', false) |
| 130 .then(function(response) { | 81 .then(function(response) { |
| 131 assert_equals(response.method, 'XXX'); | 82 assert_equals(response.method, 'XXX'); |
| 132 assert_equals(response.body, 'test string xxx'); | 83 assert_equals(response.body, 'test string xxx'); |
| 133 }); | 84 }); |
| 134 } | 85 } |
| 135 | 86 |
| 136 function options_method_test() { | 87 function options_method_test() { |
| 137 return xhr_send(host_info['HTTP_ORIGIN'], 'OPTIONS', 'test string xxx', false) | 88 return xhr_send(host_info['HTTPS_ORIGIN'], 'OPTIONS', 'test string xxx', false
) |
| 138 .then(function(response) { | 89 .then(function(response) { |
| 139 assert_equals(response.method, 'OPTIONS'); | 90 assert_equals(response.method, 'OPTIONS'); |
| 140 assert_equals(response.body, 'test string xxx'); | 91 assert_equals(response.body, 'test string xxx'); |
| 141 }); | 92 }); |
| 142 } | 93 } |
| 143 | 94 |
| 144 function form_data_test() { | 95 function form_data_test() { |
| 145 return create_file_system_file('fsfile.txt', 'fs file content') | 96 var formData = new FormData(); |
| 146 .then(function(file_system_file) { | 97 formData.append('sample string', '1234567890'); |
| 147 var formData = new FormData(); | 98 formData.append('sample blob', new Blob(['blob content'])); |
| 148 formData.append('sample string', '1234567890'); | 99 formData.append('sample file', new File(['file content'], 'file.dat')); |
| 149 formData.append('sample blob', new Blob(['blob content'])); | 100 return xhr_send(host_info['HTTPS_ORIGIN'], 'POST', formData, false) |
| 150 formData.append('sample file', new File(['file content'], 'file.dat')); | |
| 151 formData.append('sample fs file', file_system_file); | |
| 152 return xhr_send(host_info['HTTP_ORIGIN'], 'POST', formData, false); | |
| 153 }) | |
| 154 .then(function(response) { | 101 .then(function(response) { |
| 155 assert_equals(response.method, 'POST'); | 102 assert_equals(response.method, 'POST'); |
| 156 var boundary = get_boundary(response.headers); | 103 var boundary = get_boundary(response.headers); |
| 157 var expected_body = | 104 var expected_body = |
| 158 '--' + boundary + '\r\n' + | 105 '--' + boundary + '\r\n' + |
| 159 'Content-Disposition: form-data; name="sample string"\r\n' + | 106 'Content-Disposition: form-data; name="sample string"\r\n' + |
| 160 '\r\n' + | 107 '\r\n' + |
| 161 '1234567890\r\n' + | 108 '1234567890\r\n' + |
| 162 '--' + boundary + '\r\n' + | 109 '--' + boundary + '\r\n' + |
| 163 'Content-Disposition: form-data; name="sample blob"; ' + | 110 'Content-Disposition: form-data; name="sample blob"; ' + |
| 164 'filename="blob"\r\n' + | 111 'filename="blob"\r\n' + |
| 165 'Content-Type: application/octet-stream\r\n' + | 112 'Content-Type: application/octet-stream\r\n' + |
| 166 '\r\n' + | 113 '\r\n' + |
| 167 'blob content\r\n' + | 114 'blob content\r\n' + |
| 168 '--' + boundary + '\r\n' + | 115 '--' + boundary + '\r\n' + |
| 169 'Content-Disposition: form-data; name="sample file"; ' + | 116 'Content-Disposition: form-data; name="sample file"; ' + |
| 170 'filename="file.dat"\r\n' + | 117 'filename="file.dat"\r\n' + |
| 171 'Content-Type: application/octet-stream\r\n' + | 118 'Content-Type: application/octet-stream\r\n' + |
| 172 '\r\n' + | 119 '\r\n' + |
| 173 'file content\r\n' + | 120 'file content\r\n' + |
| 174 '--' + boundary + '\r\n' + | |
| 175 'Content-Disposition: form-data; name="sample fs file"; ' + | |
| 176 'filename="fsfile.txt"\r\n' + | |
| 177 'Content-Type: text/plain\r\n' + | |
| 178 '\r\n' + | |
| 179 'fs file content\r\n' + | |
| 180 '--' + boundary + '--\r\n'; | 121 '--' + boundary + '--\r\n'; |
| 181 assert_equals(response.body, expected_body); | 122 assert_equals(response.body, expected_body); |
| 182 }); | 123 }); |
| 183 } | 124 } |
| 184 | 125 |
| 185 function mode_credentials_test() { | 126 function mode_credentials_test() { |
| 186 return xhr_send(host_info['HTTP_ORIGIN'], 'GET', '', false) | 127 return xhr_send(host_info['HTTPS_ORIGIN'], 'GET', '', false) |
| 187 .then(function(response){ | 128 .then(function(response){ |
| 188 assert_equals(response.mode, 'cors'); | 129 assert_equals(response.mode, 'cors'); |
| 189 assert_equals(response.credentials, 'include'); | 130 assert_equals(response.credentials, 'same-origin'); |
| 190 return xhr_send(host_info['HTTP_ORIGIN'], 'GET', '', true); | 131 return xhr_send(host_info['HTTPS_ORIGIN'], 'GET', '', true); |
| 191 }) | 132 }) |
| 192 .then(function(response){ | 133 .then(function(response){ |
| 193 assert_equals(response.mode, 'cors'); | 134 assert_equals(response.mode, 'cors'); |
| 194 assert_equals(response.credentials, 'include'); | 135 assert_equals(response.credentials, 'include'); |
| 195 return xhr_send(host_info['HTTP_REMOTE_ORIGIN'], 'GET', '', false); | 136 return xhr_send(host_info['HTTPS_REMOTE_ORIGIN'], 'GET', '', false); |
| 196 }) | 137 }) |
| 197 .then(function(response){ | 138 .then(function(response){ |
| 198 assert_equals(response.mode, 'cors'); | 139 assert_equals(response.mode, 'cors'); |
| 199 assert_equals(response.credentials, 'same-origin'); | 140 assert_equals(response.credentials, 'same-origin'); |
| 200 return xhr_send(host_info['HTTP_REMOTE_ORIGIN'], 'GET', '', true); | 141 return xhr_send(host_info['HTTPS_REMOTE_ORIGIN'], 'GET', '', true); |
| 201 }) | 142 }) |
| 202 .then(function(response){ | 143 .then(function(response){ |
| 203 assert_equals(response.mode, 'cors'); | 144 assert_equals(response.mode, 'cors'); |
| 204 assert_equals(response.credentials, 'include'); | 145 assert_equals(response.credentials, 'include'); |
| 205 }); | 146 }); |
| 206 } | 147 } |
| 207 | 148 |
| 208 function data_url_test() { | 149 function data_url_test() { |
| 209 return new Promise(function(resolve, reject) { | 150 return new Promise(function(resolve, reject) { |
| 210 var xhr = new XMLHttpRequest(); | 151 var xhr = new XMLHttpRequest(); |
| 211 xhr.onload = function() { | 152 xhr.onload = function() { |
| 212 resolve(xhr.response); | 153 resolve(xhr.response); |
| 213 }; | 154 }; |
| 214 xhr.onerror = function() { | 155 xhr.onerror = function() { |
| 215 reject('XHR should succeed.'); | 156 reject('XHR should succeed.'); |
| 216 }; | 157 }; |
| 217 xhr.responseType = 'text'; | 158 xhr.responseType = 'text'; |
| 218 xhr.open('GET', 'data:text/html,Foobar', true); | 159 xhr.open('GET', 'data:text/html,Foobar', true); |
| 219 xhr.send(); | 160 xhr.send(); |
| 220 }) | 161 }) |
| 221 .then(function(data) { | 162 .then(function(data) { |
| 222 assert_equals(data, 'Foobar'); | 163 assert_equals(data, 'Foobar'); |
| 223 }); | 164 }); |
| 224 } | 165 } |
| 225 | 166 |
| 226 window.addEventListener('message', function(evt) { | 167 window.addEventListener('message', function(evt) { |
| 227 var port = evt.ports[0]; | 168 port = evt.ports[0]; |
| 228 get_header_test() | 169 string_test() |
| 229 .then(post_header_test) | |
| 230 .then(cross_origin_get_header_test) | |
| 231 .then(cross_origin_post_header_test) | |
| 232 .then(string_test) | |
| 233 .then(blob_test) | 170 .then(blob_test) |
| 234 .then(custom_method_test) | 171 .then(custom_method_test) |
| 235 .then(options_method_test) | 172 .then(options_method_test) |
| 236 .then(form_data_test) | 173 .then(form_data_test) |
| 237 .then(mode_credentials_test) | 174 .then(mode_credentials_test) |
| 238 .then(data_url_test) | 175 .then(data_url_test) |
| 239 .then(function() { port.postMessage({results: 'finish'}); }) | 176 .then(function() { port.postMessage({results: 'finish'}); }) |
| 240 .catch(function(e) { port.postMessage({results: 'failure:' + e}); }); | 177 .catch(function(e) { port.postMessage({results: 'failure:' + e}); }); |
| 241 }); | 178 }); |
| 242 </script> | 179 </script> |
| OLD | NEW |