Skip to content

Commit 09c8bb4

Browse files
authored
change response to OK for health check (#46)
1 parent e3526f6 commit 09c8bb4

4 files changed

Lines changed: 40 additions & 5 deletions

File tree

backend/src/httpHandler.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
handleGetImageRequest,
99
handleUploadImageRequest,
1010
handleGetOwnDocumentsRequest,
11+
handleHealthRequest,
1112
} from "./httpHandler";
1213
import { Document } from "../generated/prisma/client";
1314
import { downloadEncryptedImage } from "./utils/uploaderDownloader";
@@ -44,6 +45,15 @@ vi.mock("formidable", () => ({
4445
},
4546
}));
4647

48+
describe("handleHealthRequest", () => {
49+
it("responds with OK to health check", () => {
50+
const response = mock<ServerResponse<IncomingMessage>>();
51+
handleHealthRequest(response);
52+
const result = response.end.mock.calls[0][0] as string;
53+
expect(result).toEqual("OK");
54+
});
55+
});
56+
4757
describe("handleCreateDocumentRequest", () => {
4858
it("creates a document without owner", async () => {
4959
const doc = buildFullDocument({ ownerExternalId: null });

backend/src/httpHandler.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ import { deleteImageFromBucket } from "./utils/s3";
1818

1919
const DEFAULT_MAX_IMAGE_SIZE_BYTES = 1 * 1024 * 1024; // 1 MB default
2020

21+
export const handleHealthRequest = (
22+
response: ServerResponse<IncomingMessage>,
23+
): void => {
24+
response.writeHead(200, { "Content-Type": "text/plain" });
25+
response.end("OK");
26+
};
27+
2128
export const handleCreateDocumentRequest = async (
2229
response: ServerResponse<IncomingMessage>,
2330
prisma: PrismaClient,
@@ -107,12 +114,11 @@ export const handleUploadImageRequest = async (
107114
}
108115
}
109116
} catch (error) {
110-
if (
111-
error instanceof Error &&
112-
error.message.includes("maxTotalFileSize")
113-
) {
117+
if (error instanceof Error && error.message.includes("maxTotalFileSize")) {
114118
response.writeHead(413, { "Content-Type": "text/json" });
115-
response.end(JSON.stringify({ error: "File size exceeds maximum allowed size" }));
119+
response.end(
120+
JSON.stringify({ error: "File size exceeds maximum allowed size" }),
121+
);
116122
return;
117123
}
118124
console.error(error);

backend/src/httpRouter.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,22 @@ import {
99
handleGetImageRequest,
1010
handleUploadImageRequest,
1111
handleGetOwnDocumentsRequest,
12+
handleHealthRequest,
1213
} from "./httpHandler";
1314

1415
vi.mock("./httpHandler");
1516

1617
describe("httpRouter", () => {
18+
it("responds with OK to health check", async () => {
19+
const payload = mock<onRequestPayload>();
20+
payload.request.method = "GET";
21+
payload.request.url = "/";
22+
payload.request.headers = {};
23+
24+
await expect(httpRouter(payload, null)).rejects.toThrow();
25+
expect(handleHealthRequest).toHaveBeenCalled();
26+
});
27+
1728
it("posts documents", async () => {
1829
const payload = mock<onRequestPayload>();
1930
payload.request.method = "POST";

backend/src/httpRouter.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
handleGetImageRequest,
66
handleUploadImageRequest,
77
handleGetOwnDocumentsRequest,
8+
handleHealthRequest,
89
} from "./httpHandler";
910
import { PrismaClient } from "../generated/prisma/client";
1011
import { onRequestPayload } from "@hocuspocus/server";
@@ -25,6 +26,13 @@ const httpRouter = async (data: onRequestPayload, prisma: PrismaClient) => {
2526
const modificationSecret = request.headers.authorization;
2627
const personId = extractPersonIdFromCookies(request.headers.cookie);
2728

29+
// health check response
30+
if (method === "GET" && !resource) {
31+
handleHealthRequest(response);
32+
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
33+
return Promise.reject();
34+
}
35+
2836
if (method === "POST" && resource === "documents" && !subResource) {
2937
await handleCreateDocumentRequest(response, prisma, personId);
3038
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors

0 commit comments

Comments
 (0)