API Testing : Check no compression

Verify that the API response is not compressed when the client does not send the 'Accept-Encoding' header.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the API endpoint is 'https:/​/​exampleapi.com/​data'2/​/​Assuming 'Accept-Encoding' header is not sent by the client34import io.restassured.RestAssured;5import io.restassured.response.Response;67public class NoCompressionTest {89 @Test10 public void testNoCompression() {1112 /​/​Set base URI for API endpoint13 RestAssured.baseURI = "https:/​/​exampleapi.com/​data";1415 /​/​Send GET request to API16 Response response = given()17 .when()18 .get();1920 /​/​Assert that the response has no compression21 String contentEncoding = response.getHeader("Content-Encoding");22 Assert.assertNull(contentEncoding, "Content-Encoding header was present indicating compression");2324 /​/​To connect to remote client with desired capabilities, add the following commented code:25 /​*DesiredCapabilities capabilities = new DesiredCapabilities();26 capabilities.setBrowserName("chrome");27 capabilities.setCapability("version", "91.0");28 capabilities.setCapability("platform", "Windows 10");29 WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);30 RestAssured.proxy("localhost", 8080);*/​3132 }3334}

Language: Javascript

copy
1/​/​ Mocha + Chai + Supertest.23const expect = require('chai').expect;4const request = require('supertest');56describe('API Testing', () => {7 const apiUrl = 'https:/​/​example.com/​api';89 it('should not compress response when client does not send header', (done) => {10 request(apiUrl)11 .get('/​endpoint')12 .end((err, res) => {13 expect(err).to.be.null;14 expect(res.status).to.equal(200);15 expect(res.headers['content-encoding']).to.not.exist;16 done();17 });18 });19});2021/​/​ Uncomment and modify the desired capabilities to use remote client22/​/​ const webdriver = require('selenium-webdriver');23/​/​ const capabilities = {24/​/​ browserName: 'chrome',25/​/​ version: 'latest',26/​/​ platform: 'Windows 10',27/​/​ name: 'API testing'28/​/​ };29/​/​ const driver = new webdriver.Builder()30/​/​ .usingServer('http:/​/​localhost:4444/​wd/​hub')31/​/​ .withCapabilities(capabilities)32/​/​ .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.

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