Header assertions check if the response headers from a server are correct. This helps ensure the server sends the right information about the response.
Header assertions in Postman
Start learning this pattern below
Jump into concepts and practice - no test required
pm.test("Header assertion name", function () { pm.response.to.have.header("Header-Name"); pm.expect(pm.response.headers.get("Header-Name")).to.eql("Expected-Value"); });
Use pm.response.to.have.header() to check if a header exists.
Use pm.response.headers.get() to get the header value for exact matching.
pm.test("Content-Type is JSON", function () { pm.response.to.have.header("Content-Type"); pm.expect(pm.response.headers.get("Content-Type")).to.include("application/json"); });
pm.test("Server header is correct", function () { pm.response.to.have.header("Server"); pm.expect(pm.response.headers.get("Server")).to.eql("nginx"); });
pm.test("Cache-Control header exists", function () { pm.response.to.have.header("Cache-Control"); });
This Postman test script checks two headers: it confirms the Content-Type header exists and includes 'application/json', and it verifies the Server header exactly equals 'nginx'.
pm.test("Check Content-Type header", function () { pm.response.to.have.header("Content-Type"); pm.expect(pm.response.headers.get("Content-Type")).to.include("application/json"); }); pm.test("Check Server header", function () { pm.response.to.have.header("Server"); pm.expect(pm.response.headers.get("Server")).to.eql("nginx"); });
Header names are case-insensitive but use the exact casing for readability.
Use to.include() when the header value may have extra details (like charset).
Check for header existence before checking its value to avoid errors.
Header assertions verify the presence and value of response headers.
They help confirm the server sends correct metadata about the response.
Use Postman's pm.response.to.have.header() and pm.expect() for clear tests.
Practice
pm.response.to.have.header('Content-Type') check for?Solution
Step 1: Understand the assertion method
The methodpm.response.to.have.header()is used to check response headers.Step 2: Identify what is being checked
The argument 'Content-Type' specifies the header name to look for in the response.Final Answer:
It checks if the response includes a header named 'Content-Type'. -> Option BQuick Check:
Header presence check = It checks if the response includes a header named 'Content-Type'. [OK]
- Confusing response headers with response body content
- Checking request headers instead of response headers
- Assuming it checks status codes
Solution
Step 1: Recall correct Postman syntax for header value assertion
Usepm.expect(pm.response.headers.get('Header-Name')).to.eql('value')to check header value.Step 2: Check each option's syntax
pm.expect(pm.response.headers.get('Cache-Control')).to.eql('no-cache'); uses correct methodheaders.get()and assertionto.eql(). Others have syntax errors or incorrect usage.Final Answer:
pm.expect(pm.response.headers.get('Cache-Control')).to.eql('no-cache'); -> Option AQuick Check:
Use headers.get() with pm.expect() [OK]
- Using incorrect method like headers('name')
- Trying to pass two arguments to to.have.header()
- Using response.headers as an object without get()
pm.test('Check Server header', () => {
pm.expect(pm.response.headers.get('Server')).to.equal('nginx');
});What will happen if the response header 'Server' is 'Apache'?
Solution
Step 1: Understand the assertion
The test expects the 'Server' header value to be exactly 'nginx'.Step 2: Compare actual header value
The actual header value is 'Apache', which does not match 'nginx', so assertion fails.Final Answer:
The test will fail because the header value is not 'nginx'. -> Option DQuick Check:
Value mismatch causes failure [OK]
- Assuming header presence is enough to pass
- Thinking syntax error occurs on value mismatch
- Believing test skips on assertion failure
pm.test('Check Content-Length', () => {
pm.expect(pm.response.headers.get('Content-Length')).to.be('1234');
});Why does this test fail to run correctly?
Solution
Step 1: Check assertion method correctness
The methodto.beis not a valid Chai assertion method for value equality in Postman.Step 2: Identify correct assertion method
Useto.equalorto.eqlto compare values correctly.Final Answer:
Becauseto.beis not a valid assertion method; it should beto.equalorto.eql. -> Option CQuick Check:
Use to.equal() for value assertions [OK]
- Using to.be() instead of to.equal()
- Assuming header value is numeric without quotes
- Thinking headers.get() returns array
Solution
Step 1: Extract and convert header value to number
UseNumber()to convert the header string value to a number for comparison.Step 2: Use correct assertion for numeric comparison
Usepm.expect(val).to.be.above(1000)to check if the number is greater than 1000.Final Answer:
pm.test('X-Rate-Limit check', () => { const val = Number(pm.response.headers.get('X-Rate-Limit')); pm.expect(val).to.be.above(1000); }); -> Option AQuick Check:
Convert header to number, then assert with to.be.above() [OK]
- Using to.be.greaterThan() which is not a valid Chai method
- Not converting header value to number before comparison
- Trying to compare header string directly to number
