Verify that the API returns the correct response time for different types of requests (e.g. GET, POST, PUT, DELETE).
Language: Java
Framework: Rest assured
1import org.junit.Test;2import io.restassured.RestAssured;3import io.restassured.response.Response;45public class APITest {67 @Test8 public void testResponseTime() {9 //Assuming API endpoint is http://example.com/api10 RestAssured.baseURI = "http://example.com/api/";11 12 //Assuming GET request is made to endpoint /users13 Response userResponse = RestAssured.given().when().get("/users");14 userResponse.then().assertThat().statusCode(200);15 System.out.println("Get request response time: " + userResponse.getTime());1617 //Assuming POST request is made to endpoint /createUser18 Response createUserResponse = RestAssured.given().when().post("/createUser");19 createUserResponse.then().assertThat().statusCode(201);20 System.out.println("Post request response time: " + createUserResponse.getTime());2122 //Assuming PUT request is made to endpoint /updateUser23 Response updateUserResponse = RestAssured.given().when().put("/updateUser");24 updateUserResponse.then().assertThat().statusCode(200);25 System.out.println("Put request response time: " + updateUserResponse.getTime());2627 //Assuming DELETE request is made to endpoint /deleteUser28 Response deleteUserResponse = RestAssured.given().when().delete("/deleteUser");29 deleteUserResponse.then().assertThat().statusCode(204);30 System.out.println("Delete request response time: " + deleteUserResponse.getTime());31 32 //Assuming to connect to remote client with desired capabilities33 //DesiredCapabilities capabilities = new DesiredCapabilities();34 //capabilities.setCapability("browserName", "Chrome");35 //RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);36 }37}
Language: Javascript
1//Mocha + Chai23const { expect } = require('chai');4const axios = require('axios'); //assuming axios is already installed56describe('API Request Response Time Check', () => {7 // set the endpoint URL8 const endpoint = 'https://my-api.com/';910 describe('GET request response time', () => {11 it('should return response time within 2 seconds', async () => {12 const start = new Date();13 const response = await axios.get(endpoint);14 const responseTime = new Date() - start;15 expect(responseTime).to.be.lessThan(2000);16 });17 });1819 describe('POST request response time', () => {20 it('should return response time within 3 seconds', async () => {21 const start = new Date();22 const response = await axios.post(endpoint, { data: {} });23 const responseTime = new Date() - start;24 expect(responseTime).to.be.lessThan(3000);25 });26 });2728 describe('PUT request response time', () => {29 it('should return response time within 4 seconds', async () => {30 const start = new Date();31 const response = await axios.put(endpoint, { data: {} });32 const responseTime = new Date() - start;33 expect(responseTime).to.be.lessThan(4000);34 });35 });3637 describe('DELETE request response time', () => {38 it('should return response time within 5 seconds', async () => {39 const start = new Date();40 const response = await axios.delete(endpoint);41 const responseTime = new Date() - start;42 expect(responseTime).to.be.lessThan(5000);43 });44 });4546});
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