API Testing : Check API reliability

Verify that the API correctly handles API reliability and returns the correct resources for each API reliability metric.

Language: Java

Framework: Rest assured

copy
1import org.testng.annotations.Test;23import io.restassured.RestAssured;4import io.restassured.response.Response;56public class APITest {78 @Test9 public void testAPIReliability() {1011 /​/​Assuming base URI for API12 RestAssured.baseURI = "https:/​/​api.example.com";1314 /​/​Assuming API endpoint for API reliability15 String apiEndpoint = "/​api/​reliability";1617 /​/​Assuming that the API returns JSON format18 RestAssured.given()19 .when().get(apiEndpoint)20 .then().assertThat().statusCode(200);2122 /​/​Assuming that the API returns the resources for each API reliability metric23 Response response = RestAssured.given()24 .when().get(apiEndpoint)25 .then().extract().response();26 String responseBody = response.getBody().asString();27 Assert.assertTrue(responseBody.contains("resource1"));28 Assert.assertTrue(responseBody.contains("resource2"));29 Assert.assertTrue(responseBody.contains("resource3"));3031 /​/​Assuming connecting to remote client with desired capabilities32 /​/​DesiredCapabilities capabilities = new DesiredCapabilities();33 /​/​capabilities.setBrowserName("chrome");34 /​/​WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);3536 }37}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​ Assumptions:4/​/​ 1. The API endpoint is "https:/​/​example.com/​api".5/​/​ 2. The API resource for API reliability is "/​api/​reliability".6/​/​ 3. The API response for API reliability contains a JSON object with a "reliability" key.7/​/​ 4. A reliable API should return a reliability value greater than or equal to 0.8.8/​/​ 5. An unreliable API should return a reliability value less than 0.8.910const chai = require('chai');11const chaiHttp = require('chai-http');12const expect = chai.expect;1314chai.use(chaiHttp);1516const apiEndpoint = 'https:/​/​example.com/​api';17const apiResource = '/​api/​reliability';1819describe('API reliability', () => {20 it('should return a reliability value greater than or equal to 0.8 for a reliable API', (done) => {21 chai.request(apiEndpoint)22 .get(apiResource)23 .end((err, res) => {24 expect(err).to.be.null;25 expect(res).to.have.status(200);26 expect(res.body.reliability).to.be.gte(0.8);27 done();28 });29 });3031 it('should return a reliability value less than 0.8 for an unreliable API', (done) => {32 /​/​ Remote client with desired capabilities:33 /​/​ const capabilities = {34 /​/​ browserName: 'chrome',35 /​/​ 'goog:chromeOptions': {36 /​/​ args: ['--headless', '--disable-gpu', '--no-sandbox']37 /​/​ }38 /​/​ };39 /​/​ const driver = new webdriver.Builder()40 /​/​ .usingServer('http:/​/​localhost:4444/​wd/​hub')41 /​/​ .withCapabilities(capabilities)42 /​/​ .build();4344 /​/​ Uncomment the code above to connect to remote client with desired capabilities.4546 /​/​ Assuming an unreliable API by setting the reliability value to 0.6:47 const unreliableApi = { reliability: 0.6 };4849 chai.request(apiEndpoint)50 .get(apiResource)51 .send(unreliableApi)52 .end((err, res) => {53 expect(err).to.be.null;54 expect(res).to.have.status(200);55 expect(res.body.reliability).to.be.lt(0.8);56 done();57 });58 });59});

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.

Accelerate Your Automation Test Cycles With LambdaTest

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.

Try LambdaTest

Power Your Software Testing with AI and cloud

Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.

Start Free Testing