API Testing : Check timeout handling

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

Language: Java

Framework: Rest assured

copy
1import org.junit.Test;2import io.restassured.RestAssured;3import io.restassured.response.Response;4import static io.restassured.RestAssured.given;56public class APITimeoutTest {7 /​/​ Assuming the API endpoint is at http:/​/​localhost:8080/​api8 private String apiEndpoint = "http:/​/​localhost:8080/​api";9 10 @Test11 public void testTimeoutHandling() {12 /​/​ Set timeout of 5 seconds13 RestAssured.defaultConfig().getHttpClientConfig().setParam("http.connection.timeout", 5000);14 15 /​/​ Make API request with a delay of 10 seconds16 Response response = given()17 .when()18 .get(apiEndpoint + "/​delay/​10")19 .then()20 .extract().response();21 22 /​/​ Verify that the correct HTTP status code is returned23 assert response.statusCode() == 408;24 25 /​/​ Commented code to connect to remote client with desired capabilities26 /​*27 DesiredCapabilities desiredCapabilities = new DesiredCapabilities();28 desiredCapabilities.setCapability("browserName", "Chrome");29 desiredCapabilities.setCapability("version", "91.0");30 desiredCapabilities.setCapability("platform", Platform.WINDOWS);31 32 RemoteWebDriver remoteWebDriver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), desiredCapabilities);33 */​34 }35}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assuming the API endpoint is 'https:/​/​exampleapi.com/​timeout'45const chai = require('chai');6const chaiHttp = require('chai-http');7const expect = chai.expect;89chai.use(chaiHttp);1011describe('Testing timeout handling of API', function() {12 it('should return 408 Request Timeout if request takes more than 5 seconds', function(done) {13 this.timeout(10000); /​/​assuming that the timeout is set to 10 seconds14 chai.request('https:/​/​exampleapi.com')15 .get('/​timeout')16 .end(function(err, res) {17 expect(res).to.have.status(408); /​/​expecting HTTP status code 40818 done();19 });20 });21});2223/​/​Connecting to remote client with desired capabilities2425const webdriver = require('selenium-webdriver');26const { Builder } = require('selenium-webdriver');27const chrome = require('selenium-webdriver/​chrome');2829/​/​Assuming we want to connect to a remote client using Chrome browser with headless mode30/​/​Assuming the remote client has chromedriver version 94.0.4606.81 installed in the system and is available at 'http:/​/​<remote_ip_address>:9515'3132const options = new chrome.Options();33options.addArguments('headless');3435const capabilities = {36 browserName: 'chrome',37 chromeOptions: options,38 version: '94.0',39 chromedriverVersion: '94.0.4606.61'40}4142const remoteUrl = 'http:/​/​<remote_ip_address>:4444/​wd/​hub';4344const driver = new Builder()45 .withCapabilities(capabilities)46 .usingServer(remoteUrl)47 .build();4849/​/​Assuming the endpoint we want to test is 'https:/​/​example.com/​login'5051driver.get('https:/​/​example.com/​login')52 .then(() => {53 /​/​Write login test case here54 })55 .then(() => driver.quit()); /​/​quitting the driver after test case is completed

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