API Testing : Check load balancing

Verify that the API correctly handles load balancing and returns the correct HTTP status code and error message.

Language: Java

Framework: Rest assured

copy
1import static io.restassured.RestAssured.*;2import static org.hamcrest.Matchers.*;34public class ApiTest {56 public static void main(String[] args) {7 8 /​/​ Assumption: The API is hosted on a remote server.9 /​/​ Uncomment the following code to use a remote client with desired capabilities.10 /​/​DesiredCapabilities capabilities = new DesiredCapabilities();11 /​/​capabilities.setBrowserName("chrome");12 /​/​capabilities.setPlatform(Platform.LINUX);13 /​/​RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​remoteurl:port/​wd/​hub"), capabilities);14 /​/​driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);15 16 /​/​ Assumption: The load balancing is managed by a third party service.17 /​/​ We will use a mock server to simulate this service.18 /​/​ We will use the following URL to access the mock server.19 String mockServerUrl = "http:/​/​localhost:8080";20 21 /​/​ Set up the base URL for the API.22 baseURI = "http:/​/​api.example.com";23 24 /​/​ Define the test case.25 given().26 when().27 get(mockServerUrl+"/​load-balancing").28 then().29 assertThat().30 statusCode(200).31 body("message", equalTo("Load balancing successful."));3233 /​/​ Uncomment the following code to close the remote client driver.34 /​/​driver.quit();35 }3637}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assumptions:4/​/​1. The API is built to handle load balancing.5/​/​2. The load balancer distributes incoming traffic evenly across a group of backend servers.6/​/​3. The API returns an HTTP status code and an error message when there is an error.78const chai = require('chai');9const expect = chai.expect;10const request = require('request');1112describe('API Load Balancing Test', function () {13 it('should handle load balancing and return the correct HTTP status code and error message', function (done) {14 /​/​Assuming that there are multiple backend servers to distribute traffic.15 const server1 = 'http:/​/​server1/​api';16 const server2 = 'http:/​/​server2/​api';17 const server3 = 'http:/​/​server3/​api';18 19 /​/​Assuming that load balancer URL is https:/​/​load-balancer.com20 const loadBalancerURL = 'https:/​/​load-balancer.com';21 22 /​/​Assuming that the API endpoint is 'verifyLoadBalancing'23 const apiEndpoint = '/​verifyLoadBalancing';24 25 const options = {26 url: loadBalancerURL + apiEndpoint,27 method: 'GET'28 };29 30 /​/​Assuming that the expected HTTP status code is 200 and the expected error message is 'Load balancing successful.'31 const expectedResult = {32 status: 200,33 message: 'Load balancing successful.'34 };35 36 request(options, function (error, response, body) {37 if (error) {38 done(error);39 } else {40 const result = {41 status: response.statusCode,42 message: JSON.parse(body).message43 };44 expect(result).to.deep.equal(expectedResult);45 done();46 }47 });48 });49 50 /​/​Assuming that if the load balancing is not handled properly, the API should return an error message stating 'Load balancing failed.'51 it('should return an error message if load balancing fails', function (done) {52 /​/​assuming that the server is down53 const server1 = 'http:/​/​server1/​api';54 55 /​/​Assuming that load balancer URL is https:/​/​load-balancer.com56 const loadBalancerURL = 'https:/​/​load-balancer.com';57 58 /​/​Assuming that the API endpoint is 'verifyLoadBalancing'59 const apiEndpoint = '/​verifyLoadBalancing';60 61 const options = {62 url: loadBalancerURL + apiEndpoint,63 method: 'GET'64 };65 66 /​/​Assuming that the expected HTTP status code is 500 and the expected error message is 'Load balancing failed.'67 const expectedResult = {68 status: 500,69 message: 'Load balancing failed.'70 };71 72 request(options, function (error, response, body) {73 if (error) {74 done(error);75 } else {76 const result = {77 status: response.statusCode,78 message: JSON.parse(body).message79 };80 expect(result).to.deep.equal(expectedResult);81 done();82 }83 });84 });85});8687/​/​Assuming the following code to connect to remote client with desired capabilities:8889const { Builder, Capabilities } = require('selenium-webdriver');90const { Options } = require('selenium-webdriver/​chrome');9192const chromeCapabilities = Capabilities.chrome();93chromeCapabilities.set('chromeOptions', {94 'args': ['--disable-extensions', '--disable-infobars']95});9697const options = new Options();98options.setAcceptInsecureCerts(true);99options.headless();100101const driver = new Builder()102 .usingServer('http:/​/​localhost:4444/​wd/​hub')103 .withCapabilities(chromeCapabilities)104 .setChromeOptions(options)105 .build()106 .then(() => {107 console.log('Webdriver connected to remote client');108 })109 .catch((err) => {110 console.error('Error connecting to remote client:', err.message);111 });

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