API Testing : Verify response format

Verify that the API response is in the expected format (e.g. JSON, XML).

Language: Java

Framework: Rest assured

copy
1/​/​Assuming API endpoint is https:/​/​example.api.com/​endpoint and the response content type can be either JSON or XML23import static io.restassured.RestAssured.*;4import io.restassured.response.Response;5import org.junit.Assert;67public class APITest {89 public static void main(String[] args) {10 11 /​/​Test using local driver12 Response response = given().when().get("https:/​/​example.api.com/​endpoint");13 String contentType = response.getContentType();14 Assert.assertTrue(contentType.equals("application/​json") || contentType.equals("application/​xml"));15 16 /​/​Test using remote client with desired capabilities17 /​/​Assuming remote client URL is "http:/​/​localhost:4444/​wd/​hub" and desired capability is Chrome browser18 RemoteWebDriver driver = null;19 DesiredCapabilities capabilities = new DesiredCapabilities();20 capabilities.setBrowserName("chrome");21 try {22 driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);23 response = given().when().get("https:/​/​example.api.com/​endpoint");24 contentType = response.getContentType();25 Assert.assertTrue(contentType.equals("application/​json") || contentType.equals("application/​xml"));26 } catch (MalformedURLException e) {27 e.printStackTrace();28 }29 driver.quit(); 30 }3132}

Language: Javascript

copy
1/​/​ Mocha + Chai. 23/​/​ Assumption: The API endpoint is already available. 45/​/​ Code:67const chai = require('chai');8const expect = chai.expect;9const request = require('request');1011describe('API Testing - Verify Response Format', () => {12 let response;13 14 before((done) => {15 request.get({16 url: 'http:/​/​localhost:3000/​api/​users', /​/​ Replace with actual API endpoint17 headers: {18 'Content-Type': 'application/​json'19 }20 }, (err, res, body) => {21 if (err) {22 done(err);23 }24 response = JSON.parse(body);25 done();26 });27 });28 29 it('should return a response in JSON format', () => {30 expect(response).to.be.an('object');31 });32 33 /​/​ Commented code to connect to remote client with desired capabilities34 /​*35 const webdriver = require('selenium-webdriver');36 const remote = require('selenium-webdriver/​remote');37 38 const capabilities = {39 'browserName': 'chrome',40 'browserVersion': 'latest',41 'platformName': 'Windows 10'42 };43 44 const driver = new webdriver.Builder()45 .usingServer('http:/​/​selenium-hub:4444/​wd/​hub') /​/​ Replace with actual Selenium Grid URL46 .withCapabilities(capabilities)47 .build();48 49 after(() => {50 driver.quit();51 });52 */​53});5455/​/​ Note: This code assumes that the API response should be in JSON format. Use the appropriate expect statement if the expected format is XML or any other format. The commented code shows how to connect to a remote Selenium Grid client with desired capabilities. Please replace the URL with the actual URL of your Selenium Grid hub.

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