Verify that the API correctly handles API version.
Language: Java
Framework: Rest assured
1// Assuming the API endpoint is https://example.com/api/2// and the expected version is 1.03// assuming the version number is returned as a JSON object with a key "version"45import org.junit.jupiter.api.Test;6import static io.restassured.RestAssured.*;7import static org.hamcrest.Matchers.*;89class ApiTest {1011 @Test12 void apiVersionTest() {13 given()14 .baseUri("https://example.com")15 .basePath("/api")16 .when()17 .get()18 .then()19 .statusCode(200)20 .body("version", equalTo("1.0"));21 }2223 // uncomment the following code to run on a remote client with desired capabilities2425 // import org.openqa.selenium.remote.DesiredCapabilities;26 // import io.restassured.*;27 // import io.restassured.config.*;28 // import io.restassured.config.EncoderConfig.*;29 30 // void apiVersionRemoteTest() {3132 // DesiredCapabilities capabilities = new DesiredCapabilities();33 // capabilities.setCapability("platformName", "ANDROID");34 // capabilities.setCapability("appPackage", "com.example.app");35 // capabilities.setCapability("appActivity", "com.example.app.MainActivity");3637 // RestAssured.useRelaxedHTTPSValidation();3839 // RestAssured.config = RestAssuredConfig.config()40 // .encoderConfig(encoderConfig().encodeContentTypeAs("x-www-form-urlencoded", ContentType.URLENC));4142 // RestAssured.given()43 // .baseUri("https://example.com")44 // .basePath("/api")45 // .with().capabilities(capabilities)46 // .when()47 // .get()48 // .then()49 // .statusCode(200)50 // .body("version", equalTo("1.0"));51 // }52}
Language: Javascript
1// Mocha and Chai.23//Assumptions:4//Assuming API endpoint is https://example.com/api/v1 and the expected version is v156const request = require('request-promise');7const expect = require('chai').expect;89describe('API Version Check Test', function() {1011 it('should return the correct API version in response', async function() {1213 // Make request to API endpoint14 const response = await request.get('https://example.com/api/v1');1516 // Parse the response17 const responseBody = JSON.parse(response.body);1819 // Extract the API version from the response20 const apiVersion = responseBody.version;2122 // Check if API version matches the expected value23 expect(apiVersion).to.equal('v1');24 });2526});2728// For connecting to a remote client with desired capabilities, add the following commented code:2930// const webdriver = require('selenium-webdriver');31// const capabilities = {32// browserName: 'chrome',33// platformName: 'Windows 10',34// version: 'latest',35// // add any other desired capabilities here36// };37// const remoteUrl = 'http://localhost:4444/wd/hub';38// const driver = new webdriver.Builder()39// .withCapabilities(capabilities)40// .usingServer(remoteUrl)41// .build();
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