Verify response headers for a GET request
Preconditions (2)
✅ Expected Result: The response headers include 'Content-Type' with value 'application/json' and 'Cache-Control' with value 'no-cache'
pm.test('Content-Type header is application/json', () => { const contentType = pm.response.headers.get('Content-Type'); pm.expect(contentType).to.eql('application/json'); }); pm.test('Cache-Control header is no-cache', () => { const cacheControl = pm.response.headers.get('Cache-Control'); pm.expect(cacheControl).to.eql('no-cache'); });
The first test checks if the 'Content-Type' header is exactly 'application/json'. We use pm.response.headers.get() to get the header value and pm.expect().to.eql() for strict equality.
The second test does the same for the 'Cache-Control' header, verifying it equals 'no-cache'.
This approach ensures the headers are present and have the expected values, which is important for API correctness.
Now add data-driven testing with 3 different API endpoints and verify their 'Content-Type' headers
pm.response.headers.get('Content-Type') command do in Postman tests?pm.response.headers.get() is used to read a specific header from the response.'Content-Type' as the argument returns the value of that header from the server's reply.pm.response.headers.get('Header-Name').=== 'no-cache' for exact match.const server = pm.response.headers.get('Server');
pm.test('Server header is Apache', () => {
pm.expect(server).to.equal('Apache');
});pm.expect(server).to.equal('Apache') will fail.pm.test('Content-Length is 1234', () => {
pm.expect(pm.response.headers.get('Content-Length')).to.equal(1234);
});pm.response.headers.get() is always a string.X-Security or if its value is not enabled. Which code snippet correctly implements this?pm.response.headers.has('X-Security') to confirm the header is present.pm.response.headers.get('X-Security') and compare with 'enabled' using to.equal().Pass the test and this pattern moves unknown