Verify that the API correctly handles output sanitization and returns the correct HTTP status code and error message.
Language: Java
Framework: Rest assured
1//Assumptions: 2//1. API endpoint is already known and available for testing3//2. The expected HTTP status code for the test case is 2004//3. The expected error message for the test case is "Invalid request"56import static io.restassured.RestAssured.*;7import static io.restassured.matcher.RestAssuredMatchers.*;8import static org.hamcrest.Matchers.*;910public class APITest {11 12 @Test13 public void testOutputSanitization() {14 //Set up the API endpoint15 String url = "https://example.com/api/v1/output";16 17 //Create the request body with special characters that should be sanitized18 String requestBody = "{\"name\": \"<script>alert(1)</script>\", \"email\": \"<img src=x onerror=alert(1)>\"}";19 20 //Send the request to the API endpoint and verify the output sanitization was performed correctly21 given().22 body(requestBody).23 when().24 post(url).25 then().26 assertThat().27 statusCode(200).28 body("message", equalTo("Output sanitization successful."));29 30 //Check the error message when the request body contains invalid characters that should be sanitized31 requestBody = "{\"name\": \"<script>alert(1)</script>\", \"email\": \"<img src=\"x\" onerror=\"alert(1)\">\"}";32 33 given().34 body(requestBody).35 when().36 post(url).37 then().38 assertThat().39 statusCode(400).40 body("error", equalTo("Invalid request"));41 42 //Connect to remote client with desired capabilities 43 DesiredCapabilities capabilities = new DesiredCapabilities();44 capabilities.setBrowserName("Chrome");45 capabilities.setVersion("83.0");46 capabilities.setCapability("enableVNC", true);47 48 WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wb/hub"), capabilities);49 50 //Use local driver51 WebDriver driver = new ChromeDriver();52 }53}
Language: Javascript
1//Mocha and Chai.23//Assumptions: 4//1. The API has an endpoint '/api/sanitize-output'5//2. The API returns a JSON response with a 'status' attribute and an 'error' attribute6//3. The 'status' attribute can be either 'success' or 'failure'7//4. The 'error' attribute is a string with the error message when 'status' is 'failure'8//5. The API has already been deployed and is running locally on port 3000910const { expect } = require('chai');11const request = require('request');1213describe('Output Sanitization API Test', () => {14 it('should return HTTP status 200 and "success" status when output is sanitized', (done) => {15 const options = {16 url: 'http://localhost:3000/api/sanitize-output',17 method: 'POST',18 json: {19 input: 'This is a <script>malicious code!</script>'20 }21 };22 23 request(options, (error, response, body) => {24 expect(response.statusCode).to.equal(200);25 expect(body.status).to.equal('success');26 done();27 });28 });29 30 it('should return HTTP status 400 and error message when output is not sanitized', (done) => {31 const options = {32 url: 'http://localhost:3000/api/sanitize-output',33 method: 'POST',34 json: {35 input: 'This is a <script>malicious code!',36 }37 };3839 request(options, (error, response, body) => {40 expect(response.statusCode).to.equal(400);41 expect(body.status).to.equal('failure');42 expect(body.error).to.equal('Output is not sanitized');43 done();44 });45 });46});4748// Assuming remote client with desired capabilities is already set up49// Uncomment below code in order to run test on remote client50/*51const { remote } = require('webdriverio');52const options = {53 path: '/wd/hub',54 port: 4444,55 logLevel: 'error',56 capabilities: {57 browserName: 'chrome',58 acceptInsecureCerts: true59 }60};6162(async () => {63 const browser = await remote(options);6465 describe('Output Sanitization API Test', () => {66 it('should return HTTP status 200 and "success" status when output is sanitized', async () => {67 await browser.url('http://localhost:3000/api/sanitize-output');68 const input = await browser.$('#input');69 await input.setValue('This is a <script>malicious code!</script>');70 const button = await browser.$('#submit');71 await button.click();7273 const statusLabel = await browser.$('#status');74 expect(await statusLabel.getText()).to.equal('success');75 });7677 it('should return HTTP status 400 and error message when output is not sanitized', async () => {78 await browser.url('http://localhost:3000/api/sanitize-output');79 const input = await browser.$('#input');80 await input.setValue('This is a <script>malicious code!');81 const button = await browser.$('#submit');82 await button.click();8384 const statusLabel = await browser.$('#status');85 expect(await statusLabel.getText()).to.equal('failure');8687 const errorLabel = await browser.$('#error');88 expect(await errorLabel.getText()).to.equal('Output is not sanitized');89 });90 });9192 await browser.deleteSession();93})(); 94*/
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