I wanted to test the status code if all the key headers are valid. All I know is the Content-type key. I'm getting an error when doing this to other key headers.
Asked
Active
Viewed 150 times
1 Answers
0
var headers =pm.request.headers.toObject()
console.log(headers)
all headers send request can be accessed using above method in the test script
var headers = pm.request.headers.map()
console.log(headers.length)
_ = require("lodash")
let expectedHeaders = ["Accept","Content-Type"]
let requiredHeaders =_.filter(headers, function (n) {
return expectedHeaders.includes(n.key);
});
_.isEqual(requiredHeaders.map(n=>n.key).sort(), expectedHeaders.sort()) ? pm.test("sometest",function(){
pm.expect(pm.response.code).to.eql(200)
}):null
is here we are filtering out required headers and validating that all headers are present as expected and the running a test if the header is as expected else doing nothing

PDHide
- 18,113
- 2
- 31
- 46