Skip to content

Commit b0bfb5a

Browse files
committed
Fix tests
1 parent fce6450 commit b0bfb5a

2 files changed

Lines changed: 38 additions & 31 deletions

File tree

index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
const path = require("path")
44
const fs = require("fs")
55
const http = require("http")
6-
const https = require("spdy") // spdy allows http2, while waiting express to support the http2 module
6+
// spdy allows http2, while waiting express to support the http2 module
7+
const https = require("spdy")
78
const express = require("express")
89
const compression = require("compression")
9-
const minify = require('express-minify')
10-
10+
const minify = require("express-minify")
1111

1212
/* CONFIGURE THE SERVER */
1313

@@ -21,31 +21,30 @@ const certOptions = {
2121
const app = express()
2222

2323
// override the default express listen method to use our server
24-
app.listen = function (port=(process.env.PORT || 443)) {
25-
app.server = https.createServer(certOptions, app)
26-
app.server.listen(port)
24+
app.listen = function(port = process.env.PORT || 443) {
25+
app.server = https.createServer(certOptions, app).listen(port)
2726
console.info("Server running on port " + port + ".")
27+
return app.server
2828
}
2929

3030
// use gzip compression minify
3131
app.use(compression())
3232
app.use(minify())
3333
app.set("json spaces", 0)
3434

35-
3635
/* SETUP USEFUL FUNCTIONS */
3736

3837
// redirect http to https, usage `app.redirect()`
39-
app.redirect = function () {
38+
app.redirect = function() {
4039
app.http = http.createServer((req, res) => {
41-
res.writeHead(301, {Location: "https://" + req.headers["host"] + req.url})
40+
res.writeHead(301, { Location: "https://" + req.headers["host"] + req.url })
4241
res.end()
4342
}).listen(80)
4443
console.info("http to https redirection active.")
4544
}
4645

4746
// serve static content, usage `app.serve([path])`
48-
app.serve = function (path=process.cwd(), port) {
47+
app.serve = function(path = process.cwd(), port) {
4948
app.use(express.static(path))
5049
if (port) app.listen(port)
5150
else app.listen()
@@ -54,9 +53,10 @@ app.serve = function (path=process.cwd(), port) {
5453

5554
/* MAIN (running as script) */
5655
// usage: `serve [<path>]` or `node index.js [<path>]`
56+
/* istanbul ignore if */
5757
if (require.main === module) {
5858
// retrieve the static path from the process argv or use the cwd
59-
// the first is node, the second is serve or index.js, the third (if exists) is the path
59+
// 1st is node, 2nd is serve or index.js, 3rd (if exists) is the path
6060
app.serve(process.argv.length === 3 ? process.argv[2] : process.cwd())
6161
// redirect http to https
6262
app.redirect()

test/test.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
process.env.USE_STATIC = true
2-
process.env.PORT = 4443
3-
process.env.HTTP_PORT = 8080
1+
const PORT = 4443
42

53
const assert = require("assert")
64
const fs = require("fs")
@@ -9,21 +7,15 @@ const https = require("https")
97
const app = require("../index.js")
108

119
// make an http request on the specified path
12-
function makeRequest(path = "/", secure = false) {
13-
const agentOptions = {
14-
host: "localhost",
15-
port: process.env.PORT || 443,
16-
path: "/",
17-
rejectUnauthorized: false
18-
}
10+
function makeRequest(path = "/", secure = true, port = PORT) {
1911
const options = {
2012
host: "localhost",
21-
port: process.env.HTTP_PORT || 80,
13+
port: port,
2214
path: path,
2315
method: "GET",
24-
headers: { }
16+
headers: { },
17+
rejectUnauthorized: false
2518
}
26-
if (secure) options.agent = new https.Agent(agentOptions)
2719
const protocol = secure ? https : http
2820
return new Promise((resolve, reject) => {
2921
protocol.request(options, resp => {
@@ -41,18 +33,33 @@ function makeRequest(path = "/", secure = false) {
4133

4234
// TESTS
4335
describe("Testing https-localhost", () => {
44-
it("redirect http to https", async function() {
45-
await makeRequest()
46-
.then(res => assert(res.statusCode === 301))
47-
})
4836
it("works as a module", async function() {
4937
app.get("/test/module", (req, res) => res.send("TEST"))
50-
await makeRequest("/test/module", true)
38+
await app.listen(PORT)
39+
await makeRequest("/test/module")
5140
.then(res => assert(res.data === "TEST"))
41+
await app.server.close()
42+
})
43+
44+
it("works as a module serving static files", async function() {
45+
app.serve("test", PORT)
46+
await makeRequest("/static.html")
47+
.then(res => assert(
48+
res.data.toString() === fs.readFileSync("test/static.html").toString()))
49+
await app.server.close()
5250
})
53-
it("serve static file used as standalone tool", async function() {
54-
await makeRequest("/test/static.html", true)
51+
52+
it("works on default port", async function() {
53+
app.serve()
54+
await makeRequest("/test/static.html", true, 443)
5555
.then(res => assert(
5656
res.data.toString() === fs.readFileSync("test/static.html").toString()))
57+
await app.server.close()
58+
})
59+
60+
it("redirect http to https", async function() {
61+
await app.redirect()
62+
await makeRequest("/", false, 80)
63+
.then(res => assert(res.statusCode === 301))
5764
})
5865
})

0 commit comments

Comments
 (0)