API Testing : Check input validation

Verify that the API correctly handles input validation and returns the correct HTTP status code.

Language: Java

Framework: Rest assured

copy
1import org.testng.annotations.Test;2import io.restassured.RestAssured;3import static io.restassured.RestAssured.given;4import static org.hamcrest.Matchers.*;56public class InputValidation {78 @Test9 public void inputValidationTest() {10 11 /​/​Assuming the API endpoint is available at http:/​/​localhost:8080/​api12 RestAssured.baseURI = "http:/​/​localhost:8080/​api";13 14 /​/​Assuming the API endpoint requires a JSON payload with the following format15 String payload = "{ \"username\": \"testuser\", \"password\": \"testpassword\" }";16 17 given()18 .contentType("application/​json")19 .body(payload)20 .when()21 .post("/​login")22 .then()23 .statusCode(200)24 .body("message", equalTo("Login successful"));2526 /​/​Assuming the API endpoint returns a 400 Bad Request if the payload is empty27 given()28 .contentType("application/​json")29 .body("")30 .when()31 .post("/​login")32 .then()33 .statusCode(400)34 .body("message", equalTo("Invalid input"));35 }36 37 /​/​Assuming the following desired capabilities are needed to connect to a remote client38 /​*DesiredCapabilities capabilities = new DesiredCapabilities();39 capabilities.setCapability("platform", "WINDOWS");40 capabilities.setCapability("browserName", "chrome");41 capabilities.setCapability("version", "latest");42 RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);*/​43}

Language: Javascript

copy
1/​/​ Mocha + Chai + Supertest23/​/​ Assumptions: 4/​/​ - The API endpoint is located at 'https:/​/​example.com/​api'5/​/​ - The API endpoint requires an input parameter called 'inputParam'6/​/​ - The expected HTTP status code for an invalid input is 40078const request = require('supertest');9const expect = require('chai').expect;1011describe('API Input Validation Check', function () {12 const url = 'https:/​/​example.com/​api';13 const validInput = 'validInputValue';14 const invalidInput = 'invalidInputValue!@#$%';1516 describe('Valid Input', function () {17 it('should return 200 HTTP status code', function () {18 return request(url)19 .get(`/​?inputParam=${validInput}`)20 .expect(200);21 });22 });2324 describe('Invalid Input', function () {25 it('should return 400 HTTP status code', function () {26 return request(url)27 .get(`/​?inputParam=${invalidInput}`)28 .expect(400);29 });30 });31});3233/​/​ To connect to remote client with desired capabilities:34/​/​ - set the desired capabilities object35/​/​ - use webdriverio to create a remote driver with the desired capabilities3637const { remote } = require('webdriverio');3839const desiredCapabilities = {40 /​/​ add desired capabilities here41};4243const remoteClient = remote({44 /​/​ add remote client options here45 capabilities: desiredCapabilities46});

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