API Testing : Check small payloads

Verify that the API correctly handles small payloads and returns the correct HTTP status code and error message.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the API endpoint is https:/​/​exampleapi.com/​smallpayloads23import io.restassured.RestAssured;4import io.restassured.response.Response;5import io.restassured.specification.RequestSpecification;67public class SmallPayloadsTest {8 9 public static void main(String[] args) {10 11 /​/​Setting up the base URI for the API12 RestAssured.baseURI = "https:/​/​exampleapi.com";13 14 /​/​Creating a new request specification for the API15 RequestSpecification httpRequest = RestAssured.given();16 17 /​/​Sending a GET request to the API with a small payload of 10 characters18 Response response = httpRequest.get("/​smallpayloads?payload=abcdefghij");19 20 /​/​Verifying that the correct HTTP status code (200) is returned21 response.then().statusCode(200);22 23 /​/​Verifying that the correct error message is returned for small payloads24 response.then().body("errorMessage", equalTo("Payload is too small"));25 26 /​/​Uncomment the below code to run the test on a remote client with desired capabilities27 /​*28 DesiredCapabilities capabilities = new DesiredCapabilities();29 capabilities.setBrowserName("chrome");30 RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);31 driver.get("https:/​/​exampleapi.com");32 /​/​carry out API testing as above33 driver.quit();34 */​35 }36 37}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assuming the API URL is https:/​/​example.com/​api/​ and the payload is {"name": "John"}45const chai = require('chai');6const chaiHttp = require('chai-http');7const expect = chai.expect;89chai.use(chaiHttp);1011describe('API Test Suite', () => {12 it('should handle small payloads and return correct status code and error message', async () => {13 const res = await chai.request('https:/​/​example.com/​api/​')14 .post('/​')15 .send({"name": "John"});16 17 expect(res).to.have.status(200);18 expect(res).to.be.json;19 expect(res.body).to.have.property('message').eql('Payload received successfully');20 21 /​/​ Commented code to connect to remote client with desired capabilities22 /​/​ var webdriver = require('selenium-webdriver');23 /​/​ var capabilities = {'browserName': 'chrome', 'platform': 'WINDOWS'};24 /​/​ var remoteUrl = 'http:/​/​192.168.1.100:4444/​wd/​hub';25 /​/​ var driver = new webdriver.Builder().usingServer(remoteUrl).withCapabilities(capabilities).build();26 });27});

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