Verify that the API returns the correct results when searching for a string with escape characters.
Language: Java
Framework: Rest assured
1//Assuming that the API endpoint for search is "/search"2//Assuming that escape characters are '\', '/', '-', '[', ']', '{', '}', '(', ')', '*', '+', '?', '.', '|', '^', '$'34import org.junit.Test;5import static io.restassured.RestAssured.*;6import static org.hamcrest.Matchers.*;78public class APITest {910 @Test11 public void escapeCharacterSearch() {1213 //Assuming search query is "/example/path/search?q=\{escape character search\}"14 String searchQuery = "/example/path/search?q=\\{escape character search\\}";1516 given()17 .contentType("application/json")18 .when()19 .get("/search" + searchQuery)20 .then()21 .statusCode(200)22 .body("results", hasItem("Correct Results"));2324 //Assuming the following desired capabilities to connect to a remote client25 /*26 DesiredCapabilities capabilities = new DesiredCapabilities();27 capabilities.setBrowserName("chrome");28 capabilities.setPlatform(Platform.LINUX);2930 RemoteWebDriver driver = new RemoteWebDriver(new URL("http://remoteclient.com:4444/wd/hub"), capabilities);31 driver.get("https://example.com");32 */33 }34}
Language: Javascript
1// Mocha, Chai and SuperTest.23const assert = require('chai').assert;4const request = require('supertest');5const app = require('../app');67describe('API Testing - Check escape character search', () => {8 it('should return correct search results with escape characters',async() => {9 const searchString = 'hello%2C%20world'; // assuming comma is an escape character10 const response = await request(app)11 .get(`/search?q=${searchString}`)12 .set('Accept', 'application/json');1314 assert.strictEqual(response.status, 200);15 assert.isArray(response.body);1617 // additional assertion to check if the returned results contains the search string18 response.body.forEach((result) => {19 assert.include(result.title, decodeURIComponent(searchString));20 });21 });22});2324// Assuming the API accepts a GET request with a query parameter 'q' that contains the search string and it returns an array of JSON objects where each object represents a search result. 'app' is the express app instance that handles the API requests.
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