API Testing : Check content type representation

Verify that the API response contains the correct resource representation based on the provided content type.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the API endpoint URL is http:/​/​example.com/​api/​v1/​users 2/​/​Assuming valid content types are 'application/​json' and 'application/​xml'34import org.testng.annotations.Test;5import static io.restassured.RestAssured.*;6import static org.hamcrest.Matchers.*;78public class APITest {910 @Test11 public void testCorrectResourceRepresentationJSON() {12 given()13 .contentType("application/​json")14 .when()15 .get("http:/​/​example.com/​api/​v1/​users")16 .then()17 .assertThat()18 .contentType("application/​json")19 .body("data.id", hasItems(1, 2, 3)); 20 /​/​Assuming that the JSON response has a 'data' key that contains id values of 1, 2, and 321 }2223 @Test24 public void testCorrectResourceRepresentationXML() {25 given()26 .contentType("application/​xml")27 .when()28 .get("http:/​/​example.com/​api/​v1/​users")29 .then()30 .assertThat()31 .contentType("application/​xml")32 .body("data.id", hasItems("1", "2", "3"));33 /​/​Assuming that the XML response has a 'data' element that contains id values of 1, 2, and 3 enclosed in tags34 }3536 /​/​Code to connect to remote client with desired capabilities37 /​*38 import org.openqa.selenium.remote.DesiredCapabilities;39 import org.openqa.selenium.remote.RemoteWebDriver;40 import java.net.URL;4142 public class LambdaTestExample {43 public static final String USERNAME = "YOUR_USERNAME";44 public static final String AUTOMATE_KEY = "YOUR_AUTOMATE_KEY";45 public static final String URL = "https:/​/​" + USERNAME + ":" + AUTOMATE_KEY + "@hub.lambdatest.com/​wd/​hub";4647 public static void main(String[] args) throws Exception {48 DesiredCapabilities caps = new DesiredCapabilities();49 caps.setCapability("os", "Windows");50 caps.setCapability("os_version", "10");51 caps.setCapability("browser", "Chrome");52 caps.setCapability("browser_version", "83");53 caps.setCapability("name", "Test Case Name");5455 RemoteWebDriver driver = new RemoteWebDriver(new URL(URL), caps);56 driver.get("https:/​/​www.google.com");57 System.out.println(driver.getTitle());58 driver.quit();59 }60 }61 */​62}

Language: Javascript

copy
1/​/​ Mocha/​Chai23/​/​Assumptions:4/​/​1. The API endpoint is accessible by the local machine5/​/​2. The API endpoint accepts 'GET' requests6/​/​3. The API endpoint requires a 'Content-Type' header in the request7/​/​4. The API response is in JSON format89const request = require('request');10const expect = require('chai').expect;1112describe('API Testing for Content Type Representation', function() {13 14 it('should return the JSON resource representation for a valid Content-Type header', function(done) {1516 /​/​Headers to be passed in the request17 const headers = {18 'Content-Type': 'application/​json'19 };2021 /​/​Options for the request22 const options = {23 url: 'https:/​/​api.example.com/​users',24 method: 'GET',25 headers: headers26 };2728 /​/​Making the request and verifying the response29 request(options, function(error, response, body) {30 expect(response.headers['content-type']).to.equal('application/​json');31 done();32 });33 });3435 it('should return the XML resource representation for a valid Content-Type header', function(done) {3637 /​/​Headers to be passed in the request38 const headers = {39 'Content-Type': 'application/​xml'40 };4142 /​/​Options for the request43 const options = {44 url: 'https:/​/​api.example.com/​users',45 method: 'GET',46 headers: headers47 };4849 /​/​Making the request and verifying the response50 request(options, function(error, response, body) {51 expect(response.headers['content-type']).to.equal('application/​xml');52 done();53 });54 });5556 /​/​Connecting to remote client with desired capabilities57 /​*58 const webdriver = require('selenium-webdriver');59 const remote = require('selenium-webdriver/​remote');60 61 const capabilities = webdriver.Capabilities.chrome();62 const chromeOptions = {63 'args': ['--start-maximized'],64 'prefs': {'profile.managed_default_content_settings.geolocation': 1}65 };66 capabilities.set('chromeOptions', chromeOptions);67 const driver = new webdriver.Builder()68 .usingServer('http:/​/​localhost:4444/​wd/​hub')69 .withCapabilities(capabilities)70 .build();71 */​72});

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