diff --git a/server/src/server/router/api/v1/router.test.ts b/server/src/server/router/api/v1/router.test.ts index 0feaac268..c5c79b9dc 100644 --- a/server/src/server/router/api/v1/router.test.ts +++ b/server/src/server/router/api/v1/router.test.ts @@ -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); diff --git a/server/src/server/router/api/v1/router.ts b/server/src/server/router/api/v1/router.ts index f453733dd..2f669d542 100644 --- a/server/src/server/router/api/v1/router.ts +++ b/server/src/server/router/api/v1/router.ts @@ -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; diff --git a/server/src/server/router/api/v1/status/router.test.ts b/server/src/server/router/api/v1/status/router.test.ts new file mode 100644 index 000000000..538e2dcf9 --- /dev/null +++ b/server/src/server/router/api/v1/status/router.test.ts @@ -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); diff --git a/server/src/server/router/api/v1/status/router.ts b/server/src/server/router/api/v1/status/router.ts new file mode 100644 index 000000000..252519fec --- /dev/null +++ b/server/src/server/router/api/v1/status/router.ts @@ -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;