Test Overview
This test sends a GET request to an API endpoint and verifies that the response headers contain the expected content type and server information.
Jump into concepts and practice - no test required
This test sends a GET request to an API endpoint and verifies that the response headers contain the expected content type and server information.
pm.test("Response has correct 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("Response has Server header", function () { pm.response.to.have.header("Server"); pm.expect(pm.response.headers.get("Server")).to.equal("nginx/1.18.0"); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Send GET request to the API endpoint | Request sent, waiting for response | — | PASS |
| 2 | Check if response has 'Content-Type' header | Response received with headers | Verify 'Content-Type' header exists | PASS |
| 3 | Verify 'Content-Type' header includes 'application/json' | Header value is 'application/json; charset=utf-8' | Check header value contains 'application/json' | PASS |
| 4 | Check if response has 'Server' header | Response headers available | Verify 'Server' header exists | PASS |
| 5 | Verify 'Server' header equals 'nginx/1.18.0' | Header value is 'nginx/1.18.0' | Check header value equals expected string | PASS |
pm.response.to.have.header('Content-Type') check for?pm.response.to.have.header() is used to check response headers.pm.expect(pm.response.headers.get('Header-Name')).to.eql('value') to check header value.headers.get() and assertion to.eql(). Others have syntax errors or incorrect usage.pm.test('Check Server header', () => {
pm.expect(pm.response.headers.get('Server')).to.equal('nginx');
});pm.test('Check Content-Length', () => {
pm.expect(pm.response.headers.get('Content-Length')).to.be('1234');
});to.be is not a valid Chai assertion method for value equality in Postman.to.equal or to.eql to compare values correctly.to.be is not a valid assertion method; it should be to.equal or to.eql. -> Option CNumber() to convert the header string value to a number for comparison.pm.expect(val).to.be.above(1000) to check if the number is greater than 1000.