API Testing : Check API availability

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

Language: Java

Framework: Rest assured

copy
1/​/​Assuming we are testing an API that provides information about the availability of resources2/​/​Assuming we have the URL of the API3import org.junit.Test;4import io.restassured.RestAssured;5import io.restassured.response.Response;6import static org.hamcrest.Matchers.*;78public class APIAvailabilityTest {9 @Test10 public void testAPIAvailability() {11 /​/​Connecting to API12 RestAssured.baseURI = "http:/​/​example.com/​api"; /​/​Assuming our API URL13 RestAssured.port = 80; /​/​Assuming default port14 15 /​/​Checking API availability using GET request16 Response response = given().when().get("/​availability");17 18 /​/​Verifying response code19 response.then().assertThat().statusCode(200); 20 21 /​/​Verifying response body for correct resources22 response.then().assertThat().body("resource1", equalTo("available"));23 response.then().assertThat().body("resource2", equalTo("unavailable"));24 25 /​/​Connecting to remote client with desired capabilities26 DesiredCapabilities capabilities = DesiredCapabilities.chrome(); /​/​Assuming we want to use Chrome browser27 capabilities.setCapability("version", "latest"); /​/​Assuming we want to use latest version of Chrome28 RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities); /​/​Assuming remote client is running on local machine with default port29 }30}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assuming the API URL and the expected response code4const apiUrl = 'https:/​/​example-api.com/​availability';5const expectedStatusCode = 200;67/​/​Importing necessary packages8const chai = require('chai');9const chaiHttp = require('chai-http');10const expect = chai.expect;1112/​/​Connecting to local driver13chai.use(chaiHttp);1415describe('API Availability Test', () => {16 it('should return a valid response status code', async () => {17 const res = await chai.request(apiUrl).get('/​');18 expect(res.status).to.equal(expectedStatusCode);19 });20});

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