Add Status endpoints

This commit is contained in:
zkldi
2021-06-10 15:15:15 +01:00
parent 260d918873
commit ed468f1c3d
4 changed files with 134 additions and 28 deletions
+8 -17
View File
@@ -1,7 +1,5 @@
import t from "tap";
import { FormatVersion } from "../../../../lib/constants/version";
import { CloseAllConnections } from "../../../../test-utils/close-connections";
import { CreateFakeAuthCookie } from "../../../../test-utils/fake-session";
import mockApi from "../../../../test-utils/mock-api";
import { ClearTestingRateLimitCache } from "../../../middleware/rate-limiter";
@@ -14,7 +12,7 @@ t.test("Rate Limiting Test", async (t) => {
const promises = [];
for (let i = 0; i < 150; i++) {
promises.push(mockApi.get("/api/v1"));
promises.push(mockApi.get("/api/v1/status"));
}
const res = await Promise.all(promises);
@@ -26,23 +24,16 @@ t.test("Rate Limiting Test", async (t) => {
t.end();
});
t.test("GET /api/v1", async (t) => {
const cookie = await CreateFakeAuthCookie(mockApi);
t.test("404 Handler", async (t) => {
const res = await mockApi.get("/api/v1/invalid_route_that_will_never_exist");
t.test("Should return the current time and the server version.", async (t) => {
const res = await mockApi.get("/api/v1").set("Cookie", cookie);
t.equal(res.statusCode, 200);
t.equal(res.body.success, true);
t.ok(
Math.abs(Date.now() - res.body.body.serverTime) < 5_000,
"Should be roughly the current time (5 seconds lenience)"
);
t.equal(res.body.body.version, FormatVersion());
t.end();
t.equal(res.statusCode, 404);
t.strictSame(res.body, {
success: false,
description: "Endpoint Not Found.",
});
t.end();
});
t.teardown(CloseAllConnections);
+7 -11
View File
@@ -1,23 +1,19 @@
import { Router } from "express";
import authRouter from "./auth/router";
import importRouter from "./import/router";
import irRouter from "../../ir/router";
import { FormatVersion } from "../../../../lib/constants/version";
import statusRouter from "./status/router";
const router: Router = Router({ mergeParams: true });
router.use("/auth", authRouter);
router.use("/status", statusRouter);
router.use("/import", importRouter);
router.use("/ir", irRouter);
router.use("/", (req, res) =>
res.status(200).json({
success: true,
description: "Request recieved successfully.",
body: {
serverTime: Date.now(),
version: FormatVersion(),
},
router.all("*", (req, res) =>
res.status(404).json({
success: false,
description: "Endpoint Not Found.",
})
);
export default router;
@@ -0,0 +1,79 @@
import { FormatVersion } from "../../../../../lib/constants/version";
import { CreateFakeAuthCookie } from "../../../../../test-utils/fake-session";
import mockApi from "../../../../../test-utils/mock-api";
import t from "tap";
import { CloseAllConnections } from "../../../../../test-utils/close-connections";
t.test("GET /api/v1/status", async (t) => {
const cookie = await CreateFakeAuthCookie(mockApi);
t.test("Should return the current time and the server version.", async (t) => {
const res = await mockApi.get("/api/v1/status").set("Cookie", cookie);
t.equal(res.statusCode, 200);
t.equal(res.body.success, true);
t.ok(
Math.abs(Date.now() - res.body.body.serverTime) < 5_000,
"Should be roughly the current time (5 seconds lenience)"
);
t.equal(res.body.body.version, FormatVersion());
t.end();
});
t.test("Should echo the provided echo param.", async (t) => {
const res = await mockApi.get("/api/v1/status?echo=foobar").set("Cookie", cookie);
t.equal(res.statusCode, 200);
t.equal(res.body.success, true);
t.equal(res.body.body.echo, "foobar");
t.ok(
Math.abs(Date.now() - res.body.body.serverTime) < 5_000,
"Should be roughly the current time (5 seconds lenience)"
);
t.equal(res.body.body.version, FormatVersion());
t.end();
});
t.end();
});
t.test("POST /api/v1/status", async (t) => {
const cookie = await CreateFakeAuthCookie(mockApi);
t.test("Should return the current time and the server version.", async (t) => {
const res = await mockApi.post("/api/v1/status").set("Cookie", cookie);
t.equal(res.statusCode, 200);
t.equal(res.body.success, true);
t.ok(
Math.abs(Date.now() - res.body.body.serverTime) < 5_000,
"Should be roughly the current time (5 seconds lenience)"
);
t.equal(res.body.body.version, FormatVersion());
t.end();
});
t.test("Should echo the provided echo param.", async (t) => {
const res = await mockApi.post("/api/v1/status").set("Cookie", cookie).send({
echo: "foobar",
});
t.equal(res.statusCode, 200);
t.equal(res.body.success, true);
t.equal(res.body.body.echo, "foobar");
t.ok(
Math.abs(Date.now() - res.body.body.serverTime) < 5_000,
"Should be roughly the current time (5 seconds lenience)"
);
t.equal(res.body.body.version, FormatVersion());
t.end();
});
t.end();
});
t.teardown(CloseAllConnections);
@@ -0,0 +1,40 @@
import { Router } from "express";
import { FormatVersion } from "../../../../../lib/constants/version";
const router: Router = Router({ mergeParams: true });
router.get("/", (req, res) => {
let echo;
if (req.query.echo && typeof req.query.echo === "string") {
echo = req.query.echo;
}
res.status(200).json({
success: true,
description: "Status check successful.",
body: {
serverTime: Date.now(),
version: FormatVersion(),
echo,
},
});
});
router.post("/", (req, res) => {
let echo;
if (req.body.echo && typeof req.body.echo === "string") {
echo = req.body.echo;
}
res.status(200).json({
success: true,
description: "Status check successful.",
body: {
serverTime: Date.now(),
version: FormatVersion(),
echo,
},
});
});
export default router;