Recently, I worked on validating the consistency between the url
and version
fields in an API response. Here’s a simple script I wrote in Postman to perform this validation.
The API Response
The response we are testing looks like this:
[
{
"label": "Summer '14",
"url": "/services/data/v31.0",
"version": "31.0"
},
{
"label": "Winter '15",
"url": "/services/data/v32.0",
"version": "32.0"
},
{
"label": "Spring '15",
"url": "/services/data/v33.0",
"version": "33.0"
}
]
Each item in the array contains a label
, a url
, and a version
. The goal is to ensure:
- Each
url
starts with/services/data/v
. - The version mentioned in the
url
matches the value in theversion
field.
The Postman Script
Here’s the script I wrote to perform this check:
pm.test("URL matches the format and version matches the version key", function () {
const response = pm.response.json();
response.forEach(item => {
// Extract version from the URL
const urlPattern = /^\/services\/data\/v(\d{2}\.\d)$/;
const match = item.url.match(urlPattern);
// Verify the URL follows the expected format
pm.expect(match).to.not.be.null; // Ensure the URL matches the pattern
// Extract the version from the URL if the pattern matched
const urlVersion = match ? match[1] : null;
// Verify the extracted version matches the version key
pm.expect(urlVersion).to.eql(item.version);
});
});
Detailed Breakdown
Let’s break it down step by step to make it beginner-friendly:
- Accessing the API Response
Thepm.response.json()
method converts the response body into a JavaScript array or object, making it easy to loop through and analyze. - Regex for URL Validation
TheurlPattern
is a regular expression (/^\/services\/data\/v(\d{2}\.\d)$/
) designed to:- Check if the
url
starts with/services/data/v
. - Capture the version number (e.g.,
31.0
) using the parentheses( )
.
- Check if the
- Matching the URL
Theitem.url.match(urlPattern)
method attempts to match theurl
to the regex:- If it matches, it returns an array where the first element is the full match, and the second element is the captured version number.
- If it doesn’t match, it returns
null
.
- Validation 1: URL Format
Usingpm.expect(match).to.not.be.null
, we ensure theurl
adheres to the expected format. If the URL doesn’t match the pattern, this test will fail. - Extracting the Version from the URL
If the URL matches the regex, we extract the version usingmatch[1]
. For example, for/services/data/v31.0
, this would be31.0
. - Validation 2: Version Consistency
The extracted version is compared to theversion
field of the same item usingpm.expect(urlVersion).to.eql(item.version)
. This ensures that theurl
andversion
fields are in sync.
Why This Matters
This test ensures:
- The API response follows a predictable structure.
- The
url
andversion
fields remain consistent, reducing potential bugs in downstream systems consuming this data.
Key Takeaways
- Use regex to validate and extract data from strings.
- Always check for
null
or invalid cases to avoid runtime errors. - Breaking the problem into smaller steps (validation, extraction, comparison) makes it easier to understand and test.