From e08e3f4d37dcb959a24ce8acb0063021c89da049 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 10 Mar 2022 12:55:08 +0100 Subject: [PATCH] Replace XMLHttpRequest usage with the Fetch API in `send` (in `test/unit/testreporter.js`) Besides converting the `send` function to use the Fetch API, this patch also changes the method to return a `Promise` to get rid of the callback function. (Although, currently there's no call-site passing in a callback function.) --- test/unit/testreporter.js | 42 ++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/test/unit/testreporter.js b/test/unit/testreporter.js index cd6930fd0c3c6..b87f4b45013d7 100644 --- a/test/unit/testreporter.js +++ b/test/unit/testreporter.js @@ -1,23 +1,29 @@ const TestReporter = function (browser) { - function send(action, json, cb) { - const r = new XMLHttpRequest(); - // (The POST URI is ignored atm.) - r.open("POST", action, true); - r.setRequestHeader("Content-Type", "application/json"); - r.onreadystatechange = function sendTaskResultOnreadystatechange(e) { - if (r.readyState === 4) { - // Retry until successful - if (r.status !== 200) { - send(action, json, cb); - } else { - if (cb) { - cb(); + function send(action, json) { + return new Promise(resolve => { + json.browser = browser; + + fetch(action, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(json), + }) + .then(response => { + // Retry until successful. + if (!response.ok || response.status !== 200) { + throw new Error(response.statusText); } - } - } - }; - json.browser = browser; - r.send(JSON.stringify(json)); + resolve(); + }) + .catch(reason => { + console.warn(`TestReporter - send failed (${action}): ${reason}`); + resolve(); + + send(action, json); + }); + }); } function sendInfo(message) {