Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check not exist header field with supertest?

If I set the express.js like this:

app.set("x-powered-by", false);

How to test this?

.expect('X-Powered-By', '', done)

will throw error: Error: expected "X-Powered-By" header field

.expect('X-Powered-By', null, done)

also not works.

Thanks!

like image 983
HaveF Avatar asked Oct 09 '15 11:10

HaveF


2 Answers

I would suggest creating custom validation as follows:

it('should validate header is not present.', function(done){
  request
    .get('/path/to/your/resource')
    .end(function(error, response){
      if (error) {
        return done(error);
      }
      // Here is where we identify is header exists or not
      var isHeaderPresent = response.header['header-to-validate'] !== undefined;
      isHeaderPresent.should.be.False();
      done();
    });
});

Note also that this example uses should library for its validations.

like image 64
leo.fcx Avatar answered Oct 05 '22 01:10

leo.fcx


This is the assert you need (using Jest):

.expect((res) => expect(res.headers['X-Powered-By']).toBeUndefined())

like image 28
MEMark Avatar answered Oct 05 '22 01:10

MEMark