Verify that the API correctly handles low traffic and returns the correct HTTP status code and error message.
Language: Java
Framework: Rest assured
1import org.testng.annotations.Test;2import static io.restassured.RestAssured.given;3import static org.hamcrest.Matchers.*;4 5public class APITest {6 7 @Test8 public void testLowTraffic() {9 // Assumptions:10 // * API endpoint: http://example.com/api11 // * Low traffic is defined as <100 requests per minute12 // * Expected HTTP status code for low traffic: 20013 // * Expected error message for high traffic: "Error: Too much traffic"14 15 given()16 .when()17 .get("http://example.com/api")18 .then()19 .statusCode(200)20 .body("error", equalTo(null))21 .body("message", equalTo("Low traffic detected"));22 23 // commented code to connect to remote client with desired capabilities24 // DesiredCapabilities capabilities = new DesiredCapabilities();25 // capabilities.setCapability("platformName", "Android");26 // capabilities.setCapability("deviceName", "Android Emulator");27 // capabilities.setCapability("appPackage", "com.example.package");28 // capabilities.setCapability("appActivity", "com.example.package.MainActivity");29 // WebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);30 }31}
Language: Javascript
1// Mocha-Chai23//Assuming the API endpoint for low traffic is "/api/low_traffic"4//Assuming that the API returns HTTP status code 200 for successful response and 404 for error56const chai = require('chai');7const chaiHttp = require('chai-http');8const expect = chai.expect;910chai.use(chaiHttp);1112describe('Low Traffic API Test', function () {1314 it('Should return HTTP 200 status code and success message on low traffic', function () {15 chai.request('http://localhost:3000') //assuming API is hosted on localhost:300016 .get('/api/low_traffic')17 .end(function (err, res) {18 expect(err).to.be.null;19 expect(res).to.have.status(200);20 expect(res.body).to.have.property('message').equals('Low traffic detected');21 });22 });2324 it('Should return HTTP 404 status code and error message on non-existent page', function () {25 chai.request('http://localhost:3000')26 .get('/api/non_existent_page')27 .end(function (err, res) {28 expect(err).to.be.null;29 expect(res).to.have.status(404);30 expect(res.body).to.have.property('error').equals('Page not found');31 });32 });33 34 /* //Code to connect to remote client with desired capabilities35 const webdriver = require('selenium-webdriver');36 const remote = require('selenium-webdriver/remote');37 38 const capabilities = webdriver.Capabilities.chrome();39 capabilities.set('browserName', 'chrome');40 41 const driver = new webdriver.Builder()42 .withCapabilities(capabilities)43 .usingServer('http://remotehub.com/wd/hub')44 .build();45 */4647});
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