Verify that the API correctly handles various types of requests such as GET, POST, PUT, DELETE, OPTIONS, HEAD and PATCH.
Language: Java
Framework: Rest assured
1import org.testng.annotations.Test;2import io.restassured.RestAssured;3import io.restassured.response.Response;4import static org.hamcrest.Matchers.*;5import static io.restassured.RestAssured.given;67@Test8public void testRequestTypes() {9 //Assuming API is available at: https://exampleapi.com10 RestAssured.baseURI = "https://exampleapi.com";11 12 //GET request13 Response getResponse = given().when().get("/users");14 getResponse.then().statusCode(200).body("size()",greaterThan(0));15 16 //POST request17 Response postResponse = given().contentType("application/json").body("{\"name\":\"John\",\"age\":30}").when().post("/users");18 postResponse.then().statusCode(201);19 20 //PUT request21 Response putResponse = given().contentType("application/json").body("{\"name\":\"John\",\"age\":35}").when().put("/users/1");22 putResponse.then().statusCode(200);23 24 //DELETE request25 Response deleteResponse = given().when().delete("/users/1");26 deleteResponse.then().statusCode(200);27 28 //OPTIONS request29 Response optionsResponse = given().when().options("/users");30 optionsResponse.then().statusCode(200).body(notNullValue());31 32 //HEAD request33 Response headResponse = given().when().head("/users");34 headResponse.then().statusCode(200);35 36 //PATCH request37 Response patchResponse = given().contentType("application/json").body("{\"name\":\"John\",\"city\":\"New York\"}").when().patch("/users/1");38 patchResponse.then().statusCode(200);39 40 //Code for remote client with desired capabilities:41 //Assuming remote client with capabilities IP address: 192.168.0.1 and port: 444442 /*DesiredCapabilities capabilities = new DesiredCapabilities();43 capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");44 capabilities.setCapability(CapabilityType.VERSION, "64");45 capabilities.setCapability(CapabilityType.PLATFORM, "Windows 10");46 47 WebDriver driver = new RemoteWebDriver(new URL("http://192.168.0.1:4444/wd/hub"), capabilities);*/48}
Language: Javascript
1// Mocha + Chai + SuperTest23// Assumptions:4// The API endpoint URL is http://example.com/api/v1.5// The API supports all the request types mentioned in the test case.6// The expected response status code for each request type is 200.7// The expected response content type is JSON.89// Test code:10const request = require('supertest');11const app = require('../app'); // Assuming the API is implemented in this app.js file12const expect = require('chai').expect;1314describe('API Testing', function() {15 it('should handle GET request', function(done) {16 request(app)17 .get('/api/v1')18 .expect('Content-Type', /json/)19 .expect(200)20 .end(function(err, res) {21 if (err) return done(err);22 // Add assertions for response data here23 done();24 });25 });2627 it('should handle POST request', function(done) {28 request(app)29 .post('/api/v1')30 .expect('Content-Type', /json/)31 .expect(200)32 .end(function(err, res) {33 if (err) return done(err);34 // Add assertions for response data here35 done();36 });37 });3839 it('should handle PUT request', function(done) {40 request(app)41 .put('/api/v1')42 .expect('Content-Type', /json/)43 .expect(200)44 .end(function(err, res) {45 if (err) return done(err);46 // Add assertions for response data here47 done();48 });49 });5051 it('should handle DELETE request', function(done) {52 request(app)53 .delete('/api/v1')54 .expect('Content-Type', /json/)55 .expect(200)56 .end(function(err, res) {57 if (err) return done(err);58 // Add assertions for response data here59 done();60 });61 });6263 it('should handle OPTIONS request', function(done) {64 request(app)65 .options('/api/v1')66 .expect('Content-Type', /json/)67 .expect(200)68 .end(function(err, res) {69 if (err) return done(err);70 // Add assertions for response data here71 done();72 });73 });7475 it('should handle HEAD request', function(done) {76 request(app)77 .head('/api/v1')78 .expect(200)79 .end(function(err, res) {80 if (err) return done(err);81 done();82 });83 });8485 it('should handle PATCH request', function(done) {86 request(app)87 .patch('/api/v1')88 .expect('Content-Type', /json/)89 .expect(200)90 .end(function(err, res) {91 if (err) return done(err);92 // Add assertions for response data here93 done();94 });95 });96});9798// Uncomment the below code to run tests using a remote Selenium WebDriver with desired capabilities99100/*101const webdriver = require('selenium-webdriver');102const { Builder } = require('selenium-webdriver');103const { Capabilities } = require('selenium-webdriver');104const { Options } = require('selenium-webdriver/firefox');105106const capabilities = Capabilities.firefox();107const options = new Options().addArguments('--headless');108capabilities.set('moz:firefoxOptions', options);109110const driver = new Builder()111 .usingServer('http://localhost:4444/wd/hub') // Replace with the remote server's address112 .withCapabilities(capabilities)113 .build();114115const agent = request.agent(app);116117describe('API Testing', function() {118 before(function(done) {119 agent.get('/login')120 .end(function(err, res) {121 if (err) return done(err);122 // Add code to fill in login form and submit it here123 done();124 });125 });126127 it('should handle GET request', function(done) {128 driver.get('http://example.com/api/v1').then(function() {129 driver.findElement(webdriver.By.id('request-type')).sendKeys('GET');130 driver.findElement(webdriver.By.id('submit-btn')).click().then(function() {131 driver.wait(webdriver.until.elementLocated(webdriver.By.id('response-data')), 10000)132 .then(function() {133 // Add code here to get the response data and assert it134 done();135 });136 });137 });138 });139140 it('should handle POST request', function(done) {141 // Similar to the GET request test but with POST request type142 });143144 // Add tests for other request types145});146147after(function() {148 driver.quit();149});150*/
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