Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/lib/api/framework.test.ts
5609 views
1
/** @jest-environment node */
2
3
/*
4
This file gets unit tested both in prod and dev modes. This is important to
5
ensure that in production the input validation is skipped (for now!).
6
*/
7
8
import handler from "../../pages/api/v2/guesslang";
9
import { createMocks } from "./test-framework";
10
11
describe("test that /api/v2/guesslang works in either dev or production mode", () => {
12
test("error if code param not given in dev mode; no error in production mode", async () => {
13
const { req, res } = createMocks({
14
method: "POST",
15
url: "/api/v2/guesslang",
16
body: {},
17
});
18
19
await handler(req, res);
20
if (process.env.NODE_ENV == "production") {
21
expect(res.statusCode).toBe(200);
22
} else {
23
expect(res.statusCode).toBe(400);
24
}
25
});
26
27
test("error if code is not a string in dev mode; no error in production mode", async () => {
28
const { req, res } = createMocks({
29
method: "POST",
30
url: "/api/v2/guesslang",
31
body: { code: 10 },
32
});
33
34
await handler(req, res);
35
if (process.env.NODE_ENV == "production") {
36
expect(res.statusCode).toBe(200);
37
} else {
38
expect(res.statusCode).toBe(400);
39
}
40
});
41
});
42
43