Verify that the API correctly handles caching and returns the correct HTTP status code.
Language: Java
Framework: Rest assured
1//Assuming the URL of the API and endpoint for the caching function23import org.junit.Test;4import static io.restassured.RestAssured.*;5import static org.hamcrest.Matchers.*;67public class APICachingTest {89@Test10public void testCaching() {1112//Assuming the API response should return a 200 status code with caching enabled13given()14 .header("Cache-Control", "max-age=86400")15.when()16 .get("http://example_api.com/cache")17.then()18 .assertThat()19 .statusCode(200)20 .header("Cache-Control", "max-age=86400")21 .body(equalTo("Cache data"));2223//Code to connect to remote client with desired capabilities24//Assuming the remote client has the same URL and endpoint for the caching function25DesiredCapabilities capabilities = DesiredCapabilities.chrome();26capabilities.setPlatform(Platform.LINUX);27WebDriver driver = new RemoteWebDriver(new URL("http://example_remote_host.com:4444/wd/hub"), capabilities);2829//Assuming the API response should return a 200 status code with caching disabled30driver.get("http://example_api.com/nocache");31String pageSource = driver.getPageSource();32assertThat(pageSource, containsString("No cache data"));3334driver.quit();35}36}
Language: Javascript
1// Mocha and Chai.23// Assumption: The API endpoint is available at http://example.com/api4// and the caching mechanism is implemented using ETag headers.56const request = require('request-promise-native');7const chai = require('chai');8const expect = chai.expect;910describe('API caching handling test', function() {11 const baseUrl = 'http://example.com/api';1213 // Set up desired capabilities if connecting to a remote client14 // const capabilities = {15 // browserName: 'chrome',16 // 'goog:chromeOptions': {17 // // disable caching in Chrome for testing purposes18 // prefs: {19 // 'network.http.use-cache': false20 // }21 // }22 // };23 // const driver = new webdriver.Builder()24 // .usingServer('http://remote-client:4444/wd/hub')25 // .withCapabilities(capabilities)26 // .build();2728 // Verify that the API response contains an ETag header29 it('should return an ETag header in the response', async function() {30 const options = {31 method: 'GET',32 uri: baseUrl,33 resolveWithFullResponse: true34 };35 // Local driver connection example36 // await driver.get(baseUrl);37 // const responseHeaders = await driver.executeScript('return document.headers');38 const response = await request(options);39 expect(response.headers).to.have.property('etag');40 });4142 // Verify that the API correctly handles If-None-Match headers43 it('should return a 304 Not Modified status when a matching ETag header is sent in the request', async function() {44 const options = {45 method: 'GET',46 uri: baseUrl,47 headers: {48 'If-None-Match': 'matching-etag-value'49 },50 resolveWithFullResponse: true,51 simple: false52 };53 // Local driver connection example54 // await driver.get(baseUrl); 55 // const responseHeaders = await driver.executeScript('return document.headers');56 // options.headers['If-None-Match'] = responseHeaders.etag; 57 const response = await request(options);58 expect(response.statusCode).to.equal(304);59 });6061 // Verify that the API returns a 200 OK status when a non-matching ETag header is sent in the request62 it('should return a 200 OK status when a non-matching ETag header is sent in the request', async function() {63 const options = {64 method: 'GET',65 uri: baseUrl,66 headers: {67 'If-None-Match': 'non-matching-etag-value'68 },69 resolveWithFullResponse: true70 };71 // Local driver connection example72 // await driver.get(baseUrl); 73 const response = await request(options);74 expect(response.statusCode).to.equal(200);75 });76});
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