Verify that the API correctly handles retry attempts and returns the correct HTTP status code.
Language: Java
Framework: Rest assured
1//Assuming the API endpoint is https://example.com/api/retry and the retry attempts allowed are 3.23import org.junit.Test;4import static io.restassured.RestAssured.given;5import static org.hamcrest.Matchers.equalTo;67public class RetryAttemptsTest {89 @Test10 public void verifyRetryAttempts() {1112 int expectedStatusCode = 200; //Assuming the expected HTTP status code is 200.13 14 //Set retry attempts15 given().config(RestAssured.config().retryConfig(RetryConfig16 .retryConfig().maxAttempts(3).waitForRetry(Wait17 .forDuration(Duration.ofMillis(500)))));18 19 given().when().get("https://example.com/api/retry")20 .then().assertThat().statusCode(expectedStatusCode)21 .and().body("response", equalTo("Success"));//Assuming the API returns a response as "Success" in case of successful retry attempts.22 23 // Uncomment the below code to connect to a remote client with desired capabilities24 /*25 DesiredCapabilities desiredCapabilities = new DesiredCapabilities();26 desiredCapabilities.setCapability("platform", "Windows 10");27 desiredCapabilities.setCapability("browserName", "chrome");28 WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), desiredCapabilities);29 */30 }31}
Language: Javascript
1// Mocha and Chai.23//Assumptions:4//1. The API endpoint is already available and accessible.5//2. Retry attempts are initiated automatically upon receiving certain response codes.6//3. HTTP status code 401 Unauthorized triggers a retry attempt.7//4. HTTP status code 400 Bad Request does not trigger a retry attempt.8//5. The expected HTTP status code for a successful request is 200 OK.910const expect = require('chai').expect;11const request = require('supertest');1213describe('API Retry Attempts', function () {14 //Assumption: The API endpoint is at http://localhost:300015 const apiUrl = 'http://localhost:3000';1617 //Assumption: The API supports a POST request to /login18 const loginEndpoint = '/login';1920 //Assumption: Login credentials are passed in the request body as JSON21 const loginPayload = {22 username: 'testuser',23 password: 'testpass'24 };2526 //Assumption: The API returns 401 if the login credentials are incorrect27 const badLoginPayload = {28 username: 'wronguser',29 password: 'wrongpass'30 };3132 //Assumption: The API returns a JSON response with a token field upon successful login33 let authToken;3435 it('should handle retry attempts and return the correct HTTP status code', function (done) {36 //Assumption: The API supports a GET request to /protected with a JWT token in the Authorization header37 const protectedEndpoint = '/protected';3839 //Assumption: The API returns 400 Bad Request for requests without a JWT token40 request(apiUrl)41 .get(protectedEndpoint)42 .expect(400, done);4344 //Assumption: The API initiates a retry attempt upon receiving 401 Unauthorized45 request(apiUrl)46 .get(protectedEndpoint)47 .set('Authorization', 'Bearer ' + 'invalidtoken')48 .expect(401, function (err, res) {49 if (err) return done(err);5051 //Extract the Retry-After header value52 const retryAfter = res.headers['retry-after'];5354 //Assumption: The API sets the Retry-After header to the number of seconds until the next retry attempt55 //Assumption: The API initiates a second retry attempt after the number of seconds specified in the Retry-After header56 setTimeout(function () {57 request(apiUrl)58 .get(protectedEndpoint)59 .set('Authorization', 'Bearer ' + authToken)60 .expect(200, done);61 }, retryAfter * 1000);62 });63 });6465 before(function (done) {66 request(apiUrl)67 .post(loginEndpoint)68 .send(loginPayload)69 .end(function (err, res) {70 if (err) return done(err);7172 //Assumption: The API returns 200 OK and a token field in the response body upon successful login73 authToken = res.body.token;7475 done();76 });77 });7879 after(function (done) {80 //Assumption: The API supports a DELETE request to /logout with a JWT token in the Authorization header81 const logoutEndpoint = '/logout';8283 request(apiUrl)84 .delete(logoutEndpoint)85 .set('Authorization', 'Bearer ' + authToken)86 .end(function (err, res) {87 if (err) return done(err);8889 //Assumption: The API returns 200 OK upon successful logout90 done();91 });92 });93});
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