Verify that the API correctly handles high traffic and returns the correct HTTP status code and error message.
Language: Java
Framework: Rest assured
1//Assuming the following details about the API:2//1. API endpoint: https://myapi.com3//2. High traffic refers to 100 requests per second or more4//3. Correct HTTP status code for high traffic is 429 (Too Many Requests)5//4. Error message for high traffic is "API rate limit exceeded. Try again later."67import org.junit.Test;8import io.restassured.response.Response;9import static io.restassured.RestAssured.*;1011public class APITest {1213@Test14public void testHighTraffic() {15//Sending 100 requests per second16for(int i=0; i<100; i++) {17Response response = given()18.baseUri("https://myapi.com")19.when()20.get("/endpoint");21//Checking if the HTTP status code is 429 and error message is correct22response.then().statusCode(429)23.body("message", equalTo("API rate limit exceeded. Try again later."));24}25}2627//Code to connect to remote client with desired capabilities28/*29import io.restassured.RestAssured;30import io.restassured.remote.RemoteWebDriver;31import org.openqa.selenium.remote.DesiredCapabilities;3233public class RemoteTest {3435public static void main(String[] args) {36DesiredCapabilities capabilities = new DesiredCapabilities();37//Add desired capabilities here38RemoteWebDriver driver = new RemoteWebDriver(RestAssured.url, capabilities);39}40}41*/
Language: Javascript
1const axios = require('axios');23// Assuming the API endpoint to be tested is 'https://example.com/api'45// Test case to check high traffic6describe('API high traffic test', () => {7 8 // Assuming the number of requests to be sent for high traffic testing is 10009 const numRequests = 1000;1011 it(`Should handle ${numRequests} requests without errors`, async () => {1213 for (let i = 0; i < numRequests; i++) {14 await axios.get('https://example.com/api')15 .then((response) => {16 expect(response.status).toBe(200); // Assuming a successful response has HTTP status code 20017 expect(response.data).toBeDefined(); // Assuming data is returned in the response18 })19 .catch((error) => {20 // Assuming the API returns an error message in the response body for high traffic21 expect(error.response.statusText).toBe('Too Many Requests'); 22 expect(error.response.status).toBe(429); // Assuming HTTP status code 429 is returned for too many requests23 });24 }25 });2627});28// To connect to remote client with desired capabilities, add following code as comment:29/*30 const webdriver = require('selenium-webdriver');31 const capabilities = {32 browserName: 'chrome',33 'goog:chromeOptions': {34 args: ['--headless', '--disable-gpu']35 }36 };37 const driver = new webdriver.Builder()38 .usingServer('http://localhost:4444/wd/hub')39 .withCapabilities(capabilities)40 .build();41*/
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