@stanlemon/server 0.3.44 → 0.3.46
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stanlemon/server",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.46",
|
|
4
4
|
"description": "A basic express web server setup.",
|
|
5
5
|
"author": "Stan Lemon <stanlemon@users.noreply.github.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"@stanlemon/webdev": "*",
|
|
23
|
-
"body-parser": "^
|
|
23
|
+
"body-parser": "^2.2.0",
|
|
24
24
|
"compression": "^1.8.0",
|
|
25
25
|
"cookie-parser": "^1.4.7",
|
|
26
26
|
"dotenv": "16.4.7",
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import asyncJsonHandler from "./asyncJsonHandler";
|
|
2
|
+
|
|
3
|
+
describe("asyncHandler()", () => {
|
|
4
|
+
it("handles fn response", async () => {
|
|
5
|
+
const body = {
|
|
6
|
+
hello: "World",
|
|
7
|
+
};
|
|
8
|
+
const req = jest.fn();
|
|
9
|
+
const res = jest.fn();
|
|
10
|
+
res.status = jest.fn().mockReturnValue({ json: jest.fn() });
|
|
11
|
+
const next = jest.fn();
|
|
12
|
+
|
|
13
|
+
const controller = async (req, res, next) => {
|
|
14
|
+
return Promise.resolve(body);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
await asyncJsonHandler(controller)(req, res, next);
|
|
18
|
+
|
|
19
|
+
expect(res.status.mock.calls[0][0]).toBe(200);
|
|
20
|
+
expect(res.status().json.mock.calls[0][0]).toEqual(body);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("handles fn 400", async () => {
|
|
24
|
+
const req = jest.fn();
|
|
25
|
+
const res = jest.fn();
|
|
26
|
+
res.status = jest.fn().mockReturnValue({ json: jest.fn() });
|
|
27
|
+
const next = jest.fn();
|
|
28
|
+
|
|
29
|
+
const controller = async (req, res, next) => {
|
|
30
|
+
throw new Error("Bad Request");
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
await asyncJsonHandler(controller)(req, res, next);
|
|
34
|
+
|
|
35
|
+
expect(res.status.mock.calls[0][0]).toBe(400);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("handles fn 403", async () => {
|
|
39
|
+
const req = jest.fn();
|
|
40
|
+
const res = jest.fn();
|
|
41
|
+
res.status = jest.fn().mockReturnValue({ json: jest.fn() });
|
|
42
|
+
const next = jest.fn();
|
|
43
|
+
|
|
44
|
+
const controller = async (req, res, next) => {
|
|
45
|
+
throw new Error("Not Authorized");
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
await asyncJsonHandler(controller)(req, res, next);
|
|
49
|
+
|
|
50
|
+
expect(res.status.mock.calls[0][0]).toBe(403);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("handles fn 404", async () => {
|
|
54
|
+
const req = jest.fn();
|
|
55
|
+
const res = jest.fn();
|
|
56
|
+
res.status = jest.fn().mockReturnValue({ json: jest.fn() });
|
|
57
|
+
const next = jest.fn();
|
|
58
|
+
|
|
59
|
+
const controller = async (req, res, next) => {
|
|
60
|
+
throw new Error("Not Found");
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
await asyncJsonHandler(controller)(req, res, next);
|
|
64
|
+
|
|
65
|
+
expect(res.status.mock.calls[0][0]).toBe(404);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("handles fn 409", async () => {
|
|
69
|
+
const req = jest.fn();
|
|
70
|
+
const res = jest.fn();
|
|
71
|
+
res.status = jest.fn().mockReturnValue({ json: jest.fn() });
|
|
72
|
+
const next = jest.fn();
|
|
73
|
+
|
|
74
|
+
const controller = async (req, res, next) => {
|
|
75
|
+
throw new Error("Already Exists");
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
await asyncJsonHandler(controller)(req, res, next);
|
|
79
|
+
|
|
80
|
+
expect(res.status.mock.calls[0][0]).toBe(409);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("handles fn 500", async () => {
|
|
84
|
+
const req = jest.fn();
|
|
85
|
+
const res = jest.fn();
|
|
86
|
+
res.status = jest.fn().mockReturnValue({ json: jest.fn() });
|
|
87
|
+
const next = jest.fn();
|
|
88
|
+
|
|
89
|
+
const controller = async (req, res, next) => {
|
|
90
|
+
throw new Error("Who knows!");
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
await asyncJsonHandler(controller)(req, res, next);
|
|
94
|
+
|
|
95
|
+
expect(res.status.mock.calls[0][0]).toBe(500);
|
|
96
|
+
});
|
|
97
|
+
});
|
package/src/schemaHandler.js
CHANGED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import Joi from "joi";
|
|
2
|
+
import schemaHandler from "./schemaHandler";
|
|
3
|
+
|
|
4
|
+
describe("schemaHandler()", () => {
|
|
5
|
+
it("validates schema against invalid response", async () => {
|
|
6
|
+
const req = jest.fn();
|
|
7
|
+
req.body = {
|
|
8
|
+
foo: "bar",
|
|
9
|
+
email: "stan",
|
|
10
|
+
};
|
|
11
|
+
const res = jest.fn();
|
|
12
|
+
res.status = jest.fn().mockReturnValue({ json: jest.fn() });
|
|
13
|
+
const next = jest.fn();
|
|
14
|
+
|
|
15
|
+
const person = Joi.object({
|
|
16
|
+
fullName: Joi.string().required().label("Full Name").required(),
|
|
17
|
+
email: Joi.string().email().required().label("Email").required(),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const controller = jest.fn();
|
|
21
|
+
|
|
22
|
+
await schemaHandler(person, controller)(req, res, next);
|
|
23
|
+
|
|
24
|
+
expect(res.status.mock.calls[0][0]).toBe(400);
|
|
25
|
+
expect(res.status().json.mock.calls[0][0]).toEqual({
|
|
26
|
+
errors: {
|
|
27
|
+
fullName: '"Full Name" is required',
|
|
28
|
+
email: '"Email" must be a valid email address',
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
expect(controller).not.toHaveBeenCalled();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("validates schema against valid response", async () => {
|
|
35
|
+
const body = {
|
|
36
|
+
fullName: "Stan Lemon",
|
|
37
|
+
email: "stanlemon@users.noreply.github.com",
|
|
38
|
+
};
|
|
39
|
+
const req = jest.fn();
|
|
40
|
+
req.body = body;
|
|
41
|
+
const res = jest.fn();
|
|
42
|
+
res.status = jest.fn().mockReturnValue({ json: jest.fn() });
|
|
43
|
+
const next = jest.fn();
|
|
44
|
+
|
|
45
|
+
const person = Joi.object({
|
|
46
|
+
fullName: Joi.string().required().label("Full Name").required(),
|
|
47
|
+
email: Joi.string().email().required().label("Email").required(),
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const controller = async (req, res, next) => {
|
|
51
|
+
return Promise.resolve(body);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
await schemaHandler(person, controller)(req, res, next);
|
|
55
|
+
|
|
56
|
+
expect(res.status.mock.calls[0][0]).toBe(200);
|
|
57
|
+
expect(res.status().json.mock.calls[0][0]).toEqual(body);
|
|
58
|
+
});
|
|
59
|
+
});
|