Validate API versioning in the response header
Preconditions (3)
✅ Expected Result: The API responds with status 200 for both versions and the 'API-Version' header matches the requested version.
const requestedVersion = pm.request.url.path[0]; pm.test('Status code is 200', () => { pm.response.to.have.status(200); }); pm.test('Response has API-Version header', () => { pm.response.to.have.header('API-Version'); }); pm.test(`API-Version header matches requested version: ${requestedVersion}`, () => { const apiVersionHeader = pm.response.headers.get('API-Version'); pm.expect(apiVersionHeader).to.eql(requestedVersion); });
This Postman test script extracts the version part from the request URL path (e.g., 'v1' or 'v2').
It then runs three tests:
This approach uses Postman's pm API for clear, readable tests and avoids hardcoding version values inside the test script.
Now add data-driven testing with 3 different API versions: v1, v2, and v3
pm.test('Check API version', () => {
pm.expect(pm.response.json().version).to.equal('2.0');
});pm.test('API version is v1', () => {
pm.expect(pm.response.headers['api-version']).to.eql('v1');
});
But the test always fails. What is the likely issue?Pass the test and this pattern moves unknown