-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathtest.js
More file actions
383 lines (348 loc) · 10.9 KB
/
test.js
File metadata and controls
383 lines (348 loc) · 10.9 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
const assert = require("assert")
const fs = require("fs")
const http = require("http")
const https = require("https")
const tls = require("tls")
const net = require("net")
const sinon = require("sinon")
let app = require("../index.js")()
const certs = require("../certs.js")
const HTTPS_PORT = 4443
const HTTP_PORT = 8080
// make an http request on the specified path
function makeRequest(path = "/", secure = true, port = HTTPS_PORT) {
const options = {
host: "localhost",
port: port,
path: path,
method: "GET",
headers: { "accept-encoding": "gzip" },
rejectUnauthorized: false
}
const protocol = secure ? https : http
return new Promise((resolve, reject) => {
protocol.request(options, resp => {
let data = ""
// eslint-disable-next-line no-return-assign
resp.on("data", chunk => data += chunk)
resp.on("end", () => resolve({
data: data,
statusCode: resp.statusCode,
headers: resp.headers
}))
}).on("error", err => reject(err))
.end()
})
}
// TEST CERTFICATES
describe("Testing certs", function() {
// timeout 5 min, since requires the mkcert executable download
this.timeout(300000)
it("can be uninstalled", () => {
certs.remove()
})
it("uninstall is idempotent (doesn't fail if called twice)", () => {
certs.remove()
})
it("can be installed", function(done) {
certs.generate().then(done)
})
it("can be installed at first run", function(done) {
// inner async function
(async() => {
// remove certs
certs.remove()
// prepare the server with a mock response
app.get("/test/module", (req, res) => res.send("TEST"))
// start the server
await app.listen(HTTPS_PORT)
// make the request and check the output
await makeRequest("/test/module")
.then(res => assert(res.data === "TEST"))
// close the server
app.server.close()
done()
})()
})
it("can be installed in custom folder", function(done) {
// inner async function
(async() => {
// set a custom cert path
process.env.CERT_PATH = "test/custom-folder"
// prepare the server with a mock response
app.get("/test/module", (req, res) => res.send("TEST"))
// start the server
await app.listen(HTTPS_PORT)
// make the request and check the output
await makeRequest("/test/module")
.then(res => assert(res.data === "TEST"))
// close the server
app.server.close()
// restore the CERT_PATH to undefined
delete process.env.CERT_PATH
done()
})()
})
it("crashes if certs doesn't exists in custom folder", function(done) {
// inner async function
(async() => {
// set a custom cert path
process.env.CERT_PATH = "test/custom-folder"
// remove the certificates
fs.unlinkSync("test/custom-folder/localhost.crt")
fs.unlinkSync("test/custom-folder/localhost.key")
// stub the exit function
sinon.stub(process, "exit")
// listen
await app.listen(HTTPS_PORT)
// should exit 1
assert(process.exit.calledWith(1))
process.exit.restore()
// close the server
app.server.close()
// delete the custom folder
certs.remove(process.env.CERT_PATH)
// restore the CERT_PATH to undefined
delete process.env.CERT_PATH
done()
})()
})
it("support path with spaces", function(done) {
// inner async function
(async() => {
// set a custom cert path
process.env.CERT_PATH = "test/custom folder"
// prepare the server with a mock response
app.get("/test/module", (req, res) => res.send("TEST"))
// start the server
await app.listen(HTTPS_PORT)
// make the request and check the output
await makeRequest("/test/module")
.then(res => assert(res.data === "TEST"))
// close the server
app.server.close()
// delete the custom folder
certs.remove(process.env.CERT_PATH)
// restore the CERT_PATH to undefined
delete process.env.CERT_PATH
done()
})()
})
it("provides the certificate", function(done) {
// inner async function
(async() => {
const appCerts = await app.getCerts()
const realCerts = await certs.getCerts()
assert.deepStrictEqual(appCerts, realCerts)
done()
})()
})
it("works with environment domain", function(done) {
(async() => {
// set the environment domain
process.env.HOST = "192.168.0.1"
// Get the cert
const appCerts = await app.getCerts()
// Configure the cert to be able to read it
const secureContext = tls.createSecureContext({
cert: appCerts.cert
})
const secureSocket = new tls.TLSSocket(new net.Socket(), {
secureContext
})
// Read the cert and parse out the domain
const cert = secureSocket.getCertificate()
const certDomain = cert.subjectaltname.split(":")[1]
// Compare the domains
assert(certDomain === process.env.HOST)
done()
})()
})
})
// TESTS MODULE
describe("Testing module", () => {
// close the server after each test
afterEach(() => {
app.server.close()
delete process.env.PORT
})
it("works as express app", function(done) {
(async() => {
// prepare the server with a mock response
app.get("/test/module", (req, res) => res.send("TEST"))
// start the server
await app.listen(HTTPS_PORT)
// make the request and check the output
await makeRequest("/test/module")
.then(res => assert(res.data === "TEST"))
done()
})()
})
it("works with environment port", function(done) {
(async() => {
// prepare the server with a mock response
app.get("/test/module", (req, res) => res.send("TEST"))
// set the environment port
process.env.PORT = HTTPS_PORT
// start the server
await app.listen()
// make the request and check the output
await makeRequest("/test/module")
.then(res => assert(res.data === "TEST"))
done()
})()
})
})
// TEST SCRIPT
describe("Testing serve", () => {
// close the server after each test
afterEach(() => {
app.server.close()
delete process.env.PORT
})
it("serves static files from custom path", function(done) {
(async() => {
// start the server (serving the test folder)port 443 or port 80
app.serve("test", HTTPS_PORT)
// make the request and check the output
await makeRequest("/static.html")
.then(res => assert(res.data.toString() ===
fs.readFileSync("test/static.html").toString()))
done()
})()
})
it("serves static files from default env port", function(done) {
(async() => {
// set the environment port
process.env.PORT = HTTPS_PORT
// start the server (serving the default folder)
app.serve("test")
// make the request and check the output
await makeRequest("/static.html")
.then(res => assert(res.data.toString() ===
fs.readFileSync("test/static.html").toString()))
done()
})()
})
it("includes access-control-allow-origin header", function(done) {
(async() => {
// set the environment port
process.env.PORT = HTTPS_PORT
// start the server (serving the default folder)
app.serve("test")
// make the request and check the output
await makeRequest("/static.html")
.then(res => assert(res.headers["access-control-allow-origin"] ===
"*"))
done()
})()
})
it("doesn't crash on 404", function(done) {
(async() => {
// set the environment port
process.env.PORT = HTTPS_PORT
// start the server (serving the default folder)
app.serve()
// make the request and check the status code
await makeRequest("/do-not-exist")
.then(res => assert(res.statusCode === 404))
done()
})()
})
it("looks for a 404.html file", function(done) {
(async() => {
// start the server (serving the default folder)
await app.serve("test", HTTPS_PORT)
// make the request and check the result
await makeRequest("/do-not-exist.html")
.then(res => {
assert(res.statusCode === 404)
assert(res.data.toString() ===
fs.readFileSync("test/404.html").toString())
})
done()
})()
})
it("doesn't crash if the static path doesn't exists", function(done) {
(async() => {
// start the server (serving a non existing folder)
app.serve("does-not-exist", HTTPS_PORT)
// make the request and check the status code
await makeRequest("/")
.then(res => assert(res.statusCode === 404))
done()
})()
})
})
// TEST REDIRECT
describe("Testing redirect", () => {
// close the server after each test
afterEach(() => {
app.http.close()
delete process.env.PORT
})
it("redirect http to https", function(done) {
(async() => {
// start the redirection
await app.redirect(HTTP_PORT)
// make the request and check the status
await makeRequest("/", false, HTTP_PORT)
.then(res => {
assert(res.statusCode === 301)
assert(res.headers.location === "https://localhost/")
})
done()
})()
})
it("redirect http to https with custom ports", function(done) {
(async() => {
// start the redirection
await app.redirect(HTTP_PORT, HTTPS_PORT)
// make the request and check the status
await makeRequest("/", false, HTTP_PORT)
.then(res => {
assert(res.statusCode === 301)
assert(res.headers.location === "https://localhost:4443/")
})
done()
})()
})
it("redirect http to https with env port", function(done) {
(async() => {
// set the environment port
process.env.PORT = HTTPS_PORT
// start the redirection
await app.redirect(HTTP_PORT)
// make the request and check the status
await makeRequest("/", false, HTTP_PORT)
.then(res => {
assert(res.statusCode === 301)
assert(res.headers.location === "https://localhost:4443/")
})
done()
})()
})
})
// OTHER TESTS
describe("Testing additional features", function() {
// timeout 10 secs, since sometimes 3 secs are not sufficient
this.timeout(10000)
it("is ready for production", function(done) {
(async() => {
// set NODE_ENV to production
delete require.cache[require.resolve("../index.js")]
process.env.NODE_ENV = "production"
app = require("../index.js")()
// start the server (serving the test folder)
app.serve("test", HTTPS_PORT)
// make the request and check the output
await makeRequest("/static.html")
.then(res => assert(res.headers["content-encoding"] === "gzip"))
// reset NODE_ENV and app
delete require.cache[require.resolve("../index.js")]
delete process.env.NODE_ENV
app = require("../index.js")()
done()
})()
})
})