API Testing : Check invalid requests

Verify that the API returns the correct HTTP status code for invalid requests (e.g. HTTP 400 Bad Request).

Language: Java

Framework: Rest assured

copy
1/​/​Assumption: The API endpoint is "https:/​/​example.com/​api"2/​/​Assumption: The invalid request parameters are not specified in the test case and will be identified during test execution.34import org.testng.annotations.Test;5import io.restassured.RestAssured;6import io.restassured.response.Response;7import io.restassured.specification.RequestSpecification;89public class APITest {10@Test11public void checkInvalidRequests() {12/​/​Local Driver Configuration13RestAssured.baseURI = "https:/​/​example.com/​api";14RequestSpecification request = RestAssured.given();1516/​/​Remote client configuration with desired capabilities17/​/​DesiredCapabilities caps = DesiredCapabilities.firefox();18/​/​caps.setPlatform(Platform.WINDOWS);19/​/​caps.setCapability("version", "49.0");2021/​/​Try with invalid request parameter22Response response = request.queryParam("param", "invalid value").get();23/​/​Verify with expected status code - HTTP 400 Bad Request24assert response.getStatusCode() == 400 : "Expected status code 400 but got " + response.getStatusCode();2526/​/​Try with invalid request header27request = RestAssured.given();28request.header("Invalid Header", "invalid value");29response = request.get();30/​/​Verify with expected status code - HTTP 400 Bad Request31assert response.getStatusCode() == 400 : "Expected status code 400 but got " + response.getStatusCode();32}33}

Language: Javascript

copy
1/​/​ Mocha and Chai with Axios library.23/​/​Assumptions:4/​/​1. API URL is 'http:/​/​example.com/​api'5/​/​2. Invalid request URL is 'http:/​/​example.com/​api/​invalid'6/​/​3. Invalid request has an invalid parameter 'xyz'78const axios = require('axios');9const { expect } = require('chai');1011describe('API Testing - Invalid Requests', () => {12 it('should return HTTP 400 Bad Request for invalid requests', async () => {13 /​/​local driver14 const apiEndpoint = 'http:/​/​example.com/​api/​invalid?xyz=abc'; 15 const response = await axios.get(apiEndpoint)16 expect(response.status).to.equal(400);17 expect(response.data).to.have.property('error');18 expect(response.data.error).to.contain('Invalid parameter xyz');19 20 /​/​remote client21 /​/​const capabilities = { browserName: 'chrome' };22 /​/​const remoteEndpoint = 'http:/​/​remote.server.com/​wd/​hub';23 /​/​const remoteDriver = await new webdriver.Builder()24 /​/​.usingServer(remoteEndpoint)25 /​/​.withCapabilities(capabilities)26 /​/​.build();27 /​/​const apiEndpoint = 'http:/​/​example.com/​api/​invalid?xyz=abc'; 28 /​/​const response = await remoteDriver.get(apiEndpoint);29 /​/​expect(response.status).to.equal(400);30 /​/​expect(response.data).to.have.property('error');31 /​/​expect(response.data.error).to.contain('Invalid parameter xyz');32 });33});

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