API Testing : Check gzip compression

Verify that the API response is compressed when the client sends a request with the 'Accept-Encoding' header set to 'gzip'.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the API request has already been made and response is received23import io.restassured.http.Header;4import io.restassured.response.Response;56import java.util.ArrayList;78import static io.restassured.RestAssured.given;9import static org.hamcrest.CoreMatchers.equalTo;1011public class ApiTesting {1213 public static void main(String[] args) {14 15 /​/​Setting Accept-Encoding header to gzip16 Header acceptEncoding = new Header("Accept-Encoding", "gzip");17 18 /​/​Creating an ArrayList of headers to pass as request headers19 ArrayList<Header> headers = new ArrayList<>();20 headers.add(acceptEncoding);21 22 /​/​Making the API request with specified header23 Response response = given()24 .headers(headers)25 .when()26 .get("https:/​/​example.com/​api");2728 /​/​Verifying gzip compression29 response.then().assertThat().header("Content-Encoding", equalTo("gzip")); 30 31 /​/​Connecting to remote client with desired capabilities32 /​/​DesiredCapabilities capabilities = new DesiredCapabilities();33 /​/​capabilities.setCapability("platform", "WINDOWS");34 /​/​capabilities.setCapability("browserName", "chrome");35 /​/​capabilities.setCapability("version", "83.0");36 /​/​WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities); 37 }38}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assumptions4/​/​1. The API endpoint is already defined5/​/​2. The server side already supports gzip compression6/​/​3. The response data is expected to be in JSON format78const chai = require('chai');9const chaiHttp = require('chai-http');10const expect = chai.expect;1112chai.use(chaiHttp);1314describe('API Testing - Check gzip compression', function() {15 it('should verify that the API response is compressed when the client sends a request with the "Accept-Encoding" header set to "gzip"', function(done) {16 chai.request('http:/​/​localhost:3000')17 .get('/​api/​data')18 .set('Accept-Encoding', 'gzip')19 .end(function(err, res) {20 expect(err).to.be.null;21 expect(res).to.have.status(200);22 expect(res).to.have.header('content-encoding', 'gzip');23 expect(res).to.be.json;24 25 /​/​uncompress response data26 const zlib = require('zlib');27 zlib.gunzip(res.body, function(err, uncompressedBody) {28 expect(JSON.parse(uncompressedBody)).to.be.an('object');29 done();30 });31 });32 });33});3435/​/​Remote client with desired capabilities36/​/​const webdriver = require('selenium-webdriver');37/​/​const capabilities = {38/​/​ 'browserName': 'chrome',39/​/​ 'version': 'latest',40/​/​ 'platform': 'Windows 10',41/​/​ 'goog:chromeOptions': {42/​/​ 'args': ['--disable-infobars', 'disable-extensions', '--start-maximized']43/​/​ }44/​/​};45/​/​const driver = new webdriver.Builder()46/​/​ .usingServer('http:/​/​localhost:4444/​wd/​hub')47/​/​ .withCapabilities(capabilities)48/​/​ .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