Search code examples
node.jsexpresssupertest

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!


Solution

  • 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.