Skip to content

Instantly share code, notes, and snippets.

@kevee
Last active June 3, 2020 14:53
Show Gist options
  • Save kevee/bb5c0023cc64b604e46e016e984258f0 to your computer and use it in GitHub Desktop.
Save kevee/bb5c0023cc64b604e46e016e984258f0 to your computer and use it in GitHub Desktop.
A test interface to ensure CORS works with COVID Tracking Project APIs
<!DOCTYPE html>
<html>
<head>
<title>COVID Tracking Project API tester</title>
<link
rel="stylesheet"
href="https://code.jquery.com/qunit/qunit-2.10.0.css"
/>
</head>
<body>
<form id="host-form" style="margin: 30px 0;">
<label for="host">Enter host</label>
<input type="text" id="host" value="https://covidtracking.com" />
<button type="submit">Go</button>
</form>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="https://code.jquery.com/qunit/qunit-2.10.0.js"></script>
<script>
document
.getElementById("host-form")
.addEventListener("submit", (event) => {
event.preventDefault();
const host = document.getElementById("host").value;
QUnit.test("Status API", (assert) => {
return new Promise((resolve, reject) => {
fetch(`${host}/api/v1/status.json`)
.then((response) => response.json())
.then((status) => {
assert.ok(
typeof status.runNumber !== "undefined",
"Has run number"
);
assert.ok(
typeof status.buildTime !== "undefined",
"Has build time"
);
resolve();
})
.catch(() => {
assert.ok(false, "Error fetching data");
reject();
});
});
});
QUnit.test("States API", (assert) => {
return new Promise((resolve, reject) => {
fetch(`${host}/api/v1/states/daily.json`)
.then((response) => response.json())
.then((states) => {
assert.ok(states.length > 500, "Has records");
resolve();
})
.catch(() => {
assert.ok(false, "Error fetching data");
reject();
});
});
});
QUnit.test("US daily API", (assert) => {
return new Promise((resolve, reject) => {
fetch(`${host}/api/v1/us/daily.json`)
.then((response) => response.json())
.then((us) => {
assert.ok(us.length > 30, "Has records");
resolve();
})
.catch(() => {
assert.ok(false, "Error fetching data");
reject();
});
});
});
QUnit.test("States info with old url", (assert) => {
return new Promise((resolve, reject) => {
fetch(`${host}/api/states.json`)
.then((response) => response.json())
.then((data) => {
assert.ok(data.length > 30, "Has records");
resolve();
})
.catch(() => {
assert.ok(false, "Error fetching data");
reject();
});
});
});
QUnit.test("US info with old url and query parameter", (assert) => {
return new Promise((resolve, reject) => {
fetch(`${host}/api/us?date=20200501`)
.then((response) => response.json())
.then((data) => {
assert.ok(data.date === 20200501, "Has single record");
resolve();
})
.catch(() => {
assert.ok(false, "Error fetching data");
reject();
});
});
});
QUnit.test(
"State daily with old url and query parameter",
(assert) => {
return new Promise((resolve, reject) => {
fetch(`${host}/api/states/daily?state=ca&date=20200501`)
.then((response) => response.json())
.then((data) => {
assert.ok(data.date === 20200501, "Has single record");
resolve();
})
.catch(() => {
assert.ok(false, "Error fetching data");
reject();
});
});
}
);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment