API Testing : Check SQL injection prevention

Verify that the API correctly handles SQL injection prevention and returns the correct HTTP status code and error message.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the API url is "https:/​/​www.example.com/​api"2/​/​Assuming the API endpoint for SQL injection prevention check is "/​prevent-sql-injection"34import org.junit.Test;5import io.restassured.RestAssured;6import io.restassured.response.Response;7import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;8import static org.hamcrest.Matchers.equalTo;910public class SQLInjectionTest {1112 @Test13 public void testSQLInjectionPrevention() {14 15 /​/​Assuming valid input for the API call16 String validInput = "This is a valid input without any SQL injection code.";1718 /​/​Assuming SQL injection string to test19 String sqlInjectionString = "'; DROP TABLE users; --";2021 /​/​Assuming expected HTTP status code and error message for SQL injection prevention22 int expectedStatusCode = 400;23 String expectedErrorMessage = "SQL injection detected in request.";2425 /​/​Performing API call with valid input26 Response response = RestAssured.given()27 .param("input", validInput)28 .when()29 .post("https:/​/​www.example.com/​api/​prevent-sql-injection");3031 /​/​Validating HTTP status code32 response.then().statusCode(expectedStatusCode);3334 /​/​Validating error message35 response.then().body("error", equalTo(expectedErrorMessage));3637 /​/​Validating JSON schema of response38 response.then().assertThat().body(matchesJsonSchemaInClasspath("schema.json"));39 40 /​*41 * Uncomment below code to run test on remote client with desired capabilities42 * Assuming the URL of the remote client is "http:/​/​www.example.com:4444/​wd/​hub"43 *44 DesiredCapabilities capabilities = new DesiredCapabilities();45 capabilities.setBrowserName("chrome");46 capabilities.setVersion("91.0");47 capabilities.setCapability("enableVNC", true);48 capabilities.setCapability("enableVideo", false);4950 RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​www.example.com:4444/​wd/​hub"), capabilities);51 driver.get("https:/​/​www.example.com/​api/​prevent-sql-injection");52 */​53 }54}

Language: Javascript

copy
1/​/​Mocha + Chai + Supertest23/​/​Assumptions:4/​/​1. API Endpoint URL: http:/​/​localhost:3000/​users5/​/​2. HTTP status code for successful request: 2006/​/​3. HTTP status code for malformed request: 4007/​/​4. Error message for malformed request: "Invalid input"89const request = require('supertest');10const app = require('../​app');1112describe('API Test: SQL Injection Prevention', () => {13 it('should prevent SQL injection and return correct HTTP status code and error message', (done) => {14 const maliciousInput = "'; DROP TABLE users;"15 request(app)16 .post('/​users')17 .send({ name: 'Test User', username: maliciousInput, password: 'testpassword' })18 .expect(400)19 .expect((res) => {20 chai.expect(res.body).to.have.property('message').to.equal('Invalid input');21 })22 .end(done);23 });24});

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