API Testing : Check web hook handling

Verify that the API correctly handles web hooks and delivers the correct events to subscribed clients.

Language: Java

Framework: Rest assured

copy
1/​/​Assuming the API endpoint URL is "sample.com/​api/​hook"2/​/​Assuming there are two subscribed clients34import org.junit.Test;5import io.restassured.RestAssured;6import io.restassured.http.ContentType;78public class WebHookHandlingTest {910 @Test11 public void testWebHookHandling() {12 13 /​/​Set up the endpoint URL14 RestAssured.baseURI = "sample.com/​api/​hook";15 16 /​/​Subscribe two clients17 RestAssured.given()18 .contentType(ContentType.JSON)19 .body("{\"client_id\": \"client1\"}")20 .post("/​subscribe")21 .then()22 .statusCode(200);23 24 RestAssured.given()25 .contentType(ContentType.JSON)26 .body("{\"client_id\": \"client2\"}")27 .post("/​subscribe")28 .then()29 .statusCode(200);30 31 /​/​Trigger the event through the web hook32 RestAssured.given()33 .contentType(ContentType.JSON)34 .body("{\"event\": \"sample event\"}")35 .post("/​")36 .then()37 .statusCode(200);38 39 /​/​Verify the event is delivered to subscribed clients40 RestAssured.given()41 .contentType(ContentType.JSON)42 .get("/​events")43 .then()44 .statusCode(200)45 .body("[0].client_id", equalTo("client1"))46 .body("[0].event", equalTo("sample event"))47 .body("[1].client_id", equalTo("client2"))48 .body("[1].event", equalTo("sample event"));49 50 /​/​Instructions to connect to remote client with desired capabilities51 /​/​Assuming the remote client URL is "http:/​/​192.168.0.2:4444/​wd/​hub"52 /​/​Assuming the desired browser is Chrome53 /​*RestAssured.remote("http:/​/​192.168.0.2:4444/​wd/​hub", new DesiredCapabilities("chrome"));54 /​/​Add test code here55 */​56 }5758}

Language: Javascript

copy
1/​/​ Mocha and Chai23/​/​Assumption: The API endpoint URL for subscribing to webhooks is accessible45describe('Webhook Handling', () => {6 it('should deliver correct events to subscribed clients', async () => {7 /​/​Assumption: Subscription to the webhook is successful8 const subscriptionUrl = 'https:/​/​webhook.api.com/​subscribe';9 const response = await fetch(subscriptionUrl);10 expect(response.status).to.eql(200); /​/​Check if subscription is successful with status code 20011 12 /​/​Assumption: Subscription ID is received after successful subscription13 const subscriptionId = 'abcd1234';14 15 /​/​Assumption: Webhook is triggered with correct event payload16 const eventPayload = {17 id: 'event2',18 type: 'purchase',19 data: {20 item: 'iPhone'21 }22 };23 const webhookUrl = `https:/​/​webhook.api.com/​handle/​${subscriptionId}`;24 25 /​/​Connecting to remote client with desired capabilities26 /​/​const capabilities = { browserName: 'chrome' };27 /​/​const driver = new webdriver.Builder().withCapabilities(capabilities).usingServer('http:/​/​localhost:4444/​wd/​hub').build();28 29 /​/​Assumption: Local driver is installed and running to interact with webhook API30 const webdriver = require('selenium-webdriver');31 const driver = new webdriver.Builder()32 .forBrowser('chrome')33 .build();34 35 await driver.get(webhookUrl);36 /​/​Assumption: Webhook is triggered instantly with a response status of 20037 const webhookResponse = await driver.executeScript(`fetch('${webhookUrl}', { method: 'POST', body: JSON.stringify(${eventPayload}) })`);38 39 expect(webhookResponse.status).to.eql(200); /​/​Check if webhook is triggered with status code 20040 41 /​/​Assumption: Expectation (e.g. notification to subscribed client) is delivered successfully42 const notification = await fetch('https:/​/​client.api.com/​notifications');43 const notificationData = await notification.json();44 expect(notificationData).to.eql(eventPayload.data); /​/​Check if the notification payload and the sent event payload are identical45 46 await driver.quit(); /​/​Quit driver once the test is complete47 });48});

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