diff --git a/test/trust-proxy.test.js b/test/trust-proxy.test.js index 4e1ab58573..b97a1fd94e 100644 --- a/test/trust-proxy.test.js +++ b/test/trust-proxy.test.js @@ -85,3 +85,35 @@ test('trust proxy chain', (t) => { ) }) }) + +test('trust proxy function', (t) => { + t.plan(5) + const app = fastify({ + trustProxy: (address) => address === '127.0.0.1' + }) + app.get('/trustproxyfunc', function (req, reply) { + t.ok(req.raw.ip, 'ip is defined') + t.ok(req.raw.hostname, 'hostname is defined') + t.equal(req.raw.ip, '1.1.1.1', 'gets ip from x-forwarder-for') + t.equal(req.raw.hostname, 'example.com', 'gets hostname from x-forwarded-host') + reply.code(200).send({ip: req.ip, hostname: req.hostname}) + }) + + t.tearDown(app.close.bind(app)) + + app.listen(0, (err) => { + app.server.unref() + t.error(err) + sget( + { + method: 'GET', + headers: { + 'X-Forwarded-For': '1.1.1.1', + 'X-Forwarded-Host': 'example.com' + }, + url: 'http://localhost:' + app.server.address().port + '/trustproxyfunc' + }, + () => {} + ) + }) +})