Skip to content

Instantly share code, notes, and snippets.

@alexanderbazo
Created July 9, 2021 07:14
Show Gist options
  • Save alexanderbazo/79c9e2e5cbffced50e2d09732001be8f to your computer and use it in GitHub Desktop.
Save alexanderbazo/79c9e2e5cbffced50e2d09732001be8f to your computer and use it in GitHub Desktop.
HTTP-Request for testing ExperimentManager via fetch
<html>
<body>
<h1>Hello World</h1>
</body>
<script>
console.log("Testing Client");
/*
//Use to close open experiments when tests failed
cancelExperiment({
id: 1234567894
});
*/
// Get random experiment and cancel it
getExperiment().then(function(experiment) {
console.log("[/api/experiments/random] Received experiment");
console.log(experiment);
console.log("[/api/experiment/ID/cancel] Trying to cancel experiment");
cancelExperiment(experiment).then(function(result) {
console.log("[/api/experiment/ID/cancel] Message from server");
console.log(result);
});
});
// Get random experiment, update and close it
getExperiment().then(function(experiment) {
console.log("[/api/experiments/random] Received experiment");
console.log(experiment);
console.log("[/api/experiment/ID] Trying to update experiment");
experiment.results.data = {
log: ["ONE", "TWO"],
}
updateExperiment(experiment).then(function(result) {
console.log("[/api/experiment/ID] Message from server");
console.log(result);
console.log("[/api/experiment/ID/close] Trying to close experiment");
closeExperiment(experiment).then(function(result) {
console.log("[/api/experiment/ID/close] Message from server");
console.log(result);
});
});
});
async function getExperiment() {
let response = await fetch("/api/experiments/random"),
result = await response.json();
return result;
}
async function cancelExperiment(experiment) {
let response = await fetch(`/api/experiment/${experiment.id}/cancel`, {
method: "POST"
}),
result = await response.json();
return result;
}
async function closeExperiment(experiment) {
let response = await fetch(`/api/experiment/${experiment.id}/close`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(experiment),
}),
result = await response.json();
return result;
}
async function updateExperiment(experiment) {
let response = await fetch(`/api/experiment/${experiment.id}`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(experiment),
}),
result = await response.json();
return result;
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment