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
Result | Field | Condition |
---|---|---|
PASS | basePathImage | should not be empty |
PASS | booking | should not be empty |
PASS | resetPasswordPagePath | should not be empty |
PASS | homePage | should not be empty |
PASS | storeLocator | should not be empty |
PASS | confirmProfilePath | should not be empty |
PASS | basePathProductLendingImage | should not be empty |
PASS | faqPagePath | should not be empty |
PASS | login | should not be empty |
PASS | pathSuccesGetAllMyInfo | should not be empty |
PASS | checkout | should not be empty |
PASS | carePlusPagePath | should not be empty |
PASS | referFriendPagePath | should not be empty |
PASS | myAccount | should not be empty |
PASS | register | should not be empty |
PASS | basePathImage | should match the path pattern or be a URL |
PASS | booking | should match the path pattern or be a URL |
PASS | resetPasswordPagePath | should match the path pattern or be a URL |
PASS | homePage | should match the path pattern or be a URL |
PASS | storeLocator | should match the path pattern or be a URL |
PASS | confirmProfilePath | should match the path pattern or be a URL |
PASS | basePathProductLendingImage | should match the path pattern or be a URL |
PASS | defaultUrlsRedirect | should match the path pattern or be a URL |
PASS | faqPagePath | should match the path pattern or be a URL |
PASS | appointmentListPagePath | should match the path pattern or be a URL |
PASS | orderReturn | should match the path pattern or be a URL |
PASS | qureReplacementInfoPath | should match the path pattern or be a URL |
PASS | pageEligibilityCheck | should match the path pattern or be a URL |
PASS | forgottenPassword | should match the path pattern or be a URL |
PASS | checkHavStatusPage | should match the path pattern or be a URL |
PASS | redirectAfterDeleteLink | should match the path pattern or be a URL |
PASS | login | should match the path pattern or be a URL |
PASS | pathSuccesGetAllMyInfo | should match the path pattern or be a URL |
PASS | waitePagePath | should match the path pattern or be a URL |
PASS | checkout | should match the path pattern or be a URL |
PASS | carePlusPagePath | should match the path pattern or be a URL |
PASS | referFriendPagePath | should match the path pattern or be a URL |
PASS | myAccountOnBehalf | should match the path pattern or be a URL |
PASS | productDetail | should match the path pattern or be a URL |
PASS | shoppingBag | should match the path pattern or be a URL |
PASS | checkoutPPLPath | should match the path pattern or be a URL |
PASS | barcodeScannerPage | should match the path pattern or be a URL |
PASS | troubleshootDevice | should match the path pattern or be a URL |
PASS | preferenceCenterPagePath | should match the path pattern or be a URL |
PASS | checkNotHavStatusPage | should match the path pattern or be a URL |
PASS | myAccount | should match the path pattern or be a URL |
PASS | register | should match the path pattern or be a URL |
PASS | additionalContactDetailPath | should be null, match the path pattern, or be an empty string |
PASS | secondRegisterPagePath | should be null, match the path pattern, or be an empty string |
PASS | pathCheckSession | should be null, match the path pattern, or be an empty string |
PASS | manualDeviceActivationPath | should be null, match the path pattern, or be an empty string |
PASS | notificationAppointment | should be null, match the path pattern, or be an empty string |
PASS | searchPmiCeAppPage | should be null, match the path pattern, or be an empty string |
PASS | pathCheckSSO | should be null, match the path pattern, or be an empty string |
PASS | pathRewriteSSO | should be null, match the path pattern, or be an empty string |
PASS | welcomePathFromCheckout | should be null, match the path pattern, or be an empty string |
PASS | enableRedirectToRegistration | should be a boolean |
PASS | checkoutPPLPath | should 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:
- Non-empty fields: Required fields that cannot be empty.
- URL/path pattern matching fields: Fields that need to follow a URL or path format.
- Nullable path fields: Fields that can either be null, an empty string, or match a specific path format.
- Boolean fields: Fields that must strictly hold boolean values.
- 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.