0

I have an API which has a response header of Keep-Alive with the following values

Keep-Alive →timeout=15, max=100

I want to assert that the values timeout is at least 10 and at most 100. Earlier I was using Postman BDD library and had this code

let keepAlive = response.getHeader("Keep-Alive");

        // Make sure it contains "max=##"
        let match = /max=(\d+)/.exec(keepAlive);
        expect(match).to.be.an("array").and.not.empty;

        // Make sure the max is between 98 and 100
        let max = parseInt(match[1]);
        expect(max).to.be.at.least(15).and.at.most(100); 

However, if I use Chakram, the getHeader function errors out with TypeError: response.getHeader is not a function . Does anyone has any idea how to get a header value using Chakram.

demouser123
  • 4,108
  • 9
  • 50
  • 82

1 Answers1

1

This is the way to get a header value in Chakram. e.g. to validate if the response is in json format. expect(response).to.have.header('content-type', 'application/json; charset=utf-8'));

The response returned by Chakram is a promise and should be handled as such. So getting the 'keep-Alive' header value:

return chakram.get(serverUrl, params)
.then( chakramResponse => {
    let keepAlive = chakramResponse.response.headers['Keep-Alive'];
    let match = /max=(\d+)/.exec(keepAlive);
    return expect(match).to.be.an("array").and.not.empty;
    let max = parseInt(match[1]);
    return expect(max).to.be.at.least(15).and.at.most(100); 
});
Dinesh Kumar
  • 1,694
  • 15
  • 22