CORS testing in Postman - Build an Automation Script
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.
Now add data-driven testing with 3 different origins: one allowed, one disallowed, and one missing Origin header
Practice
Solution
Step 1: Understand CORS concept
CORS (Cross-Origin Resource Sharing) controls if a server permits requests from different websites (origins).Step 2: Relate to Postman testing
In Postman, testing CORS means checking if the server allows resource sharing from other origins.Final Answer:
To verify if a server allows resource sharing from different origins -> Option AQuick Check:
CORS testing = verify cross-origin sharing [OK]
- Confusing CORS with API speed testing
- Thinking CORS checks server software version
- Assuming CORS validates request syntax
Solution
Step 1: Identify header for cross-origin requests
TheOriginheader tells the server which website is making the request.Step 2: Use in Postman
Setting theOriginheader in Postman simulates a request from that origin for CORS testing.Final Answer:
Origin -> Option DQuick Check:
Set Origin header to test CORS [OK]
- Using Authorization header instead of Origin
- Confusing Content-Type with Origin
- Not setting any header for CORS simulation
Origin: https://example.com. The server responds with header Access-Control-Allow-Origin: https://example.com. What does this response indicate?Solution
Step 1: Understand Access-Control-Allow-Origin header
This header specifies which origin the server allows for cross-origin requests.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.Final Answer:
The server allows requests only from https://example.com -> Option AQuick Check:
Matching Access-Control-Allow-Origin means allowed origin [OK]
- Thinking it denies the origin when it matches
- Assuming wildcard '*' when specific origin is set
- Confusing syntax error with valid header
Origin: https://myapp.com, but the server response lacks Access-Control-Allow-Origin header. What is the likely issue?Solution
Step 1: Check server response headers for CORS
IfAccess-Control-Allow-Originis missing, the server likely does not allow cross-origin requests.Step 2: Rule out Postman or request issues
Postman sends headers correctly; missing header usually means server misconfiguration or no CORS support.Final Answer:
The server does not support CORS or is misconfigured -> Option BQuick Check:
Missing Access-Control-Allow-Origin means no CORS support [OK]
- Blaming Postman for missing headers
- Assuming request method causes missing header
- Thinking server is down without checking status
Solution
Step 1: Understand testing multiple origins
To test multiple origins, you must simulate requests from each origin by setting theOriginheader accordingly.Step 2: Verify server response for each origin
Check ifAccess-Control-Allow-Originmatches the requested origin or allows all origins (wildcard).Final Answer:
Send multiple requests with different Origin headers and check Access-Control-Allow-Origin in each response -> Option CQuick Check:
Test each origin with Origin header and check response [OK]
- Testing without Origin header
- Assuming localhost tests all origins
- Checking unrelated headers or response time
Ready to prove it?
Pass the test and this pattern moves unknown
