API Testing : Check disallowed method

Verify that the API returns an error message if the request method is not allowed for the resource.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the API base URL is "https:/​/​example.com/​api/​"2/​/​Assuming the resource endpoint is "/​resource"3/​/​Assuming the allowed request method is "POST"45import org.testng.annotations.Test;6import io.restassured.RestAssured;7import static io.restassured.RestAssured.*;8import static org.hamcrest.Matchers.*;910public class APITestCase {11 12 @Test13 public void checkDisallowedMethod() {14 15 /​/​Connect to the API base URL16 RestAssured.baseURI = "https:/​/​example.com/​api/​";17 18 /​/​Check the API response when an allowed method is used ("POST")19 given()20 .when()21 .post("/​resource")22 .then()23 .statusCode(200);24 25 /​/​Check the API response when a disallowed method is used ("GET")26 given()27 .when()28 .get("/​resource")29 .then()30 .statusCode(405)31 .body("error", equalTo("Method Not Allowed"));32 33 /​/​Assuming the following are the desired capabilities for a remote client34 /​/​DesiredCapabilities capabilities = new DesiredCapabilities();35 /​/​capabilities.setCapability("platform", "Windows 10");36 /​/​capabilities.setCapability("browserName", "Chrome");37 /​/​capabilities.setCapability("version", "latest");38 39 /​/​Connect to the API base URL using a remote client with desired capabilities40 /​/​RestAssured.baseURI = "https:/​/​example.com/​api/​";41 /​/​RestAssured.remote("http:/​/​localhost:4444/​wd/​hub", capabilities);42 43 /​/​Check the API response when an allowed method is used ("POST")44 /​/​given()45 /​/​.when()46 /​/​.post("/​resource")47 /​/​.then()48 /​/​.statusCode(200);49 50 /​/​Check the API response when a disallowed method is used ("GET")51 /​/​given()52 /​/​.when()53 /​/​.get("/​resource")54 /​/​.then()55 /​/​.statusCode(405)56 /​/​.body("error", equalTo("Method Not Allowed"));57 }58}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​importing necessary libraries4const chai = require('chai');5const chaiHttp = require('chai-http');6const expect = chai.expect;78/​/​connecting to the remote client with desired capabilities9/​/​uncomment below code and replace with appropriate details10/​*11const webdriver = require('selenium-webdriver');12const {Builder, Capabilities} = require('selenium-webdriver');13const capabilities = Capabilities.chrome();1415let driver = new Builder()16 .withCapabilities(capabilities)17 .usingServer('http:/​/​remoteclienturl.com/​wd/​hub')18 .build();19*/​2021chai.use(chaiHttp);2223describe('API Testing', () => {24 it('Check disallowed method', (done) => {25 chai.request('http:/​/​apiurl.com')26 .post('/​resource')27 .end((err, res) => {28 expect(res).to.have.status(405);29 expect(res.body.message).to.equal('Method Not Allowed');30 done();31 });32 });33});/​/​

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