Scratch space for learning atproto app development
1import express from "express";
2import { StatusCodes } from "http-status-codes";
3import request from "supertest";
4
5import errorHandler from "#/common/middleware/errorHandler";
6import requestLogger from "#/common/middleware/requestLogger";
7
8describe("Request Logger Middleware", () => {
9 const app = express();
10
11 beforeAll(() => {
12 app.use(requestLogger);
13 app.get("/success", (req, res) => res.status(StatusCodes.OK).send("Success"));
14 app.get("/redirect", (req, res) => res.redirect("/success"));
15 app.get("/error", () => {
16 throw new Error("Test error");
17 });
18 app.use(errorHandler());
19 });
20
21 describe("Successful requests", () => {
22 it("logs successful requests", async () => {
23 const response = await request(app).get("/success");
24 expect(response.status).toBe(StatusCodes.OK);
25 });
26
27 it("checks existing request id", async () => {
28 const requestId = "test-request-id";
29 const response = await request(app).get("/success").set("X-Request-Id", requestId);
30 expect(response.status).toBe(StatusCodes.OK);
31 });
32 });
33
34 describe("Re-directions", () => {
35 it("logs re-directions correctly", async () => {
36 const response = await request(app).get("/redirect");
37 expect(response.status).toBe(StatusCodes.MOVED_TEMPORARILY);
38 });
39 });
40
41 describe("Error handling", () => {
42 it("logs thrown errors with a 500 status code", async () => {
43 const response = await request(app).get("/error");
44 expect(response.status).toBe(StatusCodes.INTERNAL_SERVER_ERROR);
45 });
46
47 it("logs 404 for unknown routes", async () => {
48 const response = await request(app).get("/unknown");
49 expect(response.status).toBe(StatusCodes.NOT_FOUND);
50 });
51 });
52});