Bird
Raised Fist0
Postmantesting~20 mins

Header assertions in Postman - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Header Assertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
assertion
intermediate
2:00remaining
Check for exact header value
You want to verify that the response header Content-Type exactly equals application/json; charset=utf-8. Which Postman test snippet correctly asserts this?
Apm.test('Content-Type is correct', () => { pm.response.to.have.header('content-type', 'application/json'); });
Bpm.test('Content-Type is correct', () => { pm.response.headers.get('Content-Type') === 'application/json; charset=utf-8'; });
Cpm.test('Content-Type is correct', () => { pm.expect(pm.response.headers['Content-Type']).to.equal('application/json; charset=utf-8'); });
Dpm.test('Content-Type is correct', () => { pm.response.to.have.header('Content-Type', 'application/json; charset=utf-8'); });
Attempts:
2 left
💡 Hint
Use Postman's built-in assertion methods for headers.
assertion
intermediate
2:00remaining
Verify header presence without value check
Which Postman test code correctly asserts that the response contains a header named X-Request-ID, regardless of its value?
Apm.test('X-Request-ID header exists', () => { pm.response.to.have.header('X-Request-ID'); });
Bpm.test('X-Request-ID header exists', () => { pm.expect(pm.response.headers.get('X-Request-ID')).to.exist; });
Cpm.test('X-Request-ID header exists', () => { pm.response.headers.has('X-Request-ID'); });
Dpm.test('X-Request-ID header exists', () => { pm.expect(pm.response.headers['X-Request-ID']).to.be.ok; });
Attempts:
2 left
💡 Hint
Use Postman's header assertion method that checks presence only.
Predict Output
advanced
2:00remaining
Output of header value assertion with regex
What will be the result of this Postman test if the response header Server is nginx/1.18.0?
Postman
pm.test('Server header matches nginx version', () => {
  pm.expect(pm.response.headers.get('Server')).to.match(/^nginx\/\d+\.\d+\.\d+$/);
});
ATest fails due to syntax error in regex.
BTest passes because header matches regex exactly.
CTest fails because regex expects three version numbers but header has only two.
DTest passes because regex matches any string starting with 'nginx'.
Attempts:
2 left
💡 Hint
Check the regex pattern and the actual header value carefully.
🔧 Debug
advanced
2:00remaining
Identify the error in header assertion code
This Postman test is intended to check that the Cache-Control header contains the word no-cache. What is wrong with it?
Postman
pm.test('Cache-Control contains no-cache', () => {
  pm.expect(pm.response.headers.get('Cache-Control')).to.include('no-cache');
});
AThe method 'to.include' does not exist; it should be 'to.contain'.
BIf the header is missing, 'get' returns null and 'to.include' causes an error.
CThe header name should be lowercase 'cache-control' to match.
DThe assertion should use 'pm.response.to.have.header' instead.
Attempts:
2 left
💡 Hint
Consider what happens if the header is not present.
🧠 Conceptual
expert
3:00remaining
Best practice for asserting multiple headers in Postman
You want to assert that the response contains headers Content-Type with value application/json and Cache-Control with value no-store. Which approach is best to ensure clear, maintainable tests?
AWrite separate tests for each header: pm.test('Content-Type check', () => { pm.response.to.have.header('Content-Type', 'application/json'); }); pm.test('Cache-Control check', () => { pm.response.to.have.header('Cache-Control', 'no-store'); });
BUse a single assertion with a custom function to check both headers at once.
CWrite one test with multiple assertions inside: pm.test('Headers check', () => { pm.response.to.have.header('Content-Type', 'application/json'); pm.response.to.have.header('Cache-Control', 'no-store'); });
DCombine headers into one string and assert: pm.test('Headers combined', () => { pm.expect(pm.response.headers.toString()).to.include('Content-Type: application/json').and.to.include('Cache-Control: no-store'); });
Attempts:
2 left
💡 Hint
Think about test clarity and reporting.

Practice

(1/5)
1. What does the Postman assertion pm.response.to.have.header('Content-Type') check for?
easy
A. It checks if the response body contains the text 'Content-Type'.
B. It checks if the response includes a header named 'Content-Type'.
C. It verifies the status code of the response is 200.
D. It checks if the request has a header named 'Content-Type'.

Solution

  1. Step 1: Understand the assertion method

    The method pm.response.to.have.header() is used to check response headers.
  2. Step 2: Identify what is being checked

    The argument 'Content-Type' specifies the header name to look for in the response.
  3. Final Answer:

    It checks if the response includes a header named 'Content-Type'. -> Option B
  4. Quick Check:

    Header presence check = It checks if the response includes a header named 'Content-Type'. [OK]
Hint: Look for 'response.to.have.header' to check response headers [OK]
Common Mistakes:
  • Confusing response headers with response body content
  • Checking request headers instead of response headers
  • Assuming it checks status codes
2. Which of the following is the correct syntax to assert that the response header 'Cache-Control' has the value 'no-cache' in Postman?
easy
A. pm.expect(pm.response.headers.get('Cache-Control')).to.eql('no-cache');
B. pm.response.to.have.header('Cache-Control', 'no-cache');
C. pm.expect(response.headers['Cache-Control']).to.equal('no-cache');
D. pm.response.headers('Cache-Control').equals('no-cache');

Solution

  1. Step 1: Recall correct Postman syntax for header value assertion

    Use pm.expect(pm.response.headers.get('Header-Name')).to.eql('value') to check header value.
  2. Step 2: Check each option's syntax

    pm.expect(pm.response.headers.get('Cache-Control')).to.eql('no-cache'); uses correct method headers.get() and assertion to.eql(). Others have syntax errors or incorrect usage.
  3. Final Answer:

    pm.expect(pm.response.headers.get('Cache-Control')).to.eql('no-cache'); -> Option A
  4. Quick Check:

    Use headers.get() with pm.expect() [OK]
Hint: Use headers.get('name') inside pm.expect() for header value checks [OK]
Common Mistakes:
  • Using incorrect method like headers('name')
  • Trying to pass two arguments to to.have.header()
  • Using response.headers as an object without get()
3. Given this Postman test snippet:
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'?
medium
A. The test will be skipped automatically.
B. The test will pass because the header exists.
C. The test will throw a syntax error.
D. The test will fail because the header value is not 'nginx'.

Solution

  1. Step 1: Understand the assertion

    The test expects the 'Server' header value to be exactly 'nginx'.
  2. Step 2: Compare actual header value

    The actual header value is 'Apache', which does not match 'nginx', so assertion fails.
  3. Final Answer:

    The test will fail because the header value is not 'nginx'. -> Option D
  4. Quick Check:

    Value mismatch causes failure [OK]
Hint: Exact value mismatch causes test failure [OK]
Common Mistakes:
  • Assuming header presence is enough to pass
  • Thinking syntax error occurs on value mismatch
  • Believing test skips on assertion failure
4. You wrote this Postman test:
pm.test('Check Content-Length', () => {
  pm.expect(pm.response.headers.get('Content-Length')).to.be('1234');
});

Why does this test fail to run correctly?
medium
A. Because the value '1234' is a number and should not be in quotes.
B. Because 'Content-Length' header does not exist in the response.
C. Because to.be is not a valid assertion method; it should be to.equal or to.eql.
D. Because pm.response.headers.get returns an array, not a string.

Solution

  1. Step 1: Check assertion method correctness

    The method to.be is not a valid Chai assertion method for value equality in Postman.
  2. Step 2: Identify correct assertion method

    Use to.equal or to.eql to compare values correctly.
  3. Final Answer:

    Because to.be is not a valid assertion method; it should be to.equal or to.eql. -> Option C
  4. Quick Check:

    Use to.equal() for value assertions [OK]
Hint: Use to.equal() or to.eql() for value checks, not to.be [OK]
Common Mistakes:
  • Using to.be() instead of to.equal()
  • Assuming header value is numeric without quotes
  • Thinking headers.get() returns array
5. You want to write a Postman test to verify that the response has a header named 'X-Rate-Limit' and its value is a number greater than 1000. Which code snippet correctly achieves this?
hard
A. pm.test('X-Rate-Limit check', () => { const val = Number(pm.response.headers.get('X-Rate-Limit')); pm.expect(val).to.be.above(1000); });
B. pm.test('X-Rate-Limit check', () => { pm.expect(pm.response.headers.get('X-Rate-Limit')).to.be.greaterThan(1000); });
C. pm.test('X-Rate-Limit check', () => { pm.expect(pm.response.headers.has('X-Rate-Limit')).to.equal(true); pm.expect(pm.response.headers.get('X-Rate-Limit') > 1000).to.be.true; });
D. pm.test('X-Rate-Limit check', () => { pm.expect(parseInt(pm.response.headers.get('X-Rate-Limit'))).to.be.greaterThan(1000); });

Solution

  1. Step 1: Extract and convert header value to number

    Use Number() to convert the header string value to a number for comparison.
  2. Step 2: Use correct assertion for numeric comparison

    Use pm.expect(val).to.be.above(1000) to check if the number is greater than 1000.
  3. 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 A
  4. Quick Check:

    Convert header to number, then assert with to.be.above() [OK]
Hint: Convert header string to number before numeric assertions [OK]
Common Mistakes:
  • 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