API Testing : Check filter parameters

Verify that the API response contains the correct filtering information based on the provided filter parameters.

Language: Java

Framework: Rest assured

copy
1/​/​ Assume API endpoint is "https:/​/​example.com/​api"2/​/​ Assume filter parameters are "category" and "date"34import io.restassured.RestAssured;5import io.restassured.response.Response;6import io.restassured.specification.RequestSpecification;78public class ApiTest {910 public static void main(String[] args) {1112 /​/​ Connect to local driver13 RestAssured.baseURI = "https:/​/​example.com/​api";14 RequestSpecification httpRequest = RestAssured.given();1516 /​/​ Set filter parameters17 httpRequest.queryParam("category", "electronics");18 httpRequest.queryParam("date", "2022-01-01");1920 /​/​ Send request and get response21 Response response = httpRequest.get();2223 /​/​ Verify response contains correct filtering information24 response.then().assertThat().body("category", equalTo("electronics"));25 response.then().assertThat().body("date", equalTo("2022-01-01"));2627 /​/​ Commented code to connect to remote client with desired capabilities28 /​*29 DesiredCapabilities capabilities = new DesiredCapabilities();30 capabilities.setCapability("browserName", "Chrome");31 RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);32 driver.get("https:/​/​example.com/​api");33 */​34 }35}

Language: Javascript

copy
1/​/​ Mocha and Chai.23/​/​Assuming the API endpoint is 'https:/​/​example.com/​api/​'4/​/​Assuming the filter parameters are 'type=fruit&color=red'56const { expect } = require('chai');7const fetch = require('node-fetch');89describe('API Testing - Check filter parameters', function() {10 11 it('Verify that the API response contains the correct filtering information based on the provided filter parameters', async function() {12 13 const url = 'https:/​/​example.com/​api/​type=fruit&color=red';14 const response = await fetch(url);15 const data = await response.json();16 17 /​/​Assuming the response is a JSON object with the following structure - { fruit: '[an array of fruit objects]', error: '[string with error message, if any]' }18 19 expect(response.status).to.equal(200); /​/​Assuming 200 status code for successful API call20 21 expect(data.fruit).to.be.an('array');22 23 data.fruit.forEach(fruit => {24 expect(fruit.type).to.equal('fruit');25 expect(fruit.color).to.equal('red');26 });27 28 /​/​Assuming the error message is an empty string when there are no errors29 30 expect(data.error).to.equal('');31 32 });33 34});3536/​/​Code to use local driver for API testing - there's no need to connect to a remote client with desired capabilities37/​/​This assumes a Node.js environment is setup locally with the required dependencies install, such as Mocha, Chai and node-fetch.3839/​/​To run this test locally, run 'npm test' in the terminal from the directory where this file is saved.

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