0
0
Postmantesting~15 mins

API versioning validation in Postman - Build an Automation Script

Choose your learning style9 modes available
Validate API versioning in the response header
Preconditions (3)
Step 1: Send a GET request to the API endpoint with version v1 in the URL (e.g., https://api.example.com/v1/resource)
Step 2: Check the response status code is 200
Step 3: Verify the response header 'API-Version' exists
Step 4: Verify the 'API-Version' header value is 'v1'
Step 5: Send a GET request to the API endpoint with version v2 in the URL (e.g., https://api.example.com/v2/resource)
Step 6: Check the response status code is 200
Step 7: Verify the response header 'API-Version' exists
Step 8: Verify the 'API-Version' header value is 'v2'
✅ Expected Result: The API responds with status 200 for both versions and the 'API-Version' header matches the requested version.
Automation Requirements - Postman Test Scripts
Assertions Needed:
Response status code is 200
'API-Version' header exists
'API-Version' header value matches requested version
Best Practices:
Use pm.test for assertions
Use pm.response to access response data
Write clear and descriptive test names
Avoid hardcoding URLs inside tests; use environment variables if possible
Automated Solution
Postman
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:

  • Check the response status code is 200 to confirm the request succeeded.
  • Verify the response includes the 'API-Version' header to ensure version info is returned.
  • Assert that the 'API-Version' header value matches the requested version from the URL, confirming correct versioning.

This approach uses Postman's pm API for clear, readable tests and avoids hardcoding version values inside the test script.

Common Mistakes - 3 Pitfalls
Hardcoding the expected API version inside the test script
{'mistake': "Not checking if the 'API-Version' header exists before asserting its value", 'why_bad': 'If the header is missing, the test will throw an error instead of a clear failure message.', 'correct_approach': 'First assert the header exists, then check its value.'}
Ignoring the response status code and only checking headers
Bonus Challenge

Now add data-driven testing with 3 different API versions: v1, v2, and v3

Show Hint