Verify that the API returns a response with a custom HTTP header when a specific request header is sent.
Language: Java
Framework: Rest assured
1// Assumption: The API URL is https://example.com/api23import org.junit.*;4import io.restassured.*;5import io.restassured.response.Response;67public class APITest {8 9 @Test10 public void testCustomHeaderResponse() {11 12 // Set Request Header13 RequestSpecification request = RestAssured.given();14 request.header("X-Requested-With", "XMLHttpRequest");15 16 // Send Request and Validate Response17 Response response = request.get("https://example.com/api");18 Assert.assertEquals(200, response.getStatusCode());19 Assert.assertEquals("custom_value", response.getHeader("X-Custom-Header"));20 21 /*22 // Uncomment to Connect to Remote Client with Desired Capabilities23 DesiredCapabilities capabilities = new DesiredCapabilities();24 // Set Desired Capabilities25 RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);26 driver.get("https://example.com/api");27 response = driver.getPageSource();28 Assert.assertEquals("custom_value", response.getHeader("X-Custom-Header"));29 driver.quit();30 */31 }32}
Language: Javascript
1// Mocha/Chai and Axios.23// Assumption: API endpoint URL is http://localhost:3000/api4// Assumption: Request header key is "X-Custom-Header" with value "test-header"5// Assumption: Expected response header key is "X-Test-Header" with value "hello"67const axios = require('axios');8const expect = require('chai').expect;910describe('API Testing', () => {11 it('Check custom header response', async () => {12 const headers = { 'X-Custom-Header': 'test-header' };13 const response = await axios.get('http://localhost:3000/api', { headers });14 expect(response.headers['x-test-header']).to.equal('hello');15 });16});1718// To run the test on a remote client with desired capabilities:19// const webdriver = require('selenium-webdriver');20// const capabilities = webdriver.Capabilities.{desired capabilities}21// const driver = new webdriver.Builder()22// .usingServer('http://remoteclient:4444/wd/hub')23// .withCapabilities(capabilities)24// .build();
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