Postman Test Example for API Endpoint Configuration: Ensuring Required Fields and URL Patterns

Postman Test Example for API Endpoint Configuration: Ensuring Required Fields and URL Patterns

API Response Validation test I created to ensure that all critical URL, path, and configuration fields within the API response meet specific criteria. This test suite is designed to handle different data types, such as URLs, booleans, nullable fields, and various path patterns to match our application’s routing standards.

GET Request

https://www.iqos.com/content/pmisite/ae/en.pagePaths.T1729767812802.V0189584000_9283711_7101_4202.json

The Response

Click here to view the full response
{"basePathImage":"/content/pmisite/ae/en/.rrp.","booking":"/ae/en/appointments.html","resetPasswordPagePath":"/ae/en/reset-password.html","searchPmiCeAppPage":null,"registerSFInStore":"","notificationAppointment":null,"additionalContactDetailPath":"","secondRegisterPagePath":"","homePage":"/ae/en/home.html","storeLocator":"/ae/en/store-locator.html","confirmProfilePath":"/ae/en/confirm-profile.html","basePathProductLendingImage":"/content/pmisite/ae/en.rrpLending.","loyaltyPagePath":"","pathCheckSession":null,"manualDeviceActivationPath":"","defaultUrlsRedirect":"e30=","adultConsumerIdentificationPagePath":"","activityGrind":null,"faqPagePath":"/ae/en/support/faqs","selfRegistrationInStore":"","loginSFOnline":"","appointmentListPagePath":"/ae/en/appointments.html","secondLoginPagePath":"","orderReturn":"/ae/en/order-return.html","loginSFInStore":"","qureReplacementInfoPath":"/ae/en/replacement-info.html","pageEligibilityCheck":"/ae/en/eligibility.html","selfRegBenefitsPath":null,"forgottenPassword":"/ae/en/login.html","phoneRegistrationRedirect":"","checkHavStatusPage":"/ae/en/hav-form-j4.html","pathCheckSSO":null,"redirectAfterDeleteLink":"/ae/en/delete-confirmation.html","havPagePath":"","login":"/ae/en/login.html","registerSFOnline":"","pathSuccesGetAllMyInfo":"/ae/en/get-all-my-info-success.html","veevActivateLandingPagePath":"","waitePagePath":"/ae/en/wait-component.html","checkout":"/ae/en/checkout.html","carePlusPagePath":"/ae/en/care-plus.html","referFriendPagePath":"/ae/en/refer-friend.html","pathRewriteSSO":null,"myAccountOnBehalf":"/ae/en/my-account-on-behalf.html","productDetail":"/ae/en/product.html","shoppingBag":"/ae/en/shopping-bag.html","selfRegistrationOnline":"","welcomePathFromCheckout":"","enableRedirectToRegistration":false,"promotionBurningPath":"","checkoutPPLPath":"https://www.iqos.com/ae/en/checkout-2.html","selfRegistrationChoose":"","multipleChoicePagePath":"","barcodeScannerPage":"/ae/en/Scanner.html","troubleshootDevice":"/ae/en/device-diagnostic.html","preferenceCenterPagePath":"/ae/en/preference-center.html","checkNotHavStatusPage":"/ae/en/complete-hav.html","myAccount":"/ae/en/my-account.html","basePagePath":null,"register":"/ae/en/register.html","pageLocationInformation":null}


Test Script

pm.test("API Response Validation", function () {
    const jsonData = pm.response.json();
    
    // Helper function to validate URL or path pattern
    function isValidPath(path) {
        // Check if path is null, an empty string, or matches specific path format or URL pattern
        const pathPattern = /^\/(content\/pmisite\/ae\/en|ae\/en)/;
        const urlPattern = /^https?:\/\/.+/;
        return path === null || path === "" || pathPattern.test(path) || urlPattern.test(path);
    }
    
    // Fields that should not be empty
    const nonEmptyFields = [
        "basePathImage",
        "booking",
        "resetPasswordPagePath",
        "homePage",
        "storeLocator",
        "confirmProfilePath",
        "basePathProductLendingImage",
        "faqPagePath",
        "login",
        "pathSuccesGetAllMyInfo",
        "checkout",
        "carePlusPagePath",
        "referFriendPagePath",
        "myAccount",
        "register"
    ];
    nonEmptyFields.forEach(field => {
        pm.test(`${field} should not be empty`, function () {
            pm.expect(jsonData[field]).to.not.be.empty;
        });
    });

    // Fields that should match a path pattern or be a URL
    const pathFields = [
        "basePathImage",
        "booking",
        "resetPasswordPagePath",
        "homePage",
        "storeLocator",
        "confirmProfilePath",
        "basePathProductLendingImage",
        "defaultUrlsRedirect",
        "faqPagePath",
        "appointmentListPagePath",
        "orderReturn",
        "qureReplacementInfoPath",
        "pageEligibilityCheck",
        "forgottenPassword",
        "checkHavStatusPage",
        "redirectAfterDeleteLink",
        "login",
        "pathSuccesGetAllMyInfo",
        "waitePagePath",
        "checkout",
        "carePlusPagePath",
        "referFriendPagePath",
        "myAccountOnBehalf",
        "productDetail",
        "shoppingBag",
        "checkoutPPLPath",
        "barcodeScannerPage",
        "troubleshootDevice",
        "preferenceCenterPagePath",
        "checkNotHavStatusPage",
        "myAccount",
        "register"
    ];
    pathFields.forEach(field => {
        pm.test(`${field} should match the path pattern or be a URL`, function () {
            const isBase64 = field === "defaultUrlsRedirect" && /^[A-Za-z0-9+/]+={0,2}$/.test(jsonData[field]);
            pm.expect(isValidPath(jsonData[field]) || isBase64).to.be.true;
        });
    });

    // Fields that should be null, match the path pattern, or be an empty string
    const nullablePathFields = [
        "additionalContactDetailPath",
        "secondRegisterPagePath",
        "pathCheckSession",
        "manualDeviceActivationPath",
        "notificationAppointment",
        "searchPmiCeAppPage",
        "pathCheckSSO",
        "pathRewriteSSO",
        "welcomePathFromCheckout"
    ];
    nullablePathFields.forEach(field => {
        pm.test(`${field} should be null, match the path pattern, or be an empty string`, function () {
            pm.expect(isValidPath(jsonData[field])).to.be.true;
        });
    });

    // Boolean field
    pm.test("enableRedirectToRegistration should be a boolean", function () {
        pm.expect(jsonData.enableRedirectToRegistration).to.be.a("boolean");
    });

    // Full URL fields
    pm.test("checkoutPPLPath should be a full URL starting with https", function () {
        pm.expect(jsonData.checkoutPPLPath).to.match(/^https:\/\/.+/);
    });
});
Click here to see the test results
ResultFieldCondition
PASSbasePathImageshould not be empty
PASSbookingshould not be empty
PASSresetPasswordPagePathshould not be empty
PASShomePageshould not be empty
PASSstoreLocatorshould not be empty
PASSconfirmProfilePathshould not be empty
PASSbasePathProductLendingImageshould not be empty
PASSfaqPagePathshould not be empty
PASSloginshould not be empty
PASSpathSuccesGetAllMyInfoshould not be empty
PASScheckoutshould not be empty
PASScarePlusPagePathshould not be empty
PASSreferFriendPagePathshould not be empty
PASSmyAccountshould not be empty
PASSregistershould not be empty
PASSbasePathImageshould match the path pattern or be a URL
PASSbookingshould match the path pattern or be a URL
PASSresetPasswordPagePathshould match the path pattern or be a URL
PASShomePageshould match the path pattern or be a URL
PASSstoreLocatorshould match the path pattern or be a URL
PASSconfirmProfilePathshould match the path pattern or be a URL
PASSbasePathProductLendingImageshould match the path pattern or be a URL
PASSdefaultUrlsRedirectshould match the path pattern or be a URL
PASSfaqPagePathshould match the path pattern or be a URL
PASSappointmentListPagePathshould match the path pattern or be a URL
PASSorderReturnshould match the path pattern or be a URL
PASSqureReplacementInfoPathshould match the path pattern or be a URL
PASSpageEligibilityCheckshould match the path pattern or be a URL
PASSforgottenPasswordshould match the path pattern or be a URL
PASScheckHavStatusPageshould match the path pattern or be a URL
PASSredirectAfterDeleteLinkshould match the path pattern or be a URL
PASSloginshould match the path pattern or be a URL
PASSpathSuccesGetAllMyInfoshould match the path pattern or be a URL
PASSwaitePagePathshould match the path pattern or be a URL
PASScheckoutshould match the path pattern or be a URL
PASScarePlusPagePathshould match the path pattern or be a URL
PASSreferFriendPagePathshould match the path pattern or be a URL
PASSmyAccountOnBehalfshould match the path pattern or be a URL
PASSproductDetailshould match the path pattern or be a URL
PASSshoppingBagshould match the path pattern or be a URL
PASScheckoutPPLPathshould match the path pattern or be a URL
PASSbarcodeScannerPageshould match the path pattern or be a URL
PASStroubleshootDeviceshould match the path pattern or be a URL
PASSpreferenceCenterPagePathshould match the path pattern or be a URL
PASScheckNotHavStatusPageshould match the path pattern or be a URL
PASSmyAccountshould match the path pattern or be a URL
PASSregistershould match the path pattern or be a URL
PASSadditionalContactDetailPathshould be null, match the path pattern, or be an empty string
PASSsecondRegisterPagePathshould be null, match the path pattern, or be an empty string
PASSpathCheckSessionshould be null, match the path pattern, or be an empty string
PASSmanualDeviceActivationPathshould be null, match the path pattern, or be an empty string
PASSnotificationAppointmentshould be null, match the path pattern, or be an empty string
PASSsearchPmiCeAppPageshould be null, match the path pattern, or be an empty string
PASSpathCheckSSOshould be null, match the path pattern, or be an empty string
PASSpathRewriteSSOshould be null, match the path pattern, or be an empty string
PASSwelcomePathFromCheckoutshould be null, match the path pattern, or be an empty string
PASSenableRedirectToRegistrationshould be a boolean
PASScheckoutPPLPathshould be a full URL starting with https


Objective

The purpose of this test is to validate fields in our API response that must meet one of the following conditions:

  1. Non-empty fields: Required fields that cannot be empty.
  2. URL/path pattern matching fields: Fields that need to follow a URL or path format.
  3. Nullable path fields: Fields that can either be null, an empty string, or match a specific path format.
  4. Boolean fields: Fields that must strictly hold boolean values.
  5. Full URL fields: Fields that require an absolute URL, specifically beginning with HTTPS for secure connections.

Implementation Details

I’ll walk through each category tested, explaining the methods and logic applied to validate the different fields.

1. Helper Function: isValidPath()

The isValidPath() helper function is a reusable utility within the test script. It performs pattern matching to check if a field is:

  • Either null, an empty string, or follows a URL/path pattern.

The function logic:

  • Defines a path pattern (pathPattern) using regex for application-specific routes (e.g., /content/pmisite/ae/en).
  • Checks if the path matches this pattern or if it’s a full URL (urlPattern).
  • Allows null and empty values to be considered valid for certain fields, which aligns with how some fields can optionally be empty.

2. Non-Empty Field Validation

Certain fields in the API response should never be empty. These fields contain essential links or resources that the app needs to render various pages correctly. I created an array, nonEmptyFields, with these field names.

For each field in nonEmptyFields, I created a test using pm.test():

pm.test(`${field} should not be empty`, function () {
pm.expect(jsonData[field]).to.not.be.empty;
});

This ensures that each field in nonEmptyFields returns a non-empty response.

3. Path or URL Matching Fields

Some fields should either match our specific path format or be a valid URL. These are checked against the URL format pattern (urlPattern) or the path format pattern (pathPattern). I stored these fields in the pathFields array.

I then tested each field as follows:

pm.test(`${field} should match the path pattern or be a URL`, function () {
pm.expect(isValidPath(jsonData[field])).to.be.true;
});
Special Case: defaultUrlsRedirect

One field, defaultUrlsRedirect, requires special handling, as it may sometimes be encoded in Base64. This check adds a condition to isValidPath() to allow for a base64 pattern:

const isBase64 = field === "defaultUrlsRedirect" && /^[A-Za-z0-9+/]+={0,2}$/.test(jsonData[field]);
pm.expect(isValidPath(jsonData[field]) || isBase64).to.be.true;

4. Nullable Path Fields

In cases where certain fields may be nullable (i.e., allowed to be null), I defined the array nullablePathFields to list fields that can hold null, an empty string, or a valid path/URL.

For each nullable path field, I used:

pm.test(`${field} should be null, match the path pattern, or be an empty string`, function () {
pm.expect(isValidPath(jsonData[field])).to.be.true;
});

This check allows the field to either be empty (""), null, or follow the predefined URL or path pattern.

5. Boolean Field Validation

For the boolean field enableRedirectToRegistration, I verified that the value strictly returns as either true or false:

pm.test("enableRedirectToRegistration should be a boolean", function () {
pm.expect(jsonData.enableRedirectToRegistration).to.be.a("boolean");
});

This ensures there’s no data-type mismatch, which could cause unexpected issues in the application.

6. Full URL Field Validation

For the checkoutPPLPath field, I enforced HTTPS as the required protocol. This field should contain a complete URL, starting with https://, ensuring secure communication:

pm.test("checkoutPPLPath should be a full URL starting with https", function () {
    pm.expect(jsonData.checkoutPPLPath).to.match(/^https:\/\/.+/);
});

This check guarantees that checkoutPPLPath returns a valid, absolute URL beginning with https, aligning with our security standards.

Scroll to Top