Verify that the API correctly handles caching and returns the correct HIT status code.
Language: Java
Framework: Rest assured
1//Assuming API is up and running2//Assuming authentication is already done3//Assuming endpoint for the API is given45import io.restassured.RestAssured;6import static io.restassured.RestAssured.given;7import static org.hamcrest.Matchers.equalTo;89public class APITest {1011 @Test12 public void testHITCaching() {13 14 //Local Driver15 RestAssured.baseURI = "http://localhost:8080"; //Assuming the API is running on localhost:808016 17 //Remote Client with Desired Capabilities18 //WebDriver driver = new RemoteWebDriver(new URL("http://localhost:8000/wd/hub"), DesiredCapabilities.firefox()); //Assuming a remote firefox browser is used19 20 given().log().all()21 .when().get("/hit")22 .then().assertThat().statusCode(200) //Assuming 200 status code is returned for a successful GET request23 .and().body("hit_status", equalTo("cached")); //Assuming the HIT status is returned as "cached"24 }25}
Language: Javascript
1// Mocha and Chai23//Assuming the API endpoint is: 4let apiEndpoint = 'http://example.com/api'56describe('API Testing', function() {7 it('should handle HIT caching correctly', function(done) {8 let req1 = new XMLHttpRequest();9 req1.onreadystatechange = function() {10 if(req1.readyState === XMLHttpRequest.DONE) {11 let response1 = JSON.parse(req1.responseText);12 expect(response1.status).to.equal(200);13 14 let req2 = new XMLHttpRequest();15 req2.onreadystatechange = function() {16 if(req2.readyState === XMLHttpRequest.DONE) {17 let response2 = JSON.parse(req2.responseText);18 expect(response2.status).to.equal(304);19 20 done();21 }22 };23 24 req2.open('GET', apiEndpoint);25 req2.setRequestHeader('If-None-Match', response1.ETag);26 req2.send();27 }28 };29 30 req1.open('GET', apiEndpoint);31 req1.send();32 });33 34 //Uncomment the following code to connect to remote client with desired capabilities35 /*36 let webdriver = require('selenium-webdriver')37 let capabilities = {38 browserName: 'chrome',39 platformName: 'Windows',40 platformVersion: '10'41 }42 let driver = new webdriver.Builder()43 .withCapabilities(capabilities)44 .usingServer('http://yourserver:yourport/wd/hub')45 .build()46 */47 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.
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