Created
November 20, 2019 00:18
-
-
Save shrunyan/82d7e24fc4abae58da5bdd1e126efa2c to your computer and use it in GitHub Desktop.
Proof case for unexpected 404 responses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This test ensures there is no render gap between when a page is published and made available. | |
// Specifically we expect to get a 200 response (vs 404) immediately, within milliseconds, after a page is published. | |
// We will prove this by making a request to site-engine every X milliseconds and then triggering a publish | |
// At no time should we recieve a 404 response | |
const ZESTY_INSTANCE_DOMAIN = "http://zesty.pw/"; | |
const ZESTY_USER_EMAIL = ""; | |
const ZESTY_USER_PASSWORD = ""; | |
const ZESTY_INSTANCE_ZUID = "8-f48cf3a682-7fthvk"; | |
const ZESTY_MODEL_ZUID = "6-a1a600-k0b6f0"; | |
const ZESTY_ITEM_ZUID = "7-a1be38-1b42ht"; | |
const REQUEST_INTERVAL_MS = 100; | |
const request = require("request"); | |
const test = require("tape"); | |
const SDK = require("@zesty-io/sdk"); | |
test("No 404 responses on publish", async t => { | |
const auth = new SDK.Auth(); | |
const session = await auth.login(ZESTY_USER_EMAIL, ZESTY_USER_PASSWORD); | |
const sdk = new SDK(ZESTY_INSTANCE_ZUID, session.token); | |
// capture all request responses | |
let responses = []; | |
// starting making page requests | |
const intervalId = setInterval(() => { | |
request(ZESTY_INSTANCE_DOMAIN, (err, res, body) => { | |
if (err) { | |
t.fail(err); | |
} | |
console.log("request", res.statusCode, res.headers.age); | |
responses.push(res); | |
}); | |
}, REQUEST_INTERVAL_MS); | |
try { | |
// look up the item so we can get the latest version number | |
const item = await sdk.instance.getItem(ZESTY_MODEL_ZUID, ZESTY_ITEM_ZUID); | |
const title = `TEST ${Date.now()}`; | |
// save change to item | |
const saveRes = await sdk.instance.updateItem( | |
ZESTY_MODEL_ZUID, | |
ZESTY_ITEM_ZUID, | |
{ | |
data: { | |
title | |
}, | |
meta: { | |
masterZUID: ZESTY_ITEM_ZUID | |
}, | |
web: { | |
pathPart: "zesty_home" | |
} | |
} | |
); | |
if (saveRes.error) { | |
throw new Error(saveRes.error); | |
} | |
if (saveRes.statusCode !== 200) { | |
console.log(saveRes); | |
throw new Error("Unknown item state"); | |
} | |
// publish item with bumped version | |
const publishRes = await sdk.instance.publishItem( | |
ZESTY_MODEL_ZUID, | |
ZESTY_ITEM_ZUID, | |
item.data.meta.version + 1 | |
); | |
if (publishRes.error) { | |
throw new Error(publishRes.error); | |
} | |
if (publishRes.statusCode !== 200) { | |
console.log(publishRes); | |
throw new Error("Unknown item state"); | |
} | |
console.log("published", publishRes, title); | |
// Wait a second before we stop recording requests | |
setTimeout(() => { | |
clearInterval(intervalId); | |
// confirm no 404 responses were recorded. | |
const unexpectedResponses = responses.filter( | |
res => res.statusCode == 404 | |
); | |
if (unexpectedResponses.length) { | |
t.fail("Recieved 404 response"); | |
} else { | |
t.pass(); | |
} | |
t.end(); | |
}, 1000); | |
} catch (err) { | |
clearInterval(intervalId); | |
t.fail(err); | |
t.end(); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "404-response-test", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "npx tape index.js" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"@zesty-io/sdk": "0.0.9", | |
"dotenv": "^8.2.0", | |
"request": "^2.88.0", | |
"tape": "^4.11.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment