API Testing : Verify payload size

Verify that the API response payload size is within acceptable limits.

Language: Java

Framework: Rest assured

copy
1import io.restassured.RestAssured;2import io.restassured.response.Response;3import org.junit.Test;45public class APITest {6 7 @Test8 public void verifyPayloadSize() {9 /​/​Assuming the API endpoint is https:/​/​example.com/​api/​endpoint10 /​/​Assuming the acceptable payload size is 10 MB11 RestAssured.baseURI = "https:/​/​example.com";12 Response response = RestAssured.get("/​api/​endpoint");13 int payloadSize = response.getBody().asByteArray().length;14 assert(payloadSize <= 10485760);/​/​10MB is 10485760 bytes15 }1617 /​/​Code to connect to a remote client with desired capabilities18 /​/​@Test19 /​/​public void connectToRemoteClient() {20 /​/​ DesiredCapabilities capabilities = new DesiredCapabilities();21 /​/​ capabilities.setCapability("browserName", "chrome");22 /​/​ capabilities.setCapability("platform", "WINDOWS");23 /​/​ WebDriver driver = new RemoteWebDriver(new URL("http:/​/​127.0.0.1:4444/​wd/​hub"), capabilities);24 /​/​ driver.get("https:/​/​www.example.com");25 /​/​}26}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assumptions:4/​/​1. The API endpoint is already known.5/​/​2. The acceptable payload size limit is already defined as a constant.6/​/​3. The API's response is already in JSON format.78const request = require('request');9const expect = require('chai').expect;1011const API_ENDPOINT = 'http:/​/​example.com/​api';12const ACCEPTABLE_PAYLOAD_SIZE_LIMIT = 50000; /​/​bytes1314describe('API Payload Size Verification', () => {15 it('should return payload size within acceptable limit', (done) => {16 request.get(API_ENDPOINT, (error, response, body) => {17 const payloadSize = Buffer.byteLength(body, 'utf8');18 expect(payloadSize).to.be.below(ACCEPTABLE_PAYLOAD_SIZE_LIMIT);19 done();20 });21 });22});2324/​/​Connecting to remote client with desired capabilities25/​/​Assumptions:26/​/​1. The remote client's IP address is already known.27/​/​2. The remote client's port number is already known.28/​/​3. The desired capability "browserName" is already defined as "chrome".2930const WEB_DRIVER_ENDPOINT = 'http:/​/​192.168.1.100:4444/​wd/​hub';3132const {Builder} = require('selenium-webdriver');33const {Capabilites} = require('selenium-webdriver/​lib/​capabilities');3435const capabilities = Capabilities.chrome();36const options = {37 'args': ['--start-maximized']38};3940capabilities.set('chromeOptions', options);4142const driver = await new Builder()43 .usingServer(WEB_DRIVER_ENDPOINT)44 .withCapabilities(capabilities)45 .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