forked from daquinoaldo/https-localhost
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
135 lines (122 loc) · 4.38 KB
/
test.js
File metadata and controls
135 lines (122 loc) · 4.38 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
const assert = require("assert")
const fs = require("fs")
const http = require("http")
const https = require("https")
const app = require("../index.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()
})
}
// TESTS MODULE
describe("Testing https-localhost", () => {
// close the server after each test
afterEach(() => app.server.close())
it("works as a module as custom express app", async function() {
// 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"))
})
it("works with environment port", async function() {
// 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"))
})
it("serves static files from custom path", async function() {
// 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.data.toString() === fs.readFileSync("test/static.html").toString()))
})
it("serves static files from default env port", async function() {
// 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()))
})
it("doesn't crash on 404", async function() {
// 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))
})
it("looks for a 404.html file", async function() {
// 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 => {
console.log(res)
assert(res.statusCode === 404)
assert(res.data.toString() ===
fs.readFileSync("test/404.html").toString())
})
})
it("doesn't crash if the static path doesn't exists", async function() {
// start the server (serving a non existing folder)
app.serve("do-not-exists")
// make the request and check the status code
await makeRequest("/")
.then(res => assert(res.statusCode === 404))
})
it("redirect http to https", async function() {
// 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))
})
// IMPORTANT: this test MUST be the latest one, always.
// It delete the cache og the index module,
// so the variable app is broken after this test.
it("is ready for production", async function() {
// set NODE_ENV to production
delete require.cache[require.resolve("../index.js")]
process.env.NODE_ENV = "production"
const production = require("../index.js")()
// start the server (serving the test folder)
production.serve("test", HTTPS_PORT)
// make the request and check the output
await makeRequest("/static.html")
.then(res => assert(res.headers["content-encoding"] === "gzip"))
})
})