Postman test script for matching 2 key values inside of each item

Postman test script for matching 2 key values inside of each item

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:

  1. Each url starts with /services/data/v.
  2. The version mentioned in the url matches the value in the version 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:

  1. Accessing the API Response
    The pm.response.json() method converts the response body into a JavaScript array or object, making it easy to loop through and analyze.
  2. Regex for URL Validation
    The urlPattern 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 ( ).
  3. Matching the URL
    The item.url.match(urlPattern) method attempts to match the url 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.
  4. Validation 1: URL Format
    Using pm.expect(match).to.not.be.null, we ensure the url adheres to the expected format. If the URL doesn’t match the pattern, this test will fail.
  5. Extracting the Version from the URL
    If the URL matches the regex, we extract the version using match[1]. For example, for /services/data/v31.0, this would be 31.0.
  6. Validation 2: Version Consistency
    The extracted version is compared to the version field of the same item using pm.expect(urlVersion).to.eql(item.version). This ensures that the url and version fields are in sync.

Why This Matters

This test ensures:

  • The API response follows a predictable structure.
  • The url and version 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.

Scroll to Top