API Testing : Check CORS handling

Verify that the API correctly handles cross-origin resource sharing (CORS) and returns the correct HTTP status code and error message.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming API endpoint URL is https:/​/​example.com/​api23import static io.restassured.RestAssured.*;4import static org.hamcrest.Matchers.*;56public class CORSHandlingTest {78@Test9public void testCORSHandling() {1011/​/​Assuming "Access-Control-Allow-Origin" header is set to "*" for all allowed domains12given()13.header("Origin", "https:/​/​example2.com")14.when()15.options("https:/​/​example.com/​api")16.then()17.header("Access-Control-Allow-Origin", equalTo("https:/​/​example2.com"))18.header("Access-Control-Allow-Methods", equalTo("GET, POST, PUT, DELETE, OPTIONS"))19.header("Access-Control-Allow-Headers", equalTo("x-requested-with, Content-Type, Accept, Origin"))20.header("Access-Control-Max-Age", equalTo("3600"))21.statusCode(200);2223/​/​Assuming the API returns 401 error code for unauthorized requests24given()25.header("Origin", "https:/​/​example2.com")26.when()27.get("https:/​/​example.com/​api")28.then()29.statusCode(401)30.body("error.message", equalTo("Unauthorized"));3132}3334/​/​Assuming remote client can be connected using DesiredCapabilities3536/​* DesiredCapabilities capabilities = new DesiredCapabilities();37capabilities.setCapability("browserName", "Chrome");38capabilities.setCapability("version", "91.0");39capabilities.setCapability("platformName", "Windows 10");40capabilities.setCapability("seleniumVersion", "4.0.0-alpha-7");41RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities); */​4243}

Language: Javascript

copy
1/​/​ Mocha and Chai.23Code:45/​/​Assuming the API endpoint is "https:/​/​example.com/​api/​endpoint"67const axios = require('axios');8const assert = require('chai').assert;910describe('CORS handling for API endpoint', () => {11 it('should return correct HTTP status code and error message when accessing from other domains', async () => {12 /​/​Assuming the "Access-Control-Allow-Origin" header is set to "*" to allow all domains13 const response = await axios.get('https:/​/​example.com/​api/​endpoint', {headers: {Origin: 'https:/​/​otherdomain.com'}});14 assert.equal(response.status, 200, 'HTTP status code should be 200');15 /​/​Assuming the API returns an error message when accessed from other domains16 assert.equal(response.data.error, 'Cross-origin requests are not allowed', 'Error message should be "Cross-origin requests are not allowed"');17 });18 19 it('should allow cross-origin requests from specified domains', async () => {20 /​/​Assuming the "Access-Control-Allow-Origin" header is set to specific domains allowed21 const response = await axios.get('https:/​/​example.com/​api/​endpoint', {headers: {Origin: 'https:/​/​alloweddomain.com'}});22 assert.equal(response.status, 200, 'HTTP status code should be 200');23 assert.equal(response.data.result, 'Success', 'Response should indicate success');24 });25});2627/​/​To use local driver:28/​/​const WebDriver = require('selenium-webdriver');29/​/​const driver = new WebDriver.Builder().forBrowser('chrome').build();3031/​/​To connect to remote client with desired capabilities:32/​/​const WebDriver = require('selenium-webdriver');33/​/​const capabilities = {browserName: 'chrome'};34/​/​const driver = new WebDriver.Builder()35/​/​ .usingServer('http:/​/​localhost:4444/​wd/​hub')36/​/​ .withCapabilities(capabilities)37/​/​ .build();

Disclaimer: Following code snippets and related information have been sourced from GitHub and/or generated using AI code generation tools. LambdaTest takes no responsibility in the accuracy of the code and is not liable for any damages.

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Power Your Software Testing with AI and cloud

Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.

Start Free Testing