Skip to content

Commit acfdee0

Browse files
committed
Add tests for trustProxy, disallow mode, prodOnly.
1 parent 2307962 commit acfdee0

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

test/test.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,13 @@ test("Tests for https-always", async (t) => {
2222
const defaultsPort = 3080
2323
const httpDefaults = await createHttpServer(defaultsPort, false, {})
2424

25-
t.plan(3)
26-
let result = await fetch(`http://localhost:${defaultsPort}${URL}`, {
27-
redirect: "manual"
28-
})
29-
25+
t.plan(9)
26+
let result = await fetch(`http://localhost:${defaultsPort}${URL}`, {redirect: "manual"})
3027
t.equal(result.status, 301, "http Permanently Moved")
3128
t.equal(result.headers.get("location"), `https://localhost${URL}`, "Location is https with no port")
3229

3330
const trustProxyPort = 3081
34-
const httpTrustProxy = await createHttpServer(trustProxyPort, true, {})
31+
const httpTrustProxy = await createHttpServer(trustProxyPort, true, {httpsPort: 443})
3532

3633
result = await fetch(`http://localhost:${trustProxyPort}${URL}`, {
3734
redirect: "manual",
@@ -40,13 +37,36 @@ test("Tests for https-always", async (t) => {
4037
"x-forwarded-host": "localhost:3443"
4138
}
4239
})
43-
4440
t.equal(result.status, 404, "http with proxy headers Not Found")
4541

42+
result = await fetch(`http://localhost:${trustProxyPort}${URL}`, {redirect: "manual"})
43+
t.equal(result.status, 301, "http Permanently Moved")
44+
t.equal(result.headers.get("location"), `https://localhost:443${URL}`, "Location is https with port")
45+
46+
const disallowPort = 3082
47+
const httpDisallow = await createHttpServer(disallowPort, false, {redirect: false})
48+
result = await fetch(`http://localhost:${disallowPort}${URL}`, {redirect: "manual"})
49+
t.equal(result.status, 403, "http HTTPS Required")
50+
51+
process.env.NODE_ENV = "development"
52+
const devModePort = 3083
53+
const httpDevMode = await createHttpServer(devModePort, false, {productionOnly: false})
54+
result = await fetch(`http://localhost:${devModePort}${URL}`, {redirect: "manual"})
55+
t.equal(result.status, 301, "http dev mode Permanently Moved")
56+
t.equal(result.headers.get("location"), `https://localhost${URL}`, "Location is https with no port")
57+
58+
const prodOnlyPort = 3084
59+
const httpProdOnly = await createHttpServer(prodOnlyPort, false, {productionOnly: true})
60+
result = await fetch(`http://localhost:${prodOnlyPort}${URL}`, {redirect: "manual"})
61+
t.equal(result.status, 404, "http prodOnly Not Found")
62+
4663
await Promise.all([
4764
https.close(),
4865
httpDefaults.close(),
49-
httpTrustProxy.close()
66+
httpTrustProxy.close(),
67+
httpDevMode.close(),
68+
httpProdOnly.close(),
69+
httpDisallow.close()
5070
])
5171
})
5272

0 commit comments

Comments
 (0)