API Testing : Check version negotiation

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

Language: Java

Framework: Rest assured

copy
1/​/​ Assumptions: 2/​/​ We assume that the API endpoint is already set up and available for testing. 3/​/​ We also assume that the expected version number is "1.0". 4/​/​ Finally, we assume that the correct HTTP status code to be returned is 200.56import org.junit.Test;7import static io.restassured.RestAssured.*;8import static org.hamcrest.Matchers.*;910public class ApiTest {1112 @Test13 public void testVersionNegotiation() {1415 given()16 .header("Accept", "application/​vnd.example.v1+json") /​/​ Sending Accept Header with expected API Version number17 .when()18 .get("http:/​/​localhost:8080/​api/​') /​/​ Endpoint URL1920 .then()21 .statusCode(200) /​/​ Expected HTTP Status Code22 .header("Content-Type", "application/​vnd.example.v1+json") /​/​ Ensuring the returned version is the expected one23 .body("version", equalTo("1.0")); /​/​ JSON version response attribute should have value of "1.0"2425 /​/​ Uncomment this code if executing test on remote client with desired capabilities26 /​/​ DesiredCapabilities capabilities = DesiredCapabilities.chrome();27 /​/​ RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);28 }29}

Language: Javascript

copy
1/​/​Mocha with Chai Assertion Library and Supertest Library.23/​/​Assumptions: The API to be tested is hosted on http:/​/​localhost:3000 and supports versioning via a custom header named 'api-version'.45const request = require('supertest');6const app = require('../​app');7const expect = require('chai').expect;89describe('API Version Negotiation', function () {10 11 it('should return a 200 status code with correct API version header', function () {12 request(app).get('/​api/​data')13 .set('api-version', 'v1')14 .expect(200, function (err, res) {15 if (err) throw err;16 expect(res.body.data).to.equal('This is version 1 of the API');17 });18 });1920 it('should return a 400 status code with incorrect API version header', function () {21 request(app).get('/​api/​data')22 .set('api-version', 'v2')23 .expect(400)24 });2526});2728/​/​Code to connect to remote client with desired capabilities:2930/​/​Assumptions: Remote client URL: http:/​/​10.0.0.1:4444/​wd/​hub, Desired browser: Chrome 93, Desired platform: Windows 10.3132const { Builder } = require('selenium-webdriver');33const chrome = require('selenium-webdriver/​chrome');34const capabilities = {35 browserName: 'chrome',36 browserVersion: '93.0',37 platformName: 'Windows 10'38};3940(async function example() {41 const driver = await new Builder()42 .usingServer('http:/​/​10.0.0.1:4444/​wd/​hub')43 .withCapabilities(capabilities)44 .build();45 await driver.get('http:/​/​www.google.com');46 /​/​ Add your test code here47 await driver.quit();48})();

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