API Testing : Check version handling

Verify that the API correctly handles versioning and returns the correct HTTP status code.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the API endpoint is http:/​/​example.com/​version-api2/​/​Assuming the API version is specified in the header as 'version=X' where X is the version number34import static io.restassured.RestAssured.given;56import org.testng.annotations.Test;78import io.restassured.http.Header;9import io.restassured.response.Response;1011public class APIVersionTest {1213 @Test14 public void testVersionHandling() {15 /​/​Using local driver16 Response response = given()17 .header(new Header("version", "1.0"))18 .when()19 .get("http:/​/​example.com/​version-api")20 .then()21 .extract()22 .response();2324 /​/​Verifying HTTP status code25 assert(response.getStatusCode() == 200);2627 /​/​Verifying if the correct version number is returned28 assert(response.getBody().asString().contains("1.0"));2930 /​/​code for connecting to remote client with desired capabilities31 /​/​Assuming the remote client is on http:/​/​example.com:4444/​wd/​hub32 /​/​Assuming the platform is Windows 1033 /​/​Assuming the browser is Chrome version 90.034 /​*35 DesiredCapabilities capabilities = DesiredCapabilities.chrome();36 capabilities.setPlatform(Platform.WIN10);37 capabilities.setVersion("90.0");3839 RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​example.com:4444/​wd/​hub"), capabilities);40 */​41 }42}

Language: Javascript

copy
1/​/​ Mocha + Chai23/​/​Assuming the API endpoint is "https:/​/​api.example.com/​version"45const { expect } = require('chai');6const request = require('request');78describe('Version Handling Test', function() {9 const url = 'https:/​/​api.example.com/​version';10 const apiVersion = 'v1'; /​/​Assuming the current version is v11112 it("Should return 200 status code for valid version", function(done) {13 const options = {14 url: url,15 headers: {16 'Accept-Version': apiVersion /​/​Setting accepted version in headers17 }, 18 json: true19 };2021 request.get(options, function(err, res, body) {22 expect(res.statusCode).to.equal(200); /​/​Expecting response to be 200 status code23 done();24 });25 });2627 it("Should return 400 status code for invalid version", function(done) {28 const options = {29 url: url,30 headers: {31 'Accept-Version': 'invalidVersion' /​/​Setting invalid version in headers32 }, 33 json: true34 };3536 request.get(options, function(err, res, body) {37 expect(res.statusCode).to.equal(400); /​/​Expecting response to be 400 status code38 done();39 });40 });4142 /​/​ Un-comment below code to connect to a remote client with desired capabilities4344/​/​ const webdriver = require('selenium-webdriver');45/​/​ const capabilities = {46/​/​ browserName: 'chrome',47/​/​ platform: 'Windows 10',48/​/​ version: 'latest'49/​/​ };50/​/​ 51/​/​ const remoteDriver = new webdriver.Builder()52/​/​ .withCapabilities(capabilities)53/​/​ .usingServer('http:/​/​your_remote_server/​wd/​hub') /​/​Set your remote server URL here54/​/​ .build();55});

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