top of page

Enforcing Region Rules Before Sending API Requests

  • Writer: Elisha Antunes
    Elisha Antunes
  • Jun 7
  • 2 min read

Updated: Jun 23

When testing APIs that involve addresses, it’s easy to focus only on fields like street or zipCode—but state-level rules matter too. Some states may be unsupported, restricted, or require underwriting.


In my API testing setup, I wanted to ensure only valid U.S. states were used in test data—especially for policies limited to domestic clients. Here’s how I enforce controlled randomness in Postman, using a clean whitelist approach and dynamic state selection.


Goal

Ensure that any test-generated address always uses a valid U.S. state (and potentially simulate region-based rules).


Building the Script


1. Define an array of accepted U.S. states

const insuredStates = [
  'AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'FL', 'GA',
  'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD',
  'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ',
  'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC',
  'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY', 'DC'
];
pm.collectionVariables.set('insuredStates', insuredStates);


2. Randomly pick one of the states

function generateRandomState() {
  let randomInsuredState = Math.floor(Math.random() * insuredStates.length);
  return insuredStates[randomInsuredState];
}

3. Use it in your address object

const clientAddress = {
  street: generateRandomStreetAddress(),
  city: generateRandomCity(),
  state: generateRandomState(),
  zipCode: generateRandomZipCode(),
  country: "US"
};
pm.collectionVariables.set("clientAddress", JSON.stringify(clientAddress));

Why This Matters

  • Test accuracy: Ensures you're only testing quote logic for regions that are eligible.

  • Prevents false positives: Invalid states could trigger eligibility failures, muddying test results.

  • Real-world reflection: Many systems restrict coverages by geography—your tests should too.


Tip: Vary State Logic by Role

Want to simulate restricted regions for certain scenarios (e.g., a specific state requires extra validation)? You can inject conditional logic in form of a post-request script like this:

if (state === "NY") {
  pm.expect.fail("New York requires manual review for freelance policies.");
}

Takeaways

Controlled randomness is powerful. By using a valid state list, you:

  • Keep your test data clean

  • Reflect business rules early in the flow

  • Prevent downstream noise caused by bad inputs

Recent Posts

See All
Randomized Coverage Selections

Coverage selections during quoting are typically driven by risk, budget, and industry standards—not by randomness. But as testers, we...

 
 
Link Matching

When testing APIs that model real-world relationships, it’s not enough to check individual fields — you often need to validate how...

 
 
bottom of page