Verify that the API correctly handles compression and decompression of data.
Language: Java
Framework: Rest assured
1// Assumptions: 2// - The API is accessible via the URL http://example.com/api.3// - The API supports gzip compression of request and response payloads.4// - We have the latest version of Rest Assured installed.56import static io.restassured.RestAssured.given;7import static org.hamcrest.Matchers.equalTo;89import java.io.ByteArrayInputStream;10import java.util.zip.GZIPInputStream;1112import io.restassured.RestAssured;13import io.restassured.response.Response;14import io.restassured.specification.RequestSpecification;1516public class DataCompressionDecompressionTest {1718 public static void main(String[] args) {19 // Set the API base URI and port number20 RestAssured.baseURI = "http://example.com";21 RestAssured.port = 80;2223 // Create the request specification with gzip compression enabled24 RequestSpecification requestSpec = given().header("Content-Encoding", "gzip")25 .header("Accept-Encoding", "gzip");2627 // Compress some sample data using the gzip algorithm28 String originalData = "Hello, world!";29 byte[] compressedData = gzipCompress(originalData.getBytes());3031 // Send a POST request to the API to upload the compressed data32 Response postResponse = requestSpec.body(compressedData).post("/api/compressed-data");3334 // Assert that the API responds with HTTP status code 201 Created35 postResponse.then().statusCode(201);3637 // Send a GET request to the API to retrieve the compressed data38 Response getResponse = requestSpec.get("/api/compressed-data");3940 // Extract the compressed data from the response41 byte[] receivedData = getResponse.getBody().asByteArray();4243 // Decompress the received data using the gzip algorithm44 String decompressedData = new String(gzipDecompress(receivedData));4546 // Assert that the decompressed data is the same as the original data47 getResponse.then().assertThat().body(equalTo(originalData));48 }4950 // Helper method to compress a byte array using the gzip algorithm51 private static byte[] gzipCompress(byte[] data) {52 try {53 ByteArrayOutputStream baos = new ByteArrayOutputStream();54 GZIPOutputStream gzip = new GZIPOutputStream(baos);55 gzip.write(data);56 gzip.close();57 return baos.toByteArray();58 } catch (IOException e) {59 throw new RuntimeException(e);60 }61 }6263 // Helper method to decompress a byte array using the gzip algorithm64 private static byte[] gzipDecompress(byte[] data) {65 try {66 ByteArrayInputStream bais = new ByteArrayInputStream(data);67 GZIPInputStream gzip = new GZIPInputStream(bais);68 ByteArrayOutputStream baos = new ByteArrayOutputStream();69 byte[] buffer = new byte[1024];70 int len;71 while ((len = gzip.read(buffer)) > 0) {72 baos.write(buffer, 0, len);73 }74 gzip.close();75 baos.close();76 return baos.toByteArray();77 } catch (IOException e) {78 throw new RuntimeException(e);79 }80 }81}
Language: Javascript
1// Mocha with Chai Assertion Library.23//Assumptions: 4//1. API send response in Gzip format for requests with Accept-Encoding header as 'gzip' 5//2. API has endpoints to compress and decompress string data 67const chai = require('chai');8const chaiHttp = require('chai-http');910chai.use(chaiHttp);1112describe('API Testing - Compression/Decompression of Data', () => {13 it('should compress and decompress data without any errors', (done) => {14 //Assumptions: 15 //1. API endpoint for compression is 'api/v1/compress'16 //2. API endpoint for decompression is 'api/v1/decompress'17 //3. Request header 'Content-Type' is set to 'text/plain'18 19 const data = "This is a sample data to compress and decompress using API";20 chai.request('http://localhost:3000') //Assuming API endpoint to be http://localhost:300021 .post('/api/v1/compress')22 .set('Content-Type', 'text/plain')23 .set('Accept-Encoding', 'gzip') //Assuming API sends response in Gzip format for requests with Accept-Encoding header as 'gzip'24 .send(data)25 .end((err, res) => {26 if (err) {27 done(err);28 }29 const compressedData = res.body;30 chai.request('http://localhost:3000') //Assuming API endpoint to be http://localhost:300031 .post('/api/v1/decompress')32 .set('Content-Type', 'text/plain')33 .set('Accept-Encoding', 'gzip') //Assuming API sends response in Gzip format for requests with Accept-Encoding header as 'gzip'34 .send(compressedData)35 .end((err, res) => {36 if (err) {37 done(err);38 }39 const decompressedData = res.body;40 chai.expect(decompressedData).to.equal(data); //Assert that decompressed data is same as original data41 done();42 });43 });44 });45}); 4647// Code to connect to remote client with desired capabilities:48//Assuming remote client is hub.example.com with Chrome browser version 94.0.4606.61 installed49// const webdriver = require("selenium-webdriver"),50// chrome = require('selenium-webdriver/chrome');5152// let chromeCapabilities = webdriver.Capabilities.chrome();5354// chromeCapabilities.set('chromeOptions', {55// 'args': ['--disable-infobars', '--start-maximized'],56// 'prefs': {57// 'profile.managed_default_content_settings.notifications': 158// },59// 'version': '94.0.4606.61'60// });6162// let driver = new webdriver.Builder()63// .withCapabilities(chromeCapabilities) 64// .usingServer('http://hub.example.com:4444/wd/hub')65// .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