Verify that the API response is not compressed when the client sends a request with the 'Accept-Encoding' header set to a value other than 'gzip'.
Language: Java
Framework: Rest assured
1//Assuming the API endpoint URL is known and can be accessed.23import org.testng.annotations.Test;4import io.restassured.RestAssured;5import io.restassured.response.Response;67public class APITest {89 @Test10 public void testNonGzipCompression() {11 //Establishing connection to API endpoint12 RestAssured.baseURI = "https://example.com/api";13 RestAssured.basePath = "/v1";1415 //Setting request header16 RestAssured.given().header("Accept-Encoding", "deflate") //Assuming client does not want gzip compression17 .when().get("/endpoint") //Assuming the API endpoint to be tested is /endpoint18 .then().assertThat().statusCode(200); //Assuming a successful API response is indicated by a 200 status code1920 //Assuming a different client request header can be tested similarly by changing the value of "Accept-Encoding" header21 //Assuming the response body can also be tested for compression by verifying the content-encoding header in the response22 }23}
Language: Javascript
1// Code for API Testing to Check Non-Gzip Compression:23//Assuming the API endpoint url to test4const apiUrl = 'https://example.com/api';56//Assuming the request headers7const headers = {8 'Accept-Encoding': 'deflate', //Setting Accept-Encoding header to deflate9 'Content-Type': 'application/json' //Assuming JSON request10};1112//Create a new request with the headers13const request = new Request(apiUrl, { headers });1415//Assuming the expected response status code16const expectedStatusCode = 200;1718//Assuming response should not be compressed19const expectedContentEncoding = ''; //empty string for non-gzip compression2021//Assuming locally installed Selenium WebDriver22const webdriver = require('selenium-webdriver');23const chrome = require('selenium-webdriver/chrome');24const chromedriver = require('chromedriver');2526//Set up local Chrome driver27const service = new chrome.ServiceBuilder(chromedriver).build();28chrome.setDefaultService(service);2930//Initialize a new Chrome WebDriver31const driver = new webdriver.Builder()32 .withCapabilities(webdriver.Capabilities.chrome())33 .build();3435//Navigate to the API endpoint URL with the request headers36driver.get(apiUrl, headers);3738//Assuming the response will come in JSON format39driver.setScriptTimeout(10000); //10 seconds40driver.executeAsyncScript(function(callback) {41 //Get the response body as JSON42 const responseBody = JSON.parse(document.body.innerText);43 44 //Verify the response status and content encoding headers45 if (this.status === expectedStatusCode && this.getResponseHeader('Content-Encoding') === expectedContentEncoding) {46 callback(true, responseBody); //Test passed47 } else {48 callback(false, responseBody); //Test failed49 }50}).then(function(passed, responseBody) {51 if (passed) {52 console.log('API testing passed. Response:', responseBody);53 } else {54 console.error('API testing failed. Response:', responseBody);55 }56});5758//Assuming connecting to remote client with desired capabilities59const remoteUrl = 'http://localhost:4444/wd/hub'; //Assuming the remote WebDriver URL60const remoteCapabilities = webdriver.Capabilities.chrome(); //Assuming Chrome browser on remote client6162//Initialize a new remote WebDriver63const remoteDriver = new webdriver.Builder()64 .usingServer(remoteUrl)65 .withCapabilities(remoteCapabilities)66 .build();6768//Navigate to the API endpoint URL with the request headers69remoteDriver.get(apiUrl, headers);7071//Assuming the expected response time72const expectedResponseTime = 5000; //5 seconds7374//Wait for the response with expected response time75remoteDriver.wait(function() {76 //Verify the response status and content encoding headers77 return remoteDriver.executeScript(function() {78 return this.status === expectedStatusCode && this.getResponseHeader('Content-Encoding') === expectedContentEncoding;79 });80}, expectedResponseTime).then(function() {81 console.log('API testing passed on remote client.');82}, function() {83 console.error('API testing failed on remote client.');84});
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