Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set ip in supertest request

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();
      });
  });
like image 263
JuanPablo Avatar asked Dec 18 '14 18:12

JuanPablo


1 Answers

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')
like image 182
Michael Avatar answered Oct 19 '22 23:10

Michael