API Testing : Check server clustering

Verify that the API correctly handles server clustering and returns the correct HTTP status code and error message.

Language: Java

Framework: Rest assured

copy
1import org.testng.annotations.Test;2import static io.restassured.RestAssured.given;3import static org.hamcrest.Matchers.equalTo;45public class APITest {6 7 @Test8 public void testClusterAPI() {9 /​/​Assuming API endpoint to test10 String endpoint = "https:/​/​example.com/​api/​clustering";11 12 /​/​Assuming cluster server configuration13 int numOfServerNodes = 3;14 String[] serverNodes = new String[numOfServerNodes];15 serverNodes[0] = "10.0.0.1";16 serverNodes[1] = "10.0.0.2";17 serverNodes[2] = "10.0.0.3";18 19 /​/​Connecting to local driver20 RestAssured.baseURI = endpoint;21 22 given().get().then().statusCode(200).body("message", equalTo("Cluster is up and running"));23 24 /​/​Code for connecting to remote client with desired capabilities25 /​*26 DesiredCapabilities capabilities = new DesiredCapabilities();27 capabilities.setBrowserName("chrome");28 WebDriver driver = new RemoteWebDriver(new URL("http:/​/​10.0.0.1:4444/​wd/​hub"), capabilities);29 RestAssured.defaultParser = Parser.JSON;30 RestAssured.baseURI = endpoint;31 given().get().then().statusCode(200).body("message", equalTo("Cluster is up and running"));32 */​33 34 }35}

Language: Javascript

copy
1/​/​ Mocha + Chai23/​/​Assuming the API end points are available and there are two servers in the server cluster45const chai = require('chai');6const chaiHttp = require('chai-http');7const expect = chai.expect;89chai.use(chaiHttp);1011describe('API server clustering testing', () => {12 it('should return correct HTTP status code and error message when the server is down', async () => {13 /​/​Assuming server1 is down14 const res = await chai.request('http:/​/​localhost:3000/​api')15 .get('/​users')16 .send();17 18 expect(res).to.have.status(500);19 expect(res).to.have.property('body').to.deep.equal({error: 'Internal Server Error'});20 21 /​/​Connect to remote client with desired capabilities22 /​*const capabilities = {23 browserName: 'chrome',24 platform: 'Windows 10',25 version: 'latest'26 };27 28 const driver = new webdriver.Builder()29 .usingServer('http:/​/​localhost:4444/​wd/​hub')30 .withCapabilities(capabilities)31 .build();*/​32 33 });34});

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