Skip to content

Spoofing req.ip for testing #4543

Closed Answered by onosendi
onosendi asked this question in Q&A
Feb 1, 2023 · 3 comments · 4 replies
Discussion options

You must be logged in to vote

This seemed to do the trick.

Update

The better solution would be to use light-my-request's remoteAddress option:

const res = await fastify.inject({
  method: 'get',
  url: '/foo',
  remoteAddress: '01.02.03.04',
});

Old answer

fastify.addHook('onRequest', async (req) => {
    Object.defineProperty(req, 'ip', {
        value: '01.02.03.04',
    });
});

Replies: 3 comments 4 replies

Comment options

You must be logged in to vote
3 replies
@onosendi
Comment options

@Uzlopak
Comment options

@onosendi
Comment options

Comment options

You must be logged in to vote
1 reply
@onosendi
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by onosendi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants