Verify that the API returns the correct HTTP status code for forbidden requests (e.g. HTTP 403 Forbidden).
Language: Java
Framework: Rest assured
1//Assuming the API endpoint is available and can accept incoming requests23import org.junit.Test;4import static io.restassured.RestAssured.*;5import static org.hamcrest.Matchers.*;67public class ForbiddenRequestTest {89 @Test10 public void testForbiddenRequest() {11 given()12 .when()13 .get("https://exampleapi.com/forbidden_endpoint")14 .then()15 .statusCode(403);16 }17 18 //Uncomment below to connect to remote client with desired capabilities19 /*20 public void createRemoteClient() {21 DesiredCapabilities capabilities = new DesiredCapabilities();22 capabilities.setBrowserName("Chrome"); //Assuming desired browser is chrome23 capabilities.setVersion("91.0"); //Assuming desired version is 91.024 capabilities.setCapability("platform", "WIN10"); //Assuming desired platform is Windows 1025 26 RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);27 driver.get("https://exampleapi.com/forbidden_endpoint");28 int statusCode = driver.manage().getStatusCode();29 30 //Assert HTTP status code31 assert(statusCode == 403);32 }33 */34}
Language: Javascript
1// Mocha and Chai.23//Assuming the base URL of the API is "api.example.com"4//And the endpoint for forbidden requests is "/forbidden"56const { expect } = require('chai');7const request = require('request');89describe('API Forbidden Requests Testing', () => {10 it('should return HTTP status code 403 for forbidden requests', (done) => {11 const options = {12 url: 'http://api.example.com/forbidden',13 method: 'GET',14 headers: {15 'Content-Type': 'application/json'16 }17 };1819 //Local driver configuration20 const server = request(options, (error, response) => {21 expect(response.statusCode).to.equal(403);22 done();23 });2425 //Remote driver configuration26 //Uncomment below code to connect to remote client with desired capabilities27 //const capabilities = {28 // browserName: 'chrome',29 // acceptInsecureCerts: true30 //};31 //const remoteServer = new RemoteWebDriver('http://localhost:4444/wd/hub', capabilities);32 //remoteServer.get('http://api.example.com/forbidden');33 //remoteServer.wait(() => {34 // expect(remoteServer.getCurrentUrl()).to.equal('http://api.example.com/forbidden');35 // expect(remoteServer.getTitle()).to.equal('Forbidden Page');36 //}, 5000);37 //remoteServer.quit();38 });39});4041//Note: The above code is just an example, please modify it according to the requirements and environment of your application.
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