-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
273 lines (249 loc) · 7.52 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
const stream = require("stream");
const chai = require("chai");
const request = require("supertest");
const Microbe = require(".");
const expect = chai.expect;
const pkg = require("./package.json");
const error = require("@utilitywarehouse/uw-lib-error.js");
const logger = require("@utilitywarehouse/uw-lib-logger.js");
const fs = require('fs');
class LoggerStream extends stream.Writable {
constructor(data) {
super({ objectMode: true });
this.data = data;
}
_write(chunk, encoding, next) {
this.data.push(chunk);
next();
}
}
describe("Microbe", function () {
beforeEach(function () {
this.system = new Microbe();
this.system.build();
this.system.container.get("logger").level = 'silent';
});
describe("exposes operational endpoints at", function () {
it("/__/about", function (done) {
request(this.system.server)
.get("/__/about")
.expect(200)
.expect(function (res) {
expect(res.body).to.have.property("name");
expect(res.body).to.have.property("description");
})
.end(done);
});
it("/__/ready", function (done) {
request(this.system.server).get("/__/ready").expect(200, "ready\n", done);
});
it("/__/health", function (done) {
this.system.health("check", (r) => r.healthy("ok"));
request(this.system.server)
.get("/__/health")
.expect(200)
.expect(function (res) {
expect(res.body).to.have.property("checks");
expect(res.body).to.have.property("health");
})
.end(done);
});
it("/__/metrics", function (done) {
request(this.system.server).get("/__/metrics").expect(200, done);
});
});
it("sends security headers", function (done) {
request(this.system.server)
.get("/__/about")
.expect(200)
.expect(function (res) {
const headers = res.headers;
expect(headers["x-dns-prefetch-control"]).to.equal(
"off",
"x-dns-prefetch-control"
);
expect(headers["x-frame-options"]).to.equal(
"SAMEORIGIN",
"x-frame-options"
);
expect(headers["x-powered-by"]).to.equal(undefined, "x-powered-by");
expect(headers["strict-transport-security"]).to.equal(
"max-age=15552000; includeSubDomains",
"strict-transport-security"
);
expect(headers["x-download-options"]).to.equal(
"noopen",
"x-download-options"
);
expect(headers["x-content-type-options"]).to.equal(
"nosniff",
"x-content-type-options"
);
expect(headers["x-xss-protection"]).to.equal("0");
})
.end(done);
});
it("sets up default instrumentation", function (done) {
const prom = this.system.container.get("instrumentation");
expect(() => prom.metric("nodejs_memory_heap_used_bytes")).to.not.throw();
expect(() => prom.metric("nodejs_memory_heap_total_bytes")).to.not.throw();
expect(() => prom.metric("http_request_seconds")).to.not.throw();
request(this.system.server)
.get("/__/metrics")
.expect(200)
.expect(/nodejs_memory_heap_used_bytes/)
.expect(/nodejs_memory_heap_total_bytes/)
.expect(/http_request_seconds/)
.end(done);
});
describe("provides a logger that ", function () {
beforeEach(async function () {
if (fs.existsSync('./test.log')) {
await fs.unlinkSync('./test.log');
}
this.system = new Microbe();
const inj = logger({
name: pkg.name,
level: 'trace',
transport: {
targets: [
{
level: 'trace',
target: 'pino/file',
options: { destination: './test.log', sync: true }
},
]
}
})
this.system.di.inject('logger', inj)
this.system.build();
})
it("is namespaced with app name", function () {
expect(this.system.container.get("logger").bindings()).to.have.property(
"name",
pkg.name
);
});
it("attaches to req.logger with correlation id (id) and request id (r)", function (done) {
this.system.route().get("/logger", (req, res) => {
res.json(req.logger.bindings());
});
request(this.system.server)
.get("/logger")
.expect(200)
.expect(function (res) {
expect(res.body).to.have.property("id");
expect(res.body).to.have.property("r");
})
.end(function () {
setTimeout(done, 100);
});
});
it("logs requests", function (done) {
this.system.route().get("/request", (req, res) => {
res.end();
});
request(this.system.server)
.get("/request")
.end(function () {
setTimeout(() => { // pino writes async; wait for file to be complete
fs.readFile("./test.log", "utf-8", (err, data) => {
const log = JSON.parse(data)
expect(log.req.method).to.eql("GET")
expect(log.req.url).to.eql("/request");
done();
})
}, 100);
});
});
it("logs errors with stacks and previous errors", function (done) {
this.system.route().get("/errors", (req, res) => {
const e = new Error("ERROR");
e.previous = new Error("PREVIOUS");
throw e;
});
request(this.system.server)
.get("/errors")
.end((err, res) => {
setTimeout(() => { // pino writes async; wait for file to be complete
fs.readFile("./test.log", "utf-8", (err, data) => {
const log = data.split("\n").filter(t => t).map(JSON.parse)
expect(log[1].error.message).to.eql("ERROR");
expect(log[1].error).to.have.property("stack");
expect(log[1].error.previous.message).to.eql("PREVIOUS");
done();
})
}, 100);
});
});
});
it("renders errors with error.status and error.message only with http status matching error.status", function (done) {
this.system.route().get("/notFound", (req, res) => {
const e = error("NotFoundError", 400);
throw new e("na-ah");
});
request(this.system.server)
.get("/notFound")
.end((err, res) => {
expect(res.body).to.have.property("message", "na-ah");
expect(res.body).to.have.property("status", 400);
expect(res.body).to.have.property("type", "NotFoundError");
expect(res.body).to.have.property("reference");
done();
});
});
it("renders 500/Internal Server Error when no error.status / error.message", function (done) {
this.system.route().get("/internal", (req, res) => {
throw new Error();
});
request(this.system.server)
.get("/internal")
.end((err, res) => {
expect(res.body).to.have.property("message", "Internal Server Error");
expect(res.body).to.have.property("status", 500);
expect(res.body).to.have.property("type", "Error");
done();
});
});
it("can run before() and after() middleware", function (done) {
let result = "";
this.system.before((req, res, next) => {
result = result + "before.";
next();
});
this.system.route().get("/route", (req, res, next) => {
result = result + "route.";
next();
});
this.system.after((req, res, next) => {
result = result + "after";
res.end();
});
request(this.system.server)
.get("/route")
.end((err, res) => {
expect(result).to.equal("before.route.after");
done();
});
});
it("attaches a provided request ID to the request object", function (done) {
const requestId = "79d9e89d-1b2e-4b2b-9184-b51668b223d1";
this.system.route().get("/provided-id", (req, res) => {
expect(req.id).to.equal(requestId);
res.end();
});
request(this.system.server)
.get("/provided-id")
.set({ "X-Request-ID": requestId })
.expect(200, done);
});
it("attaches a generated request ID to the request object if one is not provided", function (done) {
this.system.route().get("/gen-id", (req, res) => {
expect(req.id).to.match(
/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/
);
res.end();
});
request(this.system.server).get("/gen-id").expect(200, done);
});
});