Bird
Raised Fist0
Postmantesting~15 mins

CORS testing in Postman - Build an Automation Script

Choose your learning style10 modes available
Verify CORS policy allows requests from allowed origins
Preconditions (2)
Step 1: Open Postman and create a new GET request to the API endpoint https://api.example.com/data
Step 2: Add a custom header 'Origin' with value 'https://allowed-origin.com'
Step 3: Send the request
Step 4: Observe the response headers
Step 5: Repeat the request with 'Origin' header set to 'https://disallowed-origin.com'
✅ Expected Result: For the allowed origin, the response includes header 'Access-Control-Allow-Origin' with value 'https://allowed-origin.com' and status code 200. For the disallowed origin, the response does not include 'Access-Control-Allow-Origin' header or returns a CORS error status.
Automation Requirements - Postman Tests
Assertions Needed:
Verify response status code is 200 for allowed origin
Verify 'Access-Control-Allow-Origin' header equals the allowed origin
Verify response status code is 403 or no 'Access-Control-Allow-Origin' header for disallowed origin
Best Practices:
Use environment variables for origin URLs
Write clear and descriptive test scripts in Postman Tests tab
Separate tests for allowed and disallowed origins
Use pre-request scripts to set headers dynamically
Automated Solution
Postman
pm.test('Allowed origin returns 200 status', function () {
    pm.response.to.have.status(200);
});

pm.test('Allowed origin is present in Access-Control-Allow-Origin header', function () {
    const allowedOrigin = pm.environment.get('allowedOrigin');
    pm.expect(pm.response.headers.get('Access-Control-Allow-Origin')).to.eql(allowedOrigin);
});

pm.test('Disallowed origin returns no Access-Control-Allow-Origin header or error', function () {
    const disallowedOrigin = pm.environment.get('disallowedOrigin');
    if (pm.request.headers.get('Origin') === disallowedOrigin) {
        pm.expect(pm.response.headers.has('Access-Control-Allow-Origin')).to.be.false;
        pm.expect(pm.response.code).to.be.oneOf([403, 401, 0]);
    }
}
);

This Postman test script checks the CORS behavior for allowed and disallowed origins.

First, it verifies the response status is 200 when the origin is allowed.

Then, it asserts the Access-Control-Allow-Origin header matches the allowed origin value stored in environment variables.

For the disallowed origin, it confirms the header is missing and the status code indicates an error or forbidden access.

Using environment variables makes the test flexible and reusable.

Tests are written clearly in the Postman Tests tab to provide immediate feedback after each request.

Common Mistakes - 3 Pitfalls
Hardcoding origin URLs inside test scripts
{'mistake': "Not setting the 'Origin' header in the request", 'why_bad': "CORS depends on the Origin header; without it, the server won't apply CORS rules properly.", 'correct_approach': "Always add the 'Origin' header in the request to simulate browser behavior."}
{'mistake': 'Checking only status code without verifying CORS headers', 'why_bad': 'Status code alone does not confirm if CORS policy is correctly enforced.', 'correct_approach': "Assert presence and correctness of 'Access-Control-Allow-Origin' header in response."}
Bonus Challenge

Now add data-driven testing with 3 different origins: one allowed, one disallowed, and one missing Origin header

Show Hint

Practice

(1/5)
1. What is the main purpose of CORS testing in Postman?
easy
A. To verify if a server allows resource sharing from different origins
B. To check if the server is running the latest software version
C. To test the speed of the API response
D. To validate the syntax of the API request

Solution

  1. Step 1: Understand CORS concept

    CORS (Cross-Origin Resource Sharing) controls if a server permits requests from different websites (origins).
  2. Step 2: Relate to Postman testing

    In Postman, testing CORS means checking if the server allows resource sharing from other origins.
  3. Final Answer:

    To verify if a server allows resource sharing from different origins -> Option A
  4. Quick Check:

    CORS testing = verify cross-origin sharing [OK]
Hint: CORS is about sharing resources across websites [OK]
Common Mistakes:
  • Confusing CORS with API speed testing
  • Thinking CORS checks server software version
  • Assuming CORS validates request syntax
2. Which header should you set in Postman to simulate a cross-origin request for CORS testing?
easy
A. Accept-Encoding
B. Authorization
C. Content-Type
D. Origin

Solution

  1. Step 1: Identify header for cross-origin requests

    The Origin header tells the server which website is making the request.
  2. Step 2: Use in Postman

    Setting the Origin header in Postman simulates a request from that origin for CORS testing.
  3. Final Answer:

    Origin -> Option D
  4. Quick Check:

    Set Origin header to test CORS [OK]
Hint: Set 'Origin' header to mimic browser cross-site requests [OK]
Common Mistakes:
  • Using Authorization header instead of Origin
  • Confusing Content-Type with Origin
  • Not setting any header for CORS simulation
3. You send a request in Postman with header Origin: https://example.com. The server responds with header Access-Control-Allow-Origin: https://example.com. What does this response indicate?
medium
A. The server allows requests only from https://example.com
B. The server has a syntax error in headers
C. The server allows requests from any origin
D. The server denies requests from https://example.com

Solution

  1. Step 1: Understand Access-Control-Allow-Origin header

    This header specifies which origin the server allows for cross-origin requests.
  2. Step 2: Match response header with request origin

    The server's response matches the request origin exactly, meaning it allows requests only from that origin.
  3. Final Answer:

    The server allows requests only from https://example.com -> Option A
  4. Quick Check:

    Matching Access-Control-Allow-Origin means allowed origin [OK]
Hint: Matching Access-Control-Allow-Origin means allowed origin [OK]
Common Mistakes:
  • Thinking it denies the origin when it matches
  • Assuming wildcard '*' when specific origin is set
  • Confusing syntax error with valid header
4. You test CORS in Postman by setting Origin: https://myapp.com, but the server response lacks Access-Control-Allow-Origin header. What is the likely issue?
medium
A. The request method is invalid
B. The server does not support CORS or is misconfigured
C. Postman does not send the Origin header correctly
D. The server is down

Solution

  1. Step 1: Check server response headers for CORS

    If Access-Control-Allow-Origin is missing, the server likely does not allow cross-origin requests.
  2. Step 2: Rule out Postman or request issues

    Postman sends headers correctly; missing header usually means server misconfiguration or no CORS support.
  3. Final Answer:

    The server does not support CORS or is misconfigured -> Option B
  4. Quick Check:

    Missing Access-Control-Allow-Origin means no CORS support [OK]
Hint: Missing Access-Control-Allow-Origin means server blocks CORS [OK]
Common Mistakes:
  • Blaming Postman for missing headers
  • Assuming request method causes missing header
  • Thinking server is down without checking status
5. You want to test if a server allows CORS requests from multiple origins using Postman. Which approach is best to verify this?
hard
A. Send requests with random headers and check response time
B. Send one request without Origin header and check server status
C. Send multiple requests with different Origin headers and check Access-Control-Allow-Origin in each response
D. Send requests only from localhost and assume all origins are allowed

Solution

  1. Step 1: Understand testing multiple origins

    To test multiple origins, you must simulate requests from each origin by setting the Origin header accordingly.
  2. Step 2: Verify server response for each origin

    Check if Access-Control-Allow-Origin matches the requested origin or allows all origins (wildcard).
  3. Final Answer:

    Send multiple requests with different Origin headers and check Access-Control-Allow-Origin in each response -> Option C
  4. Quick Check:

    Test each origin with Origin header and check response [OK]
Hint: Test each origin separately by changing Origin header [OK]
Common Mistakes:
  • Testing without Origin header
  • Assuming localhost tests all origins
  • Checking unrelated headers or response time
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard