Verify that the API response headers are correct.
Language: Java
Framework: Rest assured
1//Assuming the API endpoint URL is https://api.example.com/users23import io.restassured.RestAssured;4import io.restassured.response.Response;5import org.testng.annotations.Test;6import static org.hamcrest.Matchers.equalTo;78public class API_Test {910 @Test11 public void testResponseHeaders() {1213 //Connecting to the endpoint URL - local driver14 RestAssured.baseURI = "https://api.example.com/users";1516 //Commented code to connect to remote client with desired capabilities:17 /*DesiredCapabilities caps = new DesiredCapabilities();18 caps.setCapability("browserName", "Chrome");19 WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), caps);*/2021 //Sending GET request to the endpoint URL and storing the response22 Response response = RestAssured.given().get();2324 //Checking if response headers are correct using Rest Assured25 response.then().assertThat().header("Content-Type", equalTo("application/json"));26 response.then().assertThat().header("Access-Control-Allow-Origin", equalTo("*"));27 response.then().assertThat().header("Cache-Control", equalTo("no-cache"));28 }29}
Language: Javascript
1// Mocha and Chai.23//Assumptions:4//The API endpoint URL is 'https://example.com/api'5//The expected response headers are 'Content-Type: application/json' and 'Cache-Control: no-cache'67const request = require('request');8const expect = require('chai').expect;910describe('API Testing - Check Response Headers', function () {11 it('Should return the correct response headers', function (done) {12 const options = {13 url: 'https://example.com/api',14 headers: {15 'cache-control': 'no-cache'16 }17 };18 request.get(options, function(err, res, body) {19 expect(err).to.be.null;20 expect(res.statusCode).to.equal(200);21 expect(res.headers['content-type']).to.equal('application/json');22 expect(res.headers['cache-control']).to.equal('no-cache');23 done();24 });25 });2627 //Uncomment below code to use remote driver with desired capabilities28 /*const webdriver = require("selenium-webdriver");29 const capabilities = {30 'browserName': 'chrome',31 'version': '91.0'32 }33 const driver = new webdriver.Builder()34 .usingServer('http://localhost:4444/wd/hub')35 .withCapabilities(capabilities)36 .build();3738 it('Should return the correct response headers', async function () {39 await driver.get('https://example.com/api');4041 const contentType = await driver.executeScript('return window.performance.getEntries()[0].responseHeaders.get("content-type");');42 const cacheControl = await driver.executeScript('return window.performance.getEntries()[0].responseHeaders.get("cache-control");');4344 expect(contentType).to.equal('application/json');45 expect(cacheControl).to.equal('no-cache');46 });*/47});
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