Verify that the API correctly handles retries and returns the correct HTTP status code.
Language: Java
Framework: Rest assured
1import org.junit.jupiter.api.Test;2import static io.restassured.RestAssured.*;3import static io.restassured.matcher.RestAssuredMatchers.*;4import static org.hamcrest.Matchers.*;56public class ApiTest {78 @Test9 public void testRetryHandling() {10 //Assuming API endpoint to be https://example.com/api/retry11 //Assuming maximum retry count of 312 13 given().when().get("https://example.com/api/retry")14 .then().statusCode(200)15 .assertThat().body("response", equalTo("Successful"));16 17 given().when().get("https://example.com/api/retry")18 .then().statusCode(200)19 .assertThat().body("response", equalTo("Successful"));2021 given().when().get("https://example.com/api/retry")22 .then().statusCode(200)23 .assertThat().body("response", equalTo("Successful"));24 25 given().when().get("https://example.com/api/retry")26 .then().statusCode(404);27 28 //Code to connect to remote client with desired capabilities29 /*30 DesiredCapabilities capabilities = DesiredCapabilities.chrome();31 capabilities.setCapability("remoteURL", "http://localhost:4444/wd/hub");32 WebDriver driver = new RemoteWebDriver(capabilities);33 */34 }35}
Language: Javascript
1// Mocha and Chai.23//Assuming the API endpoint is https://example.com/api/v1/retry 45const request = require('supertest');6const app = require('../app'); //Assuming the API is built using Express.js78describe('API retry handling tests', () => {9 it('should return success status code on successful request', (done) => {10 request(app)11 .get('/api/v1/retry')12 .set('Accept', 'application/json')13 .expect('Content-Type', /json/)14 .expect(200, done);15 });1617 it('should retry the request and return success status code on transient error', (done) => {18 //Assuming the API returns 500 on a transient error and 200 on success19 const transientErrors = [500, 502, 503, 504];20 let numTries = 0;2122 const makeRequest = () => {23 numTries++;24 request(app)25 .get('/api/v1/retry')26 .set('Accept', 'application/json')27 .expect('Content-Type', /json/)28 .end((err, res) => {29 if (transientErrors.includes(res.status) && numTries < 3) {30 makeRequest();31 } else {32 expect(res.status).to.equal(200);33 done(err);34 }35 });36 };3738 makeRequest();39 });4041 it('should return appropriate status code on permanent error', (done) => {42 //Assuming the API returns 400 for a permanent error43 request(app)44 .get('/api/v1/retry?param=invalid')45 .set('Accept', 'application/json')46 .expect('Content-Type', /json/)47 .expect(400, done);48 });49});5051//Assuming the remote client server has the following desired capabilities5253// const driver = new webdriver.Builder()54// .withCapabilities({55// browserName: 'firefox',56// platformName: 'linux',57// "moz:firefoxOptions": {58// "args": ["--headless"] //Run headless59// }60// })61// .usingServer('http://localhost:4444/wd/hub')62// .build(); //Connect to remote client server with desired capabilities.
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