Skip to content

Commit f2b4f16

Browse files
authored
flaky (#6021)
1 parent 97df2df commit f2b4f16

File tree

2 files changed

+55
-23
lines changed

2 files changed

+55
-23
lines changed

test/toolkit.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict'
2+
3+
exports.waitForCb = function (options) {
4+
let count = null
5+
let done = false
6+
let iResolve
7+
let iReject
8+
9+
function stepIn () {
10+
if (done) {
11+
iReject(new Error('Unexpected done call'))
12+
return
13+
}
14+
15+
if (--count) {
16+
return
17+
}
18+
19+
done = true
20+
iResolve()
21+
}
22+
23+
const patience = new Promise((resolve, reject) => {
24+
iResolve = resolve
25+
iReject = reject
26+
})
27+
28+
count = options.steps || 1
29+
done = false
30+
31+
return { stepIn, patience }
32+
}

test/trust-proxy.test.js

+23-23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { test, before } = require('node:test')
44
const sget = require('simple-get').concat
55
const fastify = require('..')
66
const helper = require('./helper')
7+
const { waitForCb } = require('./toolkit')
78

89
const noop = () => {}
910

@@ -69,18 +70,14 @@ test('trust proxy, not add properties to node req', (t, done) => {
6970
})
7071

7172
app.listen({ port: 0 }, (err) => {
72-
app.server.unref()
7373
t.assert.ifError(err)
7474

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 })
7776

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)
8481
})
8582
})
8683

@@ -89,16 +86,15 @@ test('trust proxy chain', (t, done) => {
8986
const app = fastify({
9087
trustProxy: [localhost, '192.168.1.1']
9188
})
89+
t.after(() => app.close())
9290

9391
app.get('/trustproxychain', function (req, reply) {
9492
testRequestValues(t, req, { ip: '1.1.1.1', host: 'example.com', port: app.server.address().port })
9593
reply.code(200).send({ ip: req.ip, host: req.host })
9694
})
9795

9896
app.listen({ port: 0 }, (err) => {
99-
app.server.unref()
10097
t.assert.ifError(err)
101-
t.after(() => app.close())
10298
sgetForwardedRequest(app, '192.168.1.1, 1.1.1.1', '/trustproxychain', undefined, done)
10399
})
104100
})
@@ -108,15 +104,15 @@ test('trust proxy function', (t, done) => {
108104
const app = fastify({
109105
trustProxy: (address) => address === localhost
110106
})
107+
t.after(() => app.close())
108+
111109
app.get('/trustproxyfunc', function (req, reply) {
112110
testRequestValues(t, req, { ip: '1.1.1.1', host: 'example.com', port: app.server.address().port })
113111
reply.code(200).send({ ip: req.ip, host: req.host })
114112
})
115113

116114
app.listen({ port: 0 }, (err) => {
117-
app.server.unref()
118115
t.assert.ifError(err)
119-
t.after(() => app.close())
120116
sgetForwardedRequest(app, '1.1.1.1', '/trustproxyfunc', undefined, done)
121117
})
122118
})
@@ -126,15 +122,15 @@ test('trust proxy number', (t, done) => {
126122
const app = fastify({
127123
trustProxy: 1
128124
})
125+
t.after(() => app.close())
126+
129127
app.get('/trustproxynumber', function (req, reply) {
130128
testRequestValues(t, req, { ip: '1.1.1.1', ips: [localhost, '1.1.1.1'], host: 'example.com', port: app.server.address().port })
131129
reply.code(200).send({ ip: req.ip, host: req.host })
132130
})
133131

134132
app.listen({ port: 0 }, (err) => {
135-
app.server.unref()
136133
t.assert.ifError(err)
137-
t.after(() => app.close())
138134
sgetForwardedRequest(app, '2.2.2.2, 1.1.1.1', '/trustproxynumber', undefined, done)
139135
})
140136
})
@@ -144,15 +140,15 @@ test('trust proxy IP addresses', (t, done) => {
144140
const app = fastify({
145141
trustProxy: `${localhost}, 2.2.2.2`
146142
})
143+
t.after(() => app.close())
144+
147145
app.get('/trustproxyipaddrs', function (req, reply) {
148146
testRequestValues(t, req, { ip: '1.1.1.1', ips: [localhost, '1.1.1.1'], host: 'example.com', port: app.server.address().port })
149147
reply.code(200).send({ ip: req.ip, host: req.host })
150148
})
151149

152150
app.listen({ port: 0 }, (err) => {
153-
app.server.unref()
154151
t.assert.ifError(err)
155-
t.after(() => app.close())
156152
sgetForwardedRequest(app, '3.3.3.3, 2.2.2.2, 1.1.1.1', '/trustproxyipaddrs', undefined, done)
157153
})
158154
})
@@ -162,6 +158,8 @@ test('trust proxy protocol', (t, done) => {
162158
const app = fastify({
163159
trustProxy: true
164160
})
161+
t.after(() => app.close())
162+
165163
app.get('/trustproxyprotocol', function (req, reply) {
166164
testRequestValues(t, req, { ip: '1.1.1.1', protocol: 'lorem', host: 'example.com', port: app.server.address().port })
167165
reply.code(200).send({ ip: req.ip, host: req.host })
@@ -175,14 +173,16 @@ test('trust proxy protocol', (t, done) => {
175173
reply.code(200).send({ ip: req.ip, host: req.host })
176174
})
177175

178-
t.after(() => app.close())
179-
180176
app.listen({ port: 0 }, (err) => {
181-
app.server.unref()
182177
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+
185183
// 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)
187187
})
188188
})

0 commit comments

Comments
 (0)