Verify that the API correctly handles cross-site scripting (XSS) prevention and returns the correct HTTP status code and error message.
Language: Java
Framework: Rest assured
1//Assuming the API is already deployed and the required endpoints are available. 2//Assuming the API endpoint to test for XSS prevention is /api/xssprevention3//Assuming the expected HTTP status code on successful prevention of XSS is 200 4//Assuming the expected error message on unsuccessful prevention of XSS is "XSS Prevention failed"567import org.junit.Test;8import static io.restassured.RestAssured.*;9import static org.hamcrest.Matchers.*;1011public class XssPreventionTest {1213 @Test14 public void testXssPrevention() {15 given()16 .param("input", "<script>alert('XSS')</script>")17 .when()18 .post("/api/xssprevention")19 .then()20 .assertThat().statusCode(200)21 .assertThat().body(is("XSS prevention succeeded"));2223 given()24 .param("input", "<script>alert('Another XSS')</script>")25 .when()26 .post("/api/xssprevention")27 .then()28 .assertThat().statusCode(400)29 .assertThat().body(is("XSS Prevention failed"));30 }3132 //Code to connect to remote client with desired capabilities33 //Assuming the remote client address is "http://192.168.1.1:4444/wd/hub"34 //Assuming the desired browser is Chrome version 9035 public void connectToRemoteClient() {36 DesiredCapabilities capabilities = new DesiredCapabilities();37 capabilities.setBrowserName("chrome");38 capabilities.setVersion("90");39 WebDriver driver = new RemoteWebDriver(new URL("http://192.168.1.1:4444/wd/hub"), capabilities);40 }4142}
Language: Javascript
1// Mocha with Chai assertion library and Supertest to send HTTP requests.23// Assumptions:4// - Base URL for the API is known and stored in baseUrl variable5// - API endpoint for XSS prevention is known and stored in xssPreventionEndpoint variable6// - API returns HTTP status code 403 and error message "Forbidden" for blocked input7// - The input parameter for the API's XSS prevention is called "input"89const assert = require('chai').assert;10const request = require('supertest');11const baseUrl = 'http://localhost:3000';12const xssPreventionEndpoint = '/xss-prevention';1314describe('XSS Prevention API Testing', function() {15 it('should block input with XSS attack code', function(done) {16 const xssInput = '<script>alert("XSS!");</script>';17 request(baseUrl)18 .post(xssPreventionEndpoint)19 .send({input: xssInput})20 .expect(403)21 .end(function(err, res) {22 if (err) return done(err);23 assert.equal(res.text, 'Forbidden');24 done();25 });26 });2728 // Uncomment the following code to connect to remote client with desired capabilities29 /*30 const webdriver = require('selenium-webdriver');31 const remoteUrl = 'http://localhost:4444/wd/hub';32 const desiredCapabilities = webdriver.Capabilities.chrome();33 const driver = new webdriver.Builder()34 .usingServer(remoteUrl)35 .withCapabilities(desiredCapabilities)36 .build();37 */3839});40
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.
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.
Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.
Start Free Testing