with supertest, I can make a resquest to test my node.js application
var request = require('supertest');
var api = require('../server').app;
...
it('json response', function(done){
request(api)
.get('/api')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.end(function(err, res){
done();
});
});
how I can set a specific ip to make the test request ?
it('ip access denied', function(done){
request(api)
.get('/api')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
// set specific ip
.end(function(err, res){
res.body.message.should.eql('Access denied');
done();
});
});
Assuming you are checking for an ip address with req.ip
via express (although other frameworks will probably behave the same),
set X-Forwarded-For
header in your test with the required ip address:
.set('X-Forwarded-For', '192.168.2.1')
You might need to enable the trust proxy option in your server:
app.enable('trust proxy')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With