API Testing : Check payload limit

Verify that the API returns an error message if the request payload exceeds the allowed limit.

Language: Java

Framework: Rest assured

copy
1import io.restassured.RestAssured;2import io.restassured.http.ContentType;3import io.restassured.response.Response;4import io.restassured.specification.RequestSpecification;5import org.junit.Assert;67import java.io.File;89public class APITest {1011 public static void main(String[] args) {1213 /​/​Assuming that the API endpoint is '/​api/​payload' and the allowed limit is 10MB14 String endpoint = "/​api/​payload";15 int allowedLimitInBytes = 10485760; /​/​10MB1617 /​/​Connecting to local driver18 RestAssured.baseURI = "http:/​/​localhost";1920 /​/​Connecting to remote client with desired capabilities - Uncomment if needed21/​/​ DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();22/​/​ WebDriver driver = null;23/​/​ try {24/​/​ driver = new RemoteWebDriver(new URL("http:/​/​REMOTE_IP_ADDRESS:4444/​wd/​hub"), desiredCapabilities);25/​/​ } catch (MalformedURLException e) {26/​/​ e.printStackTrace();27/​/​ }28/​/​ RestAssured.proxy("http:/​/​REMOTE_IP_ADDRESS:PORT").driver(driver);2930 /​/​Sending a POST request with a payload of 11MB31 File payloadFile = new File("path/​to/​11mb/​payload");32 RequestSpecification request = RestAssured.given();33 request.contentType(ContentType.BINARY);34 request.body(payloadFile);35 Response response = request.post(endpoint);3637 /​/​Verifying that the API returns an error message as the payload exceeds the allowed limit38 Assert.assertEquals("API did not return the expected status code", 413, response.getStatusCode());39 Assert.assertTrue("API did not return the expected error message", response.body().asString().contains("Payload size exceeded the allowed limit of 10MB"));40 }41}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assumptions: 4/​/​1. The API endpoint is already known and its URL is stored in a variable named "apiURL".5/​/​2. The API limit is already known and its value is stored in a variable named "apiLimit".6/​/​3. The API responds with a JSON object, containing an "error" attribute if the payload limit is exceeded.78const axios = require('axios');9const expect = require('chai').expect;1011describe('API Testing', function() {12 it('Check payload limit', async function() {13 /​/​Assuming the payload exceeds the limit by 1 byte14 const payload = 'a'.repeat(apiLimit + 1);15 16 /​/​Sending a POST request with the payload to the API endpoint17 const response = await axios.post(apiURL, {payload: payload});18 19 /​/​Expecting a "400 Bad Request" error with an error message20 expect(response.status).to.equal(400);21 expect(response.data.error).to.exist;22 });23});2425/​/​Connecting to remote client with desired capabilities26/​/​Assuming the remote client is using Selenium Grid and its URL is stored in a variable named "remoteURL".27const webdriver = require('selenium-webdriver');28const {Builder, Capabilities} = require('selenium-webdriver');2930const chromeCapabilities = Capabilities.chrome();31chromeCapabilities.set('chromeOptions', {32 'args': ['--disable-infobars', '--headless', '--disable-gpu']33});3435const driver = new Builder().withCapabilities(chromeCapabilities)36 .usingServer(remoteURL)37 .build();3839/​/​Assuming the driver is used to navigate to a login page40driver.get('https:/​/​example.com/​login');

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