Skip to content

Commit

Permalink
Add test for trust proxy with function (#1098)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored and mcollina committed Aug 26, 2018
1 parent 6bce249 commit e77cae9
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/trust-proxy.test.js
Expand Up @@ -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'
},
() => {}
)
})
})

0 comments on commit e77cae9

Please sign in to comment.