Skip to content

Commit 2307962

Browse files
committed
Reworked tests to run serially.
1 parent fc581de commit 2307962

File tree

2 files changed

+43
-33
lines changed

2 files changed

+43
-33
lines changed

src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import fp from "fastify-plugin"
44

55

66
export interface HttpsAlwaysOptions extends FastifyPluginOptions {
7-
productionOnly: boolean
8-
enabled: boolean
9-
port: number
10-
redirect: boolean
7+
productionOnly?: boolean
8+
enabled?: boolean
9+
httpsPort?: number
10+
redirect?: boolean
1111
}
1212

1313

@@ -60,7 +60,7 @@ function plugin(fastify: FastifyInstance,
6060
const {
6161
enabled = true,
6262
productionOnly = true,
63-
port,
63+
httpsPort,
6464
redirect = true,
6565
} = opts
6666

@@ -69,7 +69,7 @@ function plugin(fastify: FastifyInstance,
6969
if (!productionOnly || (productionOnly && inProd)) {
7070
const ctx: HttpsAlwaysContext = {
7171
redirect,
72-
port: port ? `:${port}` : ""
72+
port: httpsPort ? `:${httpsPort}` : ""
7373
}
7474
fastify.addHook("onRequest", (q, p, d) => handleRequest(ctx, q, p, d))
7575
}

test/test.ts

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,63 @@ import {fetch} from "undici"
44
import httpsAlwaysPlugin, {HttpsAlwaysOptions} from "../src"
55

66

7-
87
// self-signed testing cert
98
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"
109

1110
const URL = "/a/url"
1211

13-
test("default options for both fastify and https-always", async (t) => {
14-
process.env.NODE_ENV = "production"
15-
const http = Fastify()
12+
test("Tests for https-always", async (t) => {
1613
const https = Fastify({
1714
https: {
1815
key: httpsKey,
1916
cert: httpsCert
2017
}
2118
})
22-
http.register(httpsAlwaysPlugin)
23-
https.register(httpsAlwaysPlugin)
24-
25-
await http.listen({port: 3080})
2619
await https.listen({port: 3443})
27-
await http.ready()
28-
await https.ready()
20+
21+
process.env.NODE_ENV = "production"
22+
const defaultsPort = 3080
23+
const httpDefaults = await createHttpServer(defaultsPort, false, {})
2924

3025
t.plan(3)
31-
let result = await fetch(`http://localhost:3080${URL}`, {
26+
let result = await fetch(`http://localhost:${defaultsPort}${URL}`, {
3227
redirect: "manual"
33-
/*
34-
headers: {
35-
"x-forwarded-proto": "https",
36-
"x-forwarded-host": "localhost:3443"
37-
}
38-
*/
3928
})
29+
4030
t.equal(result.status, 301, "http Permanently Moved")
4131
t.equal(result.headers.get("location"), `https://localhost${URL}`, "Location is https with no port")
4232

43-
result = await fetch("https://localhost:3443/")
44-
t.equal(result.status, 404, "https Not Found")
33+
const trustProxyPort = 3081
34+
const httpTrustProxy = await createHttpServer(trustProxyPort, true, {})
4535

46-
await http.close()
47-
await https.close()
36+
result = await fetch(`http://localhost:${trustProxyPort}${URL}`, {
37+
redirect: "manual",
38+
headers: {
39+
"x-forwarded-proto": "https",
40+
"x-forwarded-host": "localhost:3443"
41+
}
42+
})
43+
44+
t.equal(result.status, 404, "http with proxy headers Not Found")
45+
46+
await Promise.all([
47+
https.close(),
48+
httpDefaults.close(),
49+
httpTrustProxy.close()
50+
])
4851
})
4952

50-
// test trustProxy
5153

52-
// test each default
54+
async function createHttpServer(port: number, trustProxy: boolean, opts: HttpsAlwaysOptions) {
55+
const http = Fastify({
56+
trustProxy
57+
})
58+
http.register(httpsAlwaysPlugin, opts)
59+
await http.listen({port})
60+
await http.ready()
61+
62+
return http
63+
}
5364

5465

5566

@@ -68,8 +79,8 @@ qvCHykPV4ZWc9ilTrS4uTtPRXpi1AkEAzc6e/NGsAecnOJGOrQmwxIUEc2z1DtEM
6879
Ie4dPuPZ7AnTKUVPhq3zLEuE+XNb9MIzcEhMP3mX2J8ZTh5/QKPRUQJBAM0pYbRu
6980
GXlfei3LnTUrSAdIRyc06i3zkWygQztYVJyRodS2vRnhwBTL5MtrgWR5ALKuaAul
7081
TorrsW76xKLvxec=
71-
-----END PRIVATE KEY-----
72-
`
82+
-----END PRIVATE KEY-----`
83+
7384
const httpsCert = `-----BEGIN CERTIFICATE-----
7485
MIICtjCCAh8CFET/HQXpZC6h7CyE2IgC/edV8gsZMA0GCSqGSIb3DQEBCwUAMIGY
7586
MQswCQYDVQQGEwJDQTEZMBcGA1UECAwQQnJpdGlzaCBDb2x1bWJpYTESMBAGA1UE
@@ -86,5 +97,4 @@ ZC1IgTrOw3xS2TjwN/FqL++HAgMBAAEwDQYJKoZIhvcNAQELBQADgYEAu588Rxko
8697
Y994JB9IowC5AWzFLSTm4fzp80JQc1Bv9IapeFxvBucumYEDmQN/opOEcBmzYqRb
8798
iCBkNwSchMbPKWdD0oCU0lIA5CC3jGfKPyFUaFS7RDtDE9GjKHf9iexFxPMNBR3a
8899
kjYW4mD0WOKNcXVIvYfRYtYimf0lyE0Fn9k=
89-
-----END CERTIFICATE-----
90-
`
100+
-----END CERTIFICATE-----`

0 commit comments

Comments
 (0)