API Testing : Check partial updates

Verify that the API handles partial updates correctly (e.g. PATCH requests).

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the API endpoint is already defined and available for testing23import io.restassured.RestAssured;4import io.restassured.http.ContentType;56import org.testng.annotations.Test;78import static io.restassured.RestAssured.given;910public class PartialUpdateTest {1112@Test13public void testPartialUpdate(){1415/​/​Assuming the API url endpoint is http:/​/​myapi.com/​employees/​12341617RestAssured.baseURI = "http:/​/​myapi.com/​";1819String jsonString = "{\"name\":\"John Doe\", \"salary\":\"10000\"}";2021given().22contentType(ContentType.JSON).23body(jsonString).24when().25patch("/​employees/​1234").26then().27statusCode(200);2829/​/​Assuming that the returned status code of 200 indicates a successful partial update of the employee record with an employee id of 1234.3031/​/​To connect to a remote client with desired capabilities, add the desired capabilities as follows:3233DesiredCapabilities capabilities = new DesiredCapabilities();34capabilities.setPlatform(Platform.WINDOWS);35capabilities.setBrowserName("chrome");36capabilities.setVersion("87.0");3738WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);3940/​/​Assuming that the hub URL is http:/​/​localhost:4444/​wd/​hub and the desired capabilities include Chrome browser version 87.0 running on Windows platform. 41}42}4344/​/​End of code

Language: Java

Framework: Rest assured

copy
1/​/​Assuming API endpoint is http:/​/​example.com/​partial-updates2/​/​Assuming partial update is supported for attribute 'firstName'34import static io.restassured.RestAssured.*;5import static org.hamcrest.Matchers.*;67public class PartialUpdateTest {8 9 String apiEndpoint = "http:/​/​example.com/​partial-updates";10 String firstName = "John";1112 @Test13 public void testPartialUpdate() {14 given()15 .contentType("application/​json")16 .body("{\"firstName\": \"" + firstName + "\"}")17 .when()18 .patch(apiEndpoint)19 .then()20 .statusCode(200)21 .body("message", is("Partial update successful"))22 .body("firstName", is(firstName));23 24 /​/​Connecting to remote client with desired capabilities25 /​*26 DesiredCapabilities caps = new DesiredCapabilities();27 caps.setBrowserName("chrome");28 caps.setPlatform(Platform.LINUX);2930 RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), caps);31 */​32 }33}

Language: Javascript

copy
1/​/​ Mocha + Chai.23/​/​Assuming that the API endpoint for partial update is "/​partial-update"4/​/​Assuming that the API returns a success status code 200 for successful partial update56const assert = require('chai').assert; 7const fetch = require('node-fetch');8const apiUrl = 'http:/​/​localhost:3000'; /​/​Assuming the API endpoint is running on this local server910describe('Partial Update API Testing', () => {11 it('Should handle partial updates correctly using PATCH requests', async () => {12 const userId = '123'; /​/​Assuming that we want to update a user with id 12313 const dataToUpdate = {name: 'John Doe'}; /​/​Assuming that we want to update the name property of the user to 'John Doe'14 const patchRequest = {15 method: 'PATCH',16 headers: { 'Content-Type': 'application/​json' },17 body: JSON.stringify(dataToUpdate)18 };19 const response = await fetch(`${apiUrl}/​partial-update/​${userId}`, patchRequest);20 const responseBody = await response.json();21 assert.equal(response.status, 200); /​/​Assuming that the API returns a success status code 200 for successful partial update22 assert.deepEqual(responseBody.user.name, 'John Doe'); /​/​Assuming that the API returns the updated user object in the response with the updated name property23 });24});2526/​/​Code to connect to remote client with desired capabilities27/​/​Assuming that we want to run the tests on a remote Selenium Grid node with Firefox2829const { Builder, By, Key, Capabilities } = require('selenium-webdriver');30const firefoxCapabilities = Capabilities.firefox();31const gridUrl = 'http:/​/​localhost:4444/​wd/​hub'; /​/​Assuming the Selenium Grid endpoint is running on this server3233/​/​Creating a remote driver with desired capabilities34const remoteDriver = await new Builder()35 .withCapabilities(firefoxCapabilities)36 .usingServer(gridUrl)37 .build();3839/​/​Implement the same tests using the remote driver40/​/​Assuming that the web application that serves the API is running on "http:/​/​localhost:8080"4142(remoteDriver code omitted for brevity)

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​ Assumptions:4/​/​ 1. API base URL: 'https:/​/​example.com/​api'5/​/​ 2. Endpoint for partial updates: '/​users/​:userId'6/​/​ 3. userId: 1237/​/​ 4. API requires authentication token89const chai = require('chai');10const chaiHttp = require('chai-http');11const expect = chai.expect;1213chai.use(chaiHttp);1415describe('Partial updates API testing', () => {16 const apiUrl = 'https:/​/​example.com/​api';17 const endpoint = `/​users/​123`;18 const authToken = 'some-auth-token';1920 it('should return 200 for successful partial update', (done) => {21 chai.request(apiUrl)22 .patch(endpoint)23 .set('Authorization', `Bearer ${authToken}`)24 .send({ firstName: 'John' })25 .end((err, res) => {26 expect(err).to.be.null;27 expect(res).to.have.status(200);28 done();29 });30 });3132 it('should return 400 for invalid partial update', (done) => {33 chai.request(apiUrl)34 .patch(endpoint)35 .set('Authorization', `Bearer ${authToken}`)36 .send({ invalidField: 'invalidValue' })37 .end((err, res) => {38 expect(err).to.be.null;39 expect(res).to.have.status(400);40 done();41 });42 });43});

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