Test Overview
This test checks if the server correctly allows or blocks cross-origin requests based on CORS policy. It verifies the presence and correctness of CORS headers in the response.
This test checks if the server correctly allows or blocks cross-origin requests based on CORS policy. It verifies the presence and correctness of CORS headers in the response.
pm.test("CORS headers are present and correct", function () { pm.response.to.have.header("access-control-allow-origin"); const originHeader = pm.response.headers.get("access-control-allow-origin"); pm.expect(originHeader).to.be.oneOf(["*", "https://allowed-origin.com"]); pm.response.to.have.header("access-control-allow-methods"); const methodsHeader = pm.response.headers.get("access-control-allow-methods"); pm.expect(methodsHeader).to.include("GET"); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Send HTTP OPTIONS request with Origin header 'https://allowed-origin.com' to the API endpoint | Postman sends preflight CORS request to server | — | PASS |
| 2 | Receive HTTP response with CORS headers from server | Response includes headers like 'access-control-allow-origin' and 'access-control-allow-methods' | — | PASS |
| 3 | Check if 'access-control-allow-origin' header is present and equals '*' or 'https://allowed-origin.com' | Header value is 'https://allowed-origin.com' | pm.expect(originHeader).to.be.oneOf(['*', 'https://allowed-origin.com']) | PASS |
| 4 | Check if 'access-control-allow-methods' header is present and includes 'GET' | Header value is 'GET, POST, OPTIONS' | pm.expect(methodsHeader).to.include('GET') | PASS |
Origin header tells the server which website is making the request.Origin header in Postman simulates a request from that origin for CORS testing.Origin: https://example.com. The server responds with header Access-Control-Allow-Origin: https://example.com. What does this response indicate?Origin: https://myapp.com, but the server response lacks Access-Control-Allow-Origin header. What is the likely issue?Access-Control-Allow-Origin is missing, the server likely does not allow cross-origin requests.Origin header accordingly.Access-Control-Allow-Origin matches the requested origin or allows all origins (wildcard).Pass the test and this pattern moves unknown