I'm writing unit tests for a Node.js Express server. The server does some stuff that depends on the remote client's IP address, retrieved using req.ip
on the server.
How can I write unit tests that will spoof their own IP addresses to test the server's responses, or is there a better way to achieve the same end? (I'm writing the unit tests in jasmine-node, if it matters.)
You can use the trust proxy feature of express to fake requests from ip addresses for your tests. When you enable trust proxy in express by app.enable('trust proxy')
you can add the X-Forwarded-For
HTTP header and set it's value to the ip required by the test.
The express ip/trust proxy behavior is documented here