API Testing : Check server-side validation

Verify that the API correctly handles server-side validation and returns the correct HTTP status code and error message.

Language: Java

Framework: Rest assured

copy
1import org.junit.Test;2import static io.restassured.RestAssured.*;3import static org.hamcrest.Matchers.*;45public class APITesting {67 @Test8 public void testServerSideValidation() {9 /​/​Assuming the base URL of the API10 String baseURL = "https:/​/​example.api.com";11 12 /​/​Assuming the endpoint for server-side validation13 String endpoint = "/​server-side-validation";14 15 /​/​Assuming the request body with an invalid value, 16 /​/​which should trigger the server-side validation error17 String requestBody = "{ \"value\": \"invalidValue\" }";18 19 /​/​Assuming the expected error message20 String expectedErrorMessage = "Invalid value provided";21 22 given()23 .contentType("application/​json")24 .body(requestBody)25 .when()26 .post(baseURL + endpoint)27 .then()28 .statusCode(400)29 .body("error", equalTo(expectedErrorMessage));30 31 /​/​Assuming the remote client with desired capabilities32 /​/​String remoteURL = "http:/​/​myserver:4444/​wd/​hub";33 /​/​DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox();34 /​/​WebDriver driver = new RemoteWebDriver(new URL(remoteURL), desiredCapabilities);35 }36}

Language: Javascript

copy
1/​/​ Mocha + Chai23/​/​Assumptions:4/​/​1. API endpoint is available.5/​/​2. Request payload is provided in JSON format.6/​/​3. Expected HTTP status code is 400.7/​/​4. Expected error message is "Invalid request body".89const request = require('request');10const chai = require('chai');11const expect = chai.expect;1213describe('API Testing - Server-Side Validation', function () {1415 const url = 'http:/​/​localhost:3000/​api/​authenticate';16 /​/​replace url with the actual endpoint URL1718 it('should return 400 status code and "Invalid request body" error message', function (done) {1920 const requestBody = {username: 'testuser', password: 'testpass'};21 /​/​replace requestBody with the actual request payload2223 const options = {24 url: url,25 headers: {26 'Content-Type': 'application/​json'27 },28 body: JSON.stringify(requestBody)29 };3031 request.post(options, function (error, response, body) {32 expect(response.statusCode).to.equal(400);33 expect(body).to.equal('Invalid request body');34 done();35 })36 });3738});3940/​/​Remote client with desired capabilities:41/​/​replace desiredCapabilities with the actual capabilities4243/​*const webdriver = require('selenium-webdriver');44const capabilities = webdriver.Capabilities.chrome();45capabilities.set('chromeOptions', {46 'args': ['--no-sandbox']47});4849const driver = new webdriver.Builder()50 .forBrowser('chrome')51 .usingServer('http:/​/​localhost:4444/​wd/​hub')52 .withCapabilities(desiredCapabilities)53 .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