API Testing : Check API compliance

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

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the API endpoint is provided and available for testing23import org.testng.annotations.Test;4import io.restassured.RestAssured;5import io.restassured.response.Response;6import io.restassured.specification.RequestSpecification;7import static org.hamcrest.Matchers.equalTo;89public class APITest {10 11 @Test12 public void apiComplianceTest() {13 14 /​/​Specify the base URL of the API15 RestAssured.baseURI = "https:/​/​api.example.com";16 17 /​/​Request object18 RequestSpecification httpRequest = RestAssured.given();19 20 /​/​Send the request and get the response21 Response response = httpRequest.get("/​compliance");22 23 /​/​Response body validation24 response.then()25 .assertThat()26 .statusCode(200)27 .body("resource1", equalTo("value1"))28 .body("resource2", equalTo("value2"))29 .body("resource3", equalTo("value3"));30 31 /​/​Test to connect to remote client with desired capabilities32 /​/​Add these lines of commented code to connect to a remote Selenium Grid with the desired capabilities3334 /​/​DesiredCapabilities capabilities = new DesiredCapabilities();3536 /​/​capabilities.setBrowserName("chrome");37 /​/​capabilities.setPlatform(Platform.LINUX);3839 /​/​RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);4041 }42 43}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​ Assumptions:4/​/​ 1. API endpoint is accessible and the correct URL has been provided.5/​/​ 2. The requested API compliance metrics are available and valid.6/​/​ 3. A valid API key/​token/​authentication credentials have been provided.78const chai = require('chai');9const expect = chai.expect;10const request = require('request');1112describe('API Compliance Testing', function() {13 const apiUrl = 'https:/​/​example.com/​api';14 const apiKey = 'valid-api-key';1516 it('should return resources for each API compliance metric', function(done) {17 const endpoint = `${apiUrl}/​api-compliance`;18 const options = {19 headers: {20 'Authorization': `Bearer ${apiKey}`21 }22 };2324 /​/​ Make API request25 request.get(endpoint, options, function(err, res, body) {26 if(err) done(err);27 else {28 expect(res.statusCode).to.equal(200); /​/​ Verify the response status code29 expect(body).to.not.be.empty; /​/​ Verify that the response body is not empty30 expect(JSON.parse(body)).to.have.all.keys('metric1', 'metric2', 'metric3'); /​/​ Verify that the response body contains all requested metrics31 done();32 }33 });34 });3536 /​/​ Code to connect to a remote client using desired capabilities37 /​/​ const webdriver = require('selenium-webdriver');38 /​/​ const capabilities = webdriver.Capabilities.chrome();39 /​/​ const chromeOptions = {40 /​/​ 'args': ['--disable-infobars', '--disable-extensions', '--start-maximized']41 /​/​ };42 /​/​ capabilities.set('chromeOptions', chromeOptions);43 /​/​ const driver = new webdriver.Builder()44 /​/​ .usingServer('http:/​/​remote-client-example.com:4444/​wd/​hub')45 /​/​ .withCapabilities(capabilities)46 /​/​ .build();47});

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