* @param {string} url * @return {!Promise<!Document>}
(url)
| 371 | * @return {!Promise<!Document>} |
| 372 | */ |
| 373 | function fetchDocument(url) { |
| 374 | return new Promise((resolve, reject) => { |
| 375 | const xhr = new XMLHttpRequest(); |
| 376 | xhr.open('GET', url, true); |
| 377 | xhr.responseType = 'document'; |
| 378 | xhr.setRequestHeader('Accept', 'text/html'); |
| 379 | xhr.onreadystatechange = () => { |
| 380 | if (xhr.readyState < /* STATUS_RECEIVED */ 2) { |
| 381 | return; |
| 382 | } |
| 383 | if (xhr.status < 100 || xhr.status > 599) { |
| 384 | xhr.onreadystatechange = null; |
| 385 | reject(new Error(`Unknown HTTP status ${xhr.status}`)); |
| 386 | return; |
| 387 | } |
| 388 | if (xhr.readyState == /* COMPLETE */ 4) { |
| 389 | if (xhr.responseXML) { |
| 390 | resolve(xhr.responseXML); |
| 391 | } else { |
| 392 | reject(new Error(`No xhr.responseXML`)); |
| 393 | } |
| 394 | } |
| 395 | }; |
| 396 | xhr.onerror = () => { |
| 397 | reject(new Error('Network failure')); |
| 398 | }; |
| 399 | xhr.onabort = () => { |
| 400 | reject(new Error('Request aborted')); |
| 401 | }; |
| 402 | xhr.send(); |
| 403 | }); |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * @param {string} url |
no test coverage detected