Skip to content

Commit fc685f4

Browse files
committed
Coverage 100%
1 parent 2921cc4 commit fc685f4

4 files changed

Lines changed: 19 additions & 39 deletions

File tree

index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,23 @@ app.use(compression())
2626
app.set("json spaces", 0)
2727

2828
// redirect http to https
29-
if (port === 443 || process.env.HTTP_PORT)
29+
/* istanbul ignore else */ // useless to test
30+
if (port === 443)
3031
app.http = http.createServer((req, res) => {
3132
res.writeHead(301, { Location: "https://" + req.headers["host"] + req.url })
3233
res.end()
33-
}).listen(process.env.HTTP_PORT || 80)
34+
}).listen(80)
3435

3536
// serve static files, if launched as: "node index.js <static-path>"
36-
if (require.main === module)
37-
app.use(express.static(process.argv[2] || process.cwd()))
37+
/* istanbul ignore else */ // useless to test
38+
if (require.main === module || process.env.USE_STATIC) {
39+
let staticPath = process.cwd()
40+
/* istanbul ignore if */ // impossible to test
41+
if (process.argv.join().includes("serve"))
42+
staticPath = process.argv.join().replace("sudo", "")
43+
.replace("serve", "").replace(" ", "")
44+
app.use(express.static(staticPath))
45+
}
3846

3947
// ready
4048
console.info("Server running on port " + port + ".")

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"pretest": "eslint --ignore-path .gitignore .",
8-
"test": "mocha --exit",
9-
"posttest": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- -R spec ./test/*.js --exit",
8+
"test": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- -R spec ./test/*.js --exit",
109
"start": "node index.js"
1110
},
1211
"bin": {
File renamed without changes.

test/test.js

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,11 @@
1-
process.env.TEST = true
2-
process.env.PORT = 4443
3-
process.env.HTTP_PORT = 8080
1+
process.env.USE_STATIC = true
42

53
const assert = require("assert")
6-
const childProcess = require("child_process")
74
const fs = require("fs")
85
const http = require("http")
96
const https = require("https")
107
const app = require("../index.js")
118

12-
// run an external script located in the scriptPath
13-
function runScript(scriptPath, args = [], background = false) {
14-
return new Promise((resolve, reject) => {
15-
const process = childProcess.fork(scriptPath, args)
16-
process.on("error", err => reject(err))
17-
process.on("exit", code => {
18-
if (code === 0) resolve()
19-
else reject(code)
20-
})
21-
if (background) resolve(process)
22-
})
23-
}
24-
25-
// sleep function, must be used with await
26-
async function sleep(ms = 0) {
27-
return new Promise(resolve => setTimeout(resolve, ms))
28-
}
29-
309
// make an http request on the specified path
3110
function makeRequest(path = "/", secure = false) {
3211
const agentOptions = {
@@ -37,7 +16,7 @@ function makeRequest(path = "/", secure = false) {
3716
}
3817
const options = {
3918
host: "localhost",
40-
port: process.env.HTTP_PORT || 80,
19+
port: 80,
4120
path: path,
4221
method: "GET",
4322
headers: { }
@@ -67,20 +46,14 @@ describe("Testing https-localhost", () => {
6746
.then(res => assert(res.statusCode === 301))
6847
})
6948
it("works as a module", async function() {
70-
app.get("/test", (req, res) => res.send("TEST"))
71-
await makeRequest("/test", true)
49+
app.get("/test/module", (req, res) => res.send("TEST"))
50+
await makeRequest("/test/module", true)
7251
.then(res => assert(res.data === "TEST"))
7352
})
7453
it("serve static file used as standalone tool", async function() {
75-
process.env.PORT = 4444
76-
process.env.HTTP_PORT = 8081
77-
runScript("index.js", ["test"], true)
78-
.then(process => proc = process) // eslint-disable-line no-return-assign
79-
.catch(err => console.error(err))
80-
await sleep(500)
81-
await makeRequest("/test.html", true)
54+
await makeRequest("/test/static.html", true)
8255
.then(res => assert(
83-
res.data.toString() === fs.readFileSync("test/test.html").toString()))
56+
res.data.toString() === fs.readFileSync("test/static.html").toString()))
8457
if (proc) proc.kill("SIGINT")
8558
})
8659
})

0 commit comments

Comments
 (0)