API Testing : Check versioning

Verify that the API correctly handles versioning and returns the correct resources for each version.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming that the API provides versioning information in the endpoint URL2/​/​Assuming that the API offers two versions, v1 and v23/​/​Assuming that the resources returned are consistent across versions4/​/​Assuming that the API returns status code 200 for successful requests56import org.junit.Test;7import io.restassured.RestAssured;8import io.restassured.response.Response;910public class ApiTest {1112 @Test13 public void testApiVersioning() {14 String baseUrl = "https:/​/​api.example.com/​";15 String endpointV1 = "v1/​resources";16 String endpointV2 = "v2/​resources";1718 /​/​Executing the test using local driver19 RestAssured.baseURI = baseUrl;2021 /​/​Executing the test using remote client with desired capabilities22 /​*23 DesiredCapabilities caps = new DesiredCapabilities();24 caps.setCapability("version", "81.0");25 caps.setCapability("platform", "WINDOWS");26 caps.setCapability("name", "API Test - Check versioning");27 RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), caps);28 RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();29 */​3031 /​/​Sending the requests and verifying the responses32 Response responseV1 = RestAssured.given().when().get(endpointV1);33 Response responseV2 = RestAssured.given().when().get(endpointV2);3435 assert responseV1.statusCode() == 200 : "Response code for v1 is not 200";36 assert responseV2.statusCode() == 200 : "Response code for v2 is not 200";3738 /​/​Verifying that the resources for v1 and v2 are not the same39 assert !responseV1.getBody().asString().equals(responseV2.getBody().asString()) : "Resources for v1 and v2 are the same";40 }41}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​ Assumptions:4/​/​ - The API end-point is https:/​/​example.com/​api5/​/​ - The API supports versioning and uses a 'v' prefix, e.g., https:/​/​example.com/​api/​v16/​/​ - The API returns JSON data in response to all requests78const request = require('request-promise');9const expect = require('chai').expect;1011describe('API Versioning test suite', () => {1213 it('should return correct resources for v1', async () => {14 const options = {15 method: 'GET',16 uri: 'https:/​/​example.com/​api/​v1',17 json: true18 };19 const response = await request(options);20 expect(response).to.have.property('version', 'v1');21 expect(response).to.have.property('resources').to.be.an('array');22 });2324 it('should return correct resources for v2', async () => {25 const options = {26 method: 'GET',27 uri: 'https:/​/​example.com/​api/​v2',28 json: true29 };30 const response = await request(options);31 expect(response).to.have.property('version', 'v2');32 expect(response).to.have.property('resources').to.be.an('array');33 });34 35 /​/​ Uncomment this code to run the test using remote driver and desired capabilities36 /​*37 const webdriver = require('selenium-webdriver');38 const remote = require('selenium-webdriver/​remote');3940 it('should run the test on remote client', async () => {41 const options = new remote.DriverServiceBuilder().usingAnyFreePort().build();42 const capabilities = webdriver.Capabilities.chrome();43 const driver = await new webdriver.Builder()44 .usingServer(options.getUrl())45 .withCapabilities(capabilities)46 .build();47 /​/​ Use the driver to perform the test48 await driver.get('https:/​/​example.com/​api/​v1');49 const body = await driver.findElement(webdriver.By.tagName('body')).getText();50 expect(body).to.contain('v1');51 await driver.quit();52 });53 */​54});

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