API Testing : Check input sanitization

Verify that the API correctly handles input sanitization and returns the correct HTTP status code and error message.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the API endpoint is already available and functional 23import org.testng.annotations.Test;4import io.restassured.RestAssured;5import static io.restassured.RestAssured.given;6import io.restassured.response.Response;78public class APITest {910 @Test11 public void testInputSanitization() {12 13 /​/​Connecting to the remote client:14 /​/​RestAssured.baseURI = "{Remote client URL}";15 /​/​RestAssured.port = {Remote client port};16 /​/​RestAssured.basePath = "{Remote client base path}";17 /​/​RestAssured.config = newRestAssuredConfig().getHttpClientConfig().setParam("http.connection.stalecheck", false);18 /​/​RestAssured.proxy("{Remote client IP address}", {Remote client port number});19 20 /​/​Assuming the API endpoint is "https:/​/​example.com/​api"21 Response response = given()22 .when()23 .post("https:/​/​example.com/​api")24 .then()25 .assertThat()26 .statusCode(400)27 .extract().response();28 29 /​/​Add more assertions for error message payload verification if required30 /​/​Assert.assertEquals(response.body().asString(), "Expected string");31 }32}

Language: Javascript

copy
1/​/​Mocha and Chai.23/​/​ Assumption: The API endpoint URL to test is "http:/​/​example.com/​api/​endpoint"4/​/​ Assumption: Input sanitization means removing any unwanted characters or HTML tags from input fields56const expect = require('chai').expect;7const axios = require('axios');89const endpointUrl = 'http:/​/​example.com/​api/​endpoint';1011describe('API input sanitization', function() {12 it('should return HTTP status code 200 and success message on valid input', async function() {13 const response = await axios.post(endpointUrl, {14 input: "valid input"15 });16 expect(response.status).to.equal(200);17 expect(response.data.message).to.equal("Input sanitized successfully");18 });1920 it('should return HTTP status code 400 and error message on invalid input containing HTML tags', async function() {21 const response = await axios.post(endpointUrl, {22 input: "<script>malicious code</​script>"23 });24 expect(response.status).to.equal(400);25 expect(response.data.error).to.equal("Invalid input format");26 });2728 it('should return HTTP status code 400 and error message on invalid input containing special characters', async function() {29 const response = await axios.post(endpointUrl, {30 input: "inv@lid #nput"31 });32 expect(response.status).to.equal(400);33 expect(response.data.error).to.equal("Invalid input format");34 });35});

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