API Testing : Check request method

Verify that the API request method is correct (e.g. GET, POST, PUT, DELETE).

Language: Java

Framework: Rest assured

copy
1import org.junit.Test;2import io.restassured.RestAssured;3import io.restassured.response.Response;4import io.restassured.specification.RequestSpecification;56public class APITest {78 @Test9 public void testRequestMethod() {10 String API_URL = "http:/​/​example.com/​api";11 String requestMethod = "GET";12 RestAssured.baseURI = API_URL;13 RequestSpecification request = RestAssured.given();1415 Response response = request.get();1617 if (response.getStatusCode() == 200) {18 String actualRequestMethod = response.getRequestMethod();19 if (!actualRequestMethod.equalsIgnoreCase(requestMethod)) {20 System.out.println("Request method is incorrect." + "\nExpected method: " + requestMethod + "\nActual method: " + actualRequestMethod);21 }22 } else {23 System.out.println("Error: API returned status code " + response.getStatusCode());24 }25 }26}2728/​/​Assumptions:29/​/​URL of the API is "http:/​/​example.com/​api"30/​/​Request method is expected to be "GET"31/​/​Local HTTP client is used for sending requests by default32/​/​Code for connecting to remote client with desired capabilities:33/​*34DesiredCapabilities capabilities = new DesiredCapabilities();35capabilities.setPlatform(Platform.WINDOWS);36capabilities.setBrowserName("chrome");37capabilities.setVersion("83.0.4103.97");3839RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);40*/​

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​ Assumptions: 4/​/​ 1. The API endpoint URL is already known and accessible.5/​/​ 2. The API request methods are standard and correct for the endpoint.67const chai = require('chai');8const chaiHttp = require('chai-http');9const expect = chai.expect;1011chai.use(chaiHttp);1213describe('API Testing - Request Method', function() {14 it('should return correct request method', function(done) {15 16 /​/​ Assumption 3: GET method is used for this test case17 /​/​ Change the method accordingly for testing other methods18 19 chai.request('http:/​/​api.example.com')20 .get('/​endpoint')21 .end(function(err, res) {22 expect(res).to.have.status(200);23 expect(res).to.have.header('content-type', 'application/​json');24 expect(res).to.have.property('method', 'GET');25 done();26 });27 });28 29 /​/​ Uncomment below code to connect with remote client using desired capabilities30 31 /​/​ const webdriver = require('selenium-webdriver');32 /​/​ const remote = require('selenium-webdriver/​remote');33 /​/​ const caps = new webdriver.Capabilities();34 /​/​ caps.set('browserName', 'chrome');35 /​/​ const driver = new webdriver.Builder().withCapabilities(caps).usingServer('http:/​/​remote.client.com:4444/​wd/​hub')36 /​/​ .setFirefoxOptions(new firefox.Options().headless()).build();37});

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