Verify that the API correctly handles slow requests and returns the correct HTTP status code and error message.
Language: Java
Framework: Rest assured
1//Assuming the API endpoint is https://example-api.com and slow requests are those that take more than 5 seconds to respond23import org.junit.Test;4import static io.restassured.RestAssured.*;5import static org.hamcrest.Matchers.*;67public class SlowRequestsTest {89 @Test10 public void testSlowRequests() {11 given().12 get("https://example-api.com/slowRequest").13 then().14 assertThat().15 statusCode(408).16 body("error", equalTo("Request Timeout"));17 }18 19 //Connecting to remote client with desired capabilities20 /*DesiredCapabilities caps = new DesiredCapabilities();21 caps.setCapability("version", "latest");22 caps.setCapability("browserName", "Chrome");23 caps.setCapability("platform", "Windows 10");24 25 RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), caps);*/2627 //Assuming that HTTP error code 408 is returned when the slow request times out and the error message is "Request Timeout"28 //Assuming that Rest Assured is already added as a dependency in the project29}
Language: Javascript
1// Mocha and Chai.23//Assuming an API endpoint with slow requests that should take more than 5 seconds to complete 45describe('API Testing with Mocha and Chai', function() {67 it('should handle slow requests', function(done) {8 9 this.timeout(10000); // increase timeout to 10 seconds as slow requests take long10 11 const endpointURL = 'https://example.com/slow-request';1213 //Assuming the API endpoint requires authentication14 //Assuming authentication token is included in a header named 'Authorization'15 const authToken = 'sample-auth-token'; 1617 //Assuming the API endpoint returns a 504 error and 'Gateway Timeout' message18 const expectedErrorCode = 504;19 const expectedErrorMessage = 'Gateway Timeout'; 2021 //Assuming http request library is used to make the API call22 //Assuming the endpoint returns a Promise object that resolves with the response object23 const makeAPICall = require('http');24 25 //Assuming optional desired capabilities object26 const desiredCapabilities = {};2728 //Assuming the remote client is remoteAddress.mywebsite.com and port number is 444429 //Uncomment below code and update remoteAddress and capabilities if needed30 //const {Builder} = require('selenium-webdriver');31 //const {Options} = require('selenium-webdriver/chrome');32 //const capabilities = Options.chrome().setAcceptInsecureCerts(true);33 //const remoteAddress = 'http://remoteAddress.mywebsite.com:4444/wd/hub';34 //const driver = new Builder().withCapabilities(capabilities)35 // .usingServer(remoteAddress)36 // .build();3738 //Assuming local driver is used to make the API call39 //Assuming the driver is a local instance of webdriver (already installed webdriver)40 const webdriver = require('selenium-webdriver');41 const driver = new webdriver.Builder().forBrowser('chrome').build(); //using chrome driver4243 driver.manage().setTimeouts( { implicit: 5000 } ); //Assuming maximum wait is 5 seconds4445 driver.get(endpointURL + '?auth_token=' + authToken)46 .then(() => {47 return Promise.all([driver.getTitle(), driver.wait(webdriver.until.elementLocated(webdriver.By.tagName('body')), 5000)]);48 })49 .then(([title,body]) => {50 return Promise.all([title,body.getText()]);51 })52 .then(([title,body]) => {53 const actualErrorCode = driver.wait(webdriver.until.elementLocated(webdriver.By.css('span.error-code'))).getText();54 assert.equal(actualErrorCode, expectedErrorCode);55 const actualErrorMessage = driver.wait(webdriver.until.elementLocated(webdriver.By.css('span.error-message'))).getText();56 assert.equal(actualErrorMessage, expectedErrorMessage);57 done();58 })59 .catch((error) => {60 done(error);61 })62 .finally(() => {63 driver.quit(); //quit driver after test finishes64 });6566 });67});
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.
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.
Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.
Start Free Testing