Test Overview
This test sends a GET request to a sample API endpoint and verifies that the response headers contain the expected Content-Type and Cache-Control values.
This test sends a GET request to a sample API endpoint and verifies that the response headers contain the expected Content-Type and Cache-Control values.
pm.test("Response headers contain expected values", function () { pm.response.to.have.header("Content-Type"); pm.expect(pm.response.headers.get("Content-Type")).to.include("application/json"); pm.response.to.have.header("Cache-Control"); pm.expect(pm.response.headers.get("Cache-Control")).to.eql("no-cache"); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Postman is ready to send the request | — | PASS |
| 2 | Send GET request to the API endpoint | Request sent, waiting for response | — | PASS |
| 3 | Receive response with headers | Response received with headers including Content-Type and Cache-Control | — | PASS |
| 4 | Check if response has header 'Content-Type' | Headers available in response | Verify 'Content-Type' header exists | PASS |
| 5 | Verify 'Content-Type' header value includes 'application/json' | Header value is 'application/json; charset=utf-8' | Check header value contains 'application/json' | PASS |
| 6 | Check if response has header 'Cache-Control' | Headers available in response | Verify 'Cache-Control' header exists | PASS |
| 7 | Verify 'Cache-Control' header value equals 'no-cache' | Header value is 'no-cache' | Check header value equals 'no-cache' | PASS |
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