Verify that the API returns the correct HTTP status code for requests that are not supported (e.g. HTTP 405 Method Not Allowed).
Language: Java
Framework: Rest assured
1//Assuming the base URI of the API2String baseURI = "http://api.example.com";34//Assuming the endpoint that doesn't support a certain HTTP method5String unsupportedEndpoint = "/unsupported";67//Assuming the HTTP method that is not supported by the endpoint8String unsupportedHTTPMethod = "PATCH";910//Assuming the expected HTTP status code for unsupported requests11int expectedStatusCode = 405;1213//Creating a request using Rest Assured14RequestSpecification request = RestAssured.given();1516//Specifying the unsupported HTTP method in the request17request.method(unsupportedHTTPMethod);1819//Sending the request to the unsupported endpoint20Response response = request.when().baseUri(baseURI).basePath(unsupportedEndpoint).send();2122//Asserting that the response contains the expected status code23response.then().statusCode(expectedStatusCode);2425//Uncomment the following code to connect to a remote client with desired capabilities26//DesiredCapabilities capabilities = new DesiredCapabilities();27//capabilities.setCapability("browserName", "chrome");28//WebDriver driver = new RemoteWebDriver(new URL("http://remoteclient:4444/wd/hub"), capabilities);29//driver.quit(); //end the session after testing is done
Language: Javascript
1// Mocha and Chai23//Assuming API Endpoint URL4const apiEndpoint = "https://example.com/api";56//Assuming Unsupported API request7const requestUrl = apiEndpoint + "/unsupported";89describe("API Testing - Check unsupported requests", () => {1011 it("Should return 405 Method Not Allowed status code", () => {12 13 //Assuming API response should contain status code14 const response = fetch(requestUrl);15 expect(response).to.have.property('status').that.equals(405);1617 });18});
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