@@ -4,52 +4,63 @@ import {fetch} from "undici"
44import httpsAlwaysPlugin , { HttpsAlwaysOptions } from "../src"
55
66
7-
87// self-signed testing cert
98process . env . NODE_TLS_REJECT_UNAUTHORIZED = "0"
109
1110const 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
6879Ie4dPuPZ7AnTKUVPhq3zLEuE+XNb9MIzcEhMP3mX2J8ZTh5/QKPRUQJBAM0pYbRu
6980GXlfei3LnTUrSAdIRyc06i3zkWygQztYVJyRodS2vRnhwBTL5MtrgWR5ALKuaAul
7081TorrsW76xKLvxec=
71- -----END PRIVATE KEY-----
72- `
82+ -----END PRIVATE KEY-----`
83+
7384const httpsCert = `-----BEGIN CERTIFICATE-----
7485MIICtjCCAh8CFET/HQXpZC6h7CyE2IgC/edV8gsZMA0GCSqGSIb3DQEBCwUAMIGY
7586MQswCQYDVQQGEwJDQTEZMBcGA1UECAwQQnJpdGlzaCBDb2x1bWJpYTESMBAGA1UE
@@ -86,5 +97,4 @@ ZC1IgTrOw3xS2TjwN/FqL++HAgMBAAEwDQYJKoZIhvcNAQELBQADgYEAu588Rxko
8697Y994JB9IowC5AWzFLSTm4fzp80JQc1Bv9IapeFxvBucumYEDmQN/opOEcBmzYqRb
8798iCBkNwSchMbPKWdD0oCU0lIA5CC3jGfKPyFUaFS7RDtDE9GjKHf9iexFxPMNBR3a
8899kjYW4mD0WOKNcXVIvYfRYtYimf0lyE0Fn9k=
89- -----END CERTIFICATE-----
90- `
100+ -----END CERTIFICATE-----`
0 commit comments