API Testing : Check SQL injection handling

Verify that the API correctly handles SQL injection attacks and returns the correct HTTP status code.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the API endpoint for SQL injection handling is https:/​/​example.com/​api23import org.junit.jupiter.api.Test;4import io.restassured.RestAssured;5import static io.restassured.RestAssured.given;6import io.restassured.response.Response;78public class APITest {9 10 @Test11 public void testSQLInjectionHandling() {12 RestAssured.baseURI = "https:/​/​example.com/​api";13 14 /​/​Add the following code to connect to remote client with desired capabilities15 /​*DesiredCapabilities caps = new DesiredCapabilities();16 caps.setCapability("platformName", "Android");17 caps.setCapability("deviceName", "emulator-5554");18 caps.setCapability("automationName", "uiautomator2");19 caps.setCapability("appPackage", "com.example");20 caps.setCapability("appActivity", ".MainActivity");21 RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​127.0.0.1:4723/​wd/​hub"), caps);22 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);*/​23 24 /​/​Assuming the API requires a valid access token for authentication25 String accessToken = "valid_access_token";26 String sqlInjectionRequestBody = "{ \"id\" : \"1\"; DROP TABLE users; --\" }";27 28 /​/​Sending request with SQL Injection payload29 Response response = given().auth().oauth2(accessToken).body(sqlInjectionRequestBody).when().post("/​sql-injection-handling");30 31 /​/​Asserting the response status code is correct32 response.then().assertThat().statusCode(400);33 }34}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assuming the API endpoint is http:/​/​example.com/​api4/​/​Creating a Mocha test suite5describe('API testing', function() {67/​/​Creating a test case for checking SQL injection handling8 it('Should handle SQL injection attacks and return the correct HTTP status code', function(done) {9 10 /​/​Assuming the API endpoint requires an authentication token11 const authToken = 'Insert authentication token here';12 13 /​/​Assuming the malicious input for injection14 const userInput = "'; DROP TABLE Users; --";15 16 /​/​Assuming the endpoint for the SQL injection check17 const endpoint = 'http:/​/​example.com/​api/​user';18 19 /​/​Assuming the payload for the API call20 const payload = {21 user: userInput22 };23 24 /​/​Assuming the expected HTTP status code for correctly handling SQL injection25 const expectedStatusCode = 400;26 27 /​/​Using local driver to create a request28 const request = require('request');29 30 /​/​Adding commented code to connect to remote client with desired capabilities31 /​/​ const webdriver = require('selenium-webdriver');32 /​/​ const capabilities = webdriver.Capabilities.chrome();33 /​/​ capabilities.set('chromeOptions', {args: ['--headless']});34 /​/​ const driver = new webdriver.Builder()35 /​/​ .usingServer('http:/​/​localhost:4444/​wd/​hub')36 /​/​ .withCapabilities(capabilities)37 /​/​ .build();38 39 /​/​Constructing the API call with the payload and authentication token40 request.post(endpoint, {json: payload, headers: {'Authorization': authToken}}, function(error, response, body) {41 42 /​/​Asserting the actual HTTP status code is the expected status code43 const actualStatusCode = response.statusCode;44 chai.expect(actualStatusCode).to.equal(expectedStatusCode);45 46 /​/​Ending the test case47 done();48 });49 });50});

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