API Testing : Check network failure handling

Verify that the API correctly handles network failures and returns the correct HTTP status code.

Language: Java

Framework: Rest assured

copy
1/​/​ Assuming the API endpoint is already developed and hosted, and I have access to test it locally, the code to test the network failure handling in Rest Assured with Java can be as follows:234import io.restassured.RestAssured;5import io.restassured.response.Response;6import org.hamcrest.Matchers;7import org.testng.Assert;8import org.testng.annotations.Test;910public class APITest {11 @Test12 public void testNetworkFailureHandling() {13 /​/​ Set the API endpoint URL14 String endpointUrl = "http:/​/​localhost:8080/​api/​some/​endpoint";15 16 /​/​ Add desired capabilities for remote client connection as commented code17 /​*18 DesiredCapabilities capabilities = new DesiredCapabilities();19 capabilities.setBrowserName("chrome");20 capabilities.setVersion("87.0");21 capabilities.setPlatform(Platform.LINUX);22 WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);23 */​24 25 /​/​ Scenario: network is down26 /​/​ Assumption: the API endpoint is not responsive27 RestAssured.given()28 .baseUri(endpointUrl)29 .when()30 .get()31 .then()32 .statusCode(Matchers.anyOf(Matchers.is(500), Matchers.is(502), Matchers.is(503)))33 .and()34 .body("error", Matchers.equalTo("Network failure"));35 36 /​/​ Scenario: network is up but endpoint is down37 /​/​ Assumption: the API endpoint is not running38 /​/​ Uncomment below code to test39 /​*Response response = RestAssured.given()40 .baseUri(endpointUrl)41 .when()42 .get();43 Assert.assertEquals(404, response.statusCode());44 Assert.assertEquals("API endpoint not found", response.getBody().asString());*/​45 }46}4748/​*49The assumptions made in the code are:50- The API endpoint is hosted locally and can be accessed through `http:/​/​localhost:8080/​api/​some/​endpoint`.51- There are two scenarios to test: network is down and endpoint is down.52- For the network failure scenario, the API should return one of the HTTP status codes: 500, 502, or 503 and the response body should contain the error message "Network failure".53- For the endpoint not found scenario, the API should return HTTP status code 404 and the response body should contain the error message "API endpoint not found".5455If remote client connection is required, the desired capabilities and remote web driver connection code should be added as comments, as shown in the code above.56*/​

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assumptions: 4/​/​1. The API URL is available and can be accessed.5/​/​2. We will simulate network failure by turning off Wi-Fi/​Internet.6/​/​3. The API URL returns HTTP status code 500 on a network failure.78const { expect } = require('chai');9const fetch = require('node-fetch');1011describe('API network failure handling test', () => {12 it('should return HTTP status 500 on network failure', async () => {13 14 /​/​Simulate network failure15 /​/​Turn off Wi-Fi/​Internet16 17 /​/​Replace <API_URL> with the actual API URL18 const response = await fetch('<API_URL>');19 20 /​/​Check HTTP status code21 expect(response.status).to.equal(500);22 23 /​/​Reconnect Wi-Fi/​Internet24 25 /​/​Uncomment the below code and replace <Remote_Capabilities> with the desired capabilities26 /​*27 const webdriver = require('selenium-webdriver');28 const capabilities = <Remote_Capabilities>;29 const driver = new webdriver.Builder()30 .usingServer('<Remote_Server_Address>')31 .withCapabilities(capabilities)32 .build();33 driver.get('<API_URL>');34 const status = await driver.executeScript('return document.readyState');35 expect(status).to.equal('complete');36 driver.quit();37 */​38 });39});4041/​/​Note: The above code assumes that the fetch() function correctly handles network failures and returns an HTTP status code. If the fetch() function throws an error instead, the test case could be modified to handle the error and check the correct error message.

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