API Testing : Check failover

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

Language: Java

Framework: Rest assured

copy
1import org.junit.Test;2import static io.restassured.RestAssured.*;3import static org.hamcrest.Matchers.*;4import io.restassured.http.ContentType;56public class TestAPIFailover {78 @Test9 public void testFailover() {10 /​/​ Assuming we have the following endpoints available - [primaryEndPoint, secondaryEndPoint]11 String primaryEndPoint = "http:/​/​primaryapi.com";12 String secondaryEndPoint = "http:/​/​secondaryapi.com";13 14 /​/​ Connecting to a remote client with desired capabilities if needed15 /​/​ RemoteWebDriver remoteDriver = new RemoteWebDriver(new URL("http:/​/​192.168.0.1:5555/​wd/​hub"), new DesiredCapabilities());16 /​/​ WebDriver driver = ChromeDriver();17 18 /​/​ Use local driver19 WebDriver driver = new ChromeDriver();20 21 /​/​ Navigate to the API endpoint22 driver.get(primaryEndPoint);23 24 /​/​ Verify that API is up25 given().contentType(ContentType.JSON)26 .when().get(primaryEndPoint)27 .then().assertThat().statusCode(200);28 29 /​/​ Simulating the failure of the primary endpoint30 /​/​ Can be done with shutdown or disconnect or making the endpoint unavailable31 driver.get(secondaryEndPoint);32 33 /​/​ Verify if secondaryEndpoint has correct HTTP status code and error message34 given().contentType(ContentType.JSON)35 .when().get(secondaryEndPoint)36 .then().assertThat().statusCode(500)37 .body("error.message", equalTo("Internal server error occured, Failover successful"));38 39 /​/​ Close the driver40 driver.quit();4142 }43}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assumptions:4/​/​1. We have the API endpoint URL and a backup URL as well5/​/​2. The API endpoint is prone to failover due to network instability or server downtime6/​/​3. The expected HTTP status code for success is 200 and for failover, it's 500.7/​/​4. The expected error message in case of failover is "Service Unavailable."89const chai = require('chai');10const chaiHttp = require('chai-http');11const expect = chai.expect;1213chai.use(chaiHttp);1415describe('API Testing: Failover Check', function() {16 it('should return HTTP status code 200 on normal API endpoint', function(done) {17 chai.request('<API endpoint URL>')18 .get('/​')19 .end(function(err, res) {20 expect(res).to.have.status(200);21 done();22 });23 });2425 it('should return HTTP status code 500 on failing over to backup API endpoint', function(done) {26 /​/​Connecting to remote client with desired capabilities 27 let desiredCaps = {28 'browserName': 'Chrome',29 'platform': 'Windows',30 'version': '91',31 'name': 'Testing Failover',32 'build': 'build-' + Date.now()33 };34 35 let ltRemoteUrl = 'https:/​/​username:accesskey@hub.lambdatest.com/​wd/​hub';3637 const driver = new webdriver.Builder()38 .usingServer(ltRemoteUrl)39 .withCapabilities(desiredCaps)40 .build();4142 driver.get('<API backup endpoint URL>').then(function() {43 chai.request('<API backup endpoint URL>')44 .get('/​')45 .end(function(err, res) {46 expect(res).to.have.status(500);47 expect(res.body.error.message).to.equal('Service Unavailable.');48 driver.quit();49 done();50 });51 });52 });53});

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