Verify that the API correctly handles content negotiation and returns the correct HTTP status code.
Language: Java
Framework: Rest assured
1// Assumptions:2// * The base URL for the API is http://api.example.com3// * The endpoint for content negotiation is /content-negotiation4// * The API supports both JSON and XML content types5// * JSON content is the default if no Accept header is provided6// * A valid API key is required for all requests78import org.junit.Test;9import static io.restassured.RestAssured.*;10import static org.hamcrest.Matchers.*;1112public class ContentNegotiationTest {1314 @Test15 public void testContentNegotiation() {1617 // Set base URL18 baseURI = "http://api.example.com";1920 // Set API key header21 header("X-Api-Key", "validApiKey");2223 // Test with JSON24 given()25 .accept("application/json")26 .when()27 .get("/content-negotiation")28 .then()29 .assertThat()30 .statusCode(200)31 .contentType("application/json");3233 // Test with XML34 given()35 .accept("application/xml")36 .when()37 .get("/content-negotiation")38 .then()39 .assertThat()40 .statusCode(200)41 .contentType("application/xml");4243 // Test with invalid Accept header44 given()45 .accept("text/plain")46 .when()47 .get("/content-negotiation")48 .then()49 .assertThat()50 .statusCode(406);5152 }5354 // If connecting to a remote client with desired capabilities:55 // import org.openqa.selenium.remote.DesiredCapabilities;56 // import org.openqa.selenium.remote.RemoteWebDriver;57 // import java.net.URL;58 //59 // @Test60 // public void testContentNegotiation() throws Exception {61 //62 // // Set desired capabilities for remote client63 // DesiredCapabilities capabilities = DesiredCapabilities.chrome();64 //65 // // Set remote client URL66 // URL remoteUrl = new URL("http://remote.client.com:4444/wd/hub");67 //68 // // Create remote driver with desired capabilities and URL69 // RemoteWebDriver driver = new RemoteWebDriver(remoteUrl, capabilities);70 //71 // // Set base URL for remote client72 // driver.get("http://api.example.com");73 //74 // // Rest of code remains the same75 //76 // driver.quit();77 //78 // }7980}
Language: Java
Framework: Rest assured
1// Assume API Base URI as "http://example.com/api"2// Assume endpoint for content negotiation as "/negotiate"34import org.junit.Test;5import io.restassured.RestAssured;6import io.restassured.http.ContentType;78public class ContentNegotiationTest {910@Test11public void testContentNegotiation() {12 // Scenario Setup13 RestAssured.baseURI = "http://example.com/api";14 15 // Request Definition16 RestAssured.given()17 .accept(ContentType.JSON)18 .when()19 .get("/negotiate")20 .then()21 // Assertions22 .statusCode(200)23 .contentType(ContentType.JSON);24 25 // To connect to remote client with desired capabilities26 // RestAssured.remote("remote-client-url", port, "username", "password");27 }28}
Language: Javascript
1// Mocha and Chai.23//Assuming endpoint URL is "https://exampleapi.com/content"45const chai = require('chai');6const chaiHttp = require('chai-http');7const expect = chai.expect;8chai.use(chaiHttp);910describe('API content negotiation tests', function () {1112 it('should return 200 OK for "application/json" header', function (done) {13 chai.request('https://exampleapi.com')14 .get('/content')15 .set('Accept', 'application/json')16 .end(function (err, res) {17 expect(err).to.be.null;18 expect(res).to.have.status(200);19 done();20 });21 });2223 it('should return 406 Not Acceptable for unsupported content types', function (done) {24 chai.request('https://exampleapi.com')25 .get('/content')26 .set('Accept', 'text/plain')27 .end(function (err, res) {28 expect(err).to.be.null;29 expect(res).to.have.status(406);30 done();31 });32 });3334 it('should return 415 Unsupported Media Type for unsupported request types', function (done) {35 chai.request('https://exampleapi.com')36 .post('/content')37 .send({38 "title": "Example Title",39 "body": "Example body content"40 })41 .set('Content-Type', 'text/plain')42 .end(function (err, res) {43 expect(err).to.be.null;44 expect(res).to.have.status(415);45 done();46 });47 });4849}); 5051//Assuming remote endpoint URL is "https://remoteserver.com/content" with desired capabilities for browser version and operating system version.5253const webdriver = require('selenium-webdriver');54const {Capabilities} = require('selenium-webdriver/lib/capabilities');55const chrome = require('selenium-webdriver/chrome');56const firefox = require('selenium-webdriver/firefox');57const edge = require('selenium-webdriver/edge');58const safari = require('selenium-webdriver/safari');5960const capabilities = new Capabilities();61capabilities.set('browserName', 'chrome');62capabilities.set('browserVersion', 'latest');63capabilities.set('platform', 'Windows 10');6465const options = new chrome.Options();66options.addArguments('--disable-extensions');67options.addArguments('--disable-popup-blocking');68options.addArguments('--disable-infobars');6970const driver = new webdriver.Builder()71 .forBrowser('chrome')72 .withCapabilities(capabilities)73 .setChromeOptions(options)74 .build();7576//To connect to remote client with the desired capabilities, replace the `forBrowser` method with `usingServer`, `withCapabilities` method with `withCapabilities`, and `setChromeOptions` method with `setFirefoxOptions`, `setEdgeOptions`, or `setSafariOptions` methods based on the browser being used. Update the capabilities with the appropriate values for the remote host and desired browser. 7778const remoteDriver = new webdriver.Builder()79 .usingServer('http://localhost:4444/wd/hub')80 .withCapabilities(capabilities)81 .build();
Language: Javascript
1// Mocha and Chai.23//Assuming API endpoint is "http://example.com/api/"4//Assuming acceptable content types are 'application/json' and 'application/xml'5//Assuming we have test data for both content types67const request = require('request');8const expect = require('chai').expect;910describe('Content Negotiation Test', function() {11 it('should handle JSON content negotiation', function(done) {12 const options = {13 url: 'http://example.com/api/',14 headers: {15 'Accept': 'application/json'16 }17 };18 request.get(options, function(err, res, body) {19 expect(res.statusCode).to.equal(200);20 expect(res.headers['content-type']).to.include('application/json');21 expect(JSON.parse(body)).to.deep.equal(jsonTestData); //Assuming jsonTestData is valid JSON test data22 done();23 });24 });25 26 it('should handle XML content negotiation', function(done) {27 const options = {28 url: 'http://example.com/api/',29 headers: {30 'Accept': 'application/xml'31 }32 };33 request.get(options, function(err, res, body) {34 expect(res.statusCode).to.equal(200);35 expect(res.headers['content-type']).to.include('application/xml');36 expect(parseXML(body)).to.deep.equal(xmlTestData); //Assuming xmlTestData is valid XML test data37 done();38 });39 });40});4142//Code to use remote client with desired capabilities43//Assuming we have Selenium Grid running on remote machine with IP address 192.168.0.1 and port 44444445const webdriver = require('selenium-webdriver');46const {Builder, Capabilities} = require('selenium-webdriver');4748const capabilities = Capabilities.chrome(); //Assuming we want to use Chrome browser49capabilities.set('version', '93.0');50capabilities.set('platform', 'WINDOWS');51capabilities.set('screenResolution', '1920x1080');5253const driver = new Builder()54 .withCapabilities(capabilities)55 .usingServer('http://192.168.0.1:4444/wd/hub')56 .build();57 58//From here, we can write our Selenium test cases using 'driver' instance.
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