@@ -4,6 +4,7 @@ const { test, before } = require('node:test')
4
4
const sget = require ( 'simple-get' ) . concat
5
5
const fastify = require ( '..' )
6
6
const helper = require ( './helper' )
7
+ const { waitForCb } = require ( './toolkit' )
7
8
8
9
const noop = ( ) => { }
9
10
@@ -69,18 +70,14 @@ test('trust proxy, not add properties to node req', (t, done) => {
69
70
} )
70
71
71
72
app . listen ( { port : 0 } , ( err ) => {
72
- app . server . unref ( )
73
73
t . assert . ifError ( err )
74
74
75
- sgetForwardedRequest ( app , '1.1.1.1' , '/trustproxy' , undefined , completed )
76
- sgetForwardedRequest ( app , '2.2.2.2, 1.1.1.1' , '/trustproxychain' , undefined , completed )
75
+ const completion = waitForCb ( { steps : 2 } )
77
76
78
- let pending = 2
79
- function completed ( ) {
80
- if ( -- pending === 0 ) {
81
- done ( )
82
- }
83
- }
77
+ sgetForwardedRequest ( app , '1.1.1.1' , '/trustproxy' , undefined , completion . stepIn )
78
+ sgetForwardedRequest ( app , '2.2.2.2, 1.1.1.1' , '/trustproxychain' , undefined , completion . stepIn )
79
+
80
+ completion . patience . then ( done )
84
81
} )
85
82
} )
86
83
@@ -89,16 +86,15 @@ test('trust proxy chain', (t, done) => {
89
86
const app = fastify ( {
90
87
trustProxy : [ localhost , '192.168.1.1' ]
91
88
} )
89
+ t . after ( ( ) => app . close ( ) )
92
90
93
91
app . get ( '/trustproxychain' , function ( req , reply ) {
94
92
testRequestValues ( t , req , { ip : '1.1.1.1' , host : 'example.com' , port : app . server . address ( ) . port } )
95
93
reply . code ( 200 ) . send ( { ip : req . ip , host : req . host } )
96
94
} )
97
95
98
96
app . listen ( { port : 0 } , ( err ) => {
99
- app . server . unref ( )
100
97
t . assert . ifError ( err )
101
- t . after ( ( ) => app . close ( ) )
102
98
sgetForwardedRequest ( app , '192.168.1.1, 1.1.1.1' , '/trustproxychain' , undefined , done )
103
99
} )
104
100
} )
@@ -108,15 +104,15 @@ test('trust proxy function', (t, done) => {
108
104
const app = fastify ( {
109
105
trustProxy : ( address ) => address === localhost
110
106
} )
107
+ t . after ( ( ) => app . close ( ) )
108
+
111
109
app . get ( '/trustproxyfunc' , function ( req , reply ) {
112
110
testRequestValues ( t , req , { ip : '1.1.1.1' , host : 'example.com' , port : app . server . address ( ) . port } )
113
111
reply . code ( 200 ) . send ( { ip : req . ip , host : req . host } )
114
112
} )
115
113
116
114
app . listen ( { port : 0 } , ( err ) => {
117
- app . server . unref ( )
118
115
t . assert . ifError ( err )
119
- t . after ( ( ) => app . close ( ) )
120
116
sgetForwardedRequest ( app , '1.1.1.1' , '/trustproxyfunc' , undefined , done )
121
117
} )
122
118
} )
@@ -126,15 +122,15 @@ test('trust proxy number', (t, done) => {
126
122
const app = fastify ( {
127
123
trustProxy : 1
128
124
} )
125
+ t . after ( ( ) => app . close ( ) )
126
+
129
127
app . get ( '/trustproxynumber' , function ( req , reply ) {
130
128
testRequestValues ( t , req , { ip : '1.1.1.1' , ips : [ localhost , '1.1.1.1' ] , host : 'example.com' , port : app . server . address ( ) . port } )
131
129
reply . code ( 200 ) . send ( { ip : req . ip , host : req . host } )
132
130
} )
133
131
134
132
app . listen ( { port : 0 } , ( err ) => {
135
- app . server . unref ( )
136
133
t . assert . ifError ( err )
137
- t . after ( ( ) => app . close ( ) )
138
134
sgetForwardedRequest ( app , '2.2.2.2, 1.1.1.1' , '/trustproxynumber' , undefined , done )
139
135
} )
140
136
} )
@@ -144,15 +140,15 @@ test('trust proxy IP addresses', (t, done) => {
144
140
const app = fastify ( {
145
141
trustProxy : `${ localhost } , 2.2.2.2`
146
142
} )
143
+ t . after ( ( ) => app . close ( ) )
144
+
147
145
app . get ( '/trustproxyipaddrs' , function ( req , reply ) {
148
146
testRequestValues ( t , req , { ip : '1.1.1.1' , ips : [ localhost , '1.1.1.1' ] , host : 'example.com' , port : app . server . address ( ) . port } )
149
147
reply . code ( 200 ) . send ( { ip : req . ip , host : req . host } )
150
148
} )
151
149
152
150
app . listen ( { port : 0 } , ( err ) => {
153
- app . server . unref ( )
154
151
t . assert . ifError ( err )
155
- t . after ( ( ) => app . close ( ) )
156
152
sgetForwardedRequest ( app , '3.3.3.3, 2.2.2.2, 1.1.1.1' , '/trustproxyipaddrs' , undefined , done )
157
153
} )
158
154
} )
@@ -162,6 +158,8 @@ test('trust proxy protocol', (t, done) => {
162
158
const app = fastify ( {
163
159
trustProxy : true
164
160
} )
161
+ t . after ( ( ) => app . close ( ) )
162
+
165
163
app . get ( '/trustproxyprotocol' , function ( req , reply ) {
166
164
testRequestValues ( t , req , { ip : '1.1.1.1' , protocol : 'lorem' , host : 'example.com' , port : app . server . address ( ) . port } )
167
165
reply . code ( 200 ) . send ( { ip : req . ip , host : req . host } )
@@ -175,14 +173,16 @@ test('trust proxy protocol', (t, done) => {
175
173
reply . code ( 200 ) . send ( { ip : req . ip , host : req . host } )
176
174
} )
177
175
178
- t . after ( ( ) => app . close ( ) )
179
-
180
176
app . listen ( { port : 0 } , ( err ) => {
181
- app . server . unref ( )
182
177
t . assert . ifError ( err )
183
- sgetForwardedRequest ( app , '1.1.1.1' , '/trustproxyprotocol' , 'lorem' )
184
- sgetForwardedRequest ( app , '1.1.1.1' , '/trustproxynoprotocol' )
178
+
179
+ const completion = waitForCb ( { steps : 3 } )
180
+ sgetForwardedRequest ( app , '1.1.1.1' , '/trustproxyprotocol' , 'lorem' , completion . stepIn )
181
+ sgetForwardedRequest ( app , '1.1.1.1' , '/trustproxynoprotocol' , undefined , completion . stepIn )
182
+
185
183
// Allow for sgetForwardedRequest requests above to finish
186
- setTimeout ( ( ) => sgetForwardedRequest ( app , '1.1.1.1' , '/trustproxyprotocols' , 'ipsum, dolor' , done ) )
184
+ setTimeout ( ( ) => sgetForwardedRequest ( app , '1.1.1.1' , '/trustproxyprotocols' , 'ipsum, dolor' , completion . stepIn ) )
185
+
186
+ completion . patience . then ( done )
187
187
} )
188
188
} )
0 commit comments