Verify that the API returns an error message if the requested resource is not authorized.
Language: Java
Framework: Rest assured
1import org.testng.annotations.Test;2import io.restassured.RestAssured;3import io.restassured.response.Response;4import io.restassured.specification.RequestSpecification;56public class TestAPI {78 @Test9 public void unauthorizedResourceTest() {1011 // Base URI for API endpoint12 RestAssured.baseURI = "https://example.com/api/";1314 // Create request object15 RequestSpecification request = RestAssured.given();1617 // Add Authorization header18 request.header("Authorization", "Bearer <access_token>");1920 // Send GET request to unauthorized resource21 Response response = request.get("unauthorized_resource");2223 // Assert that response status code is 40124 assert response.getStatusCode() == 401 : "Response status code is not 401";2526 // Assert that response body contains error message27 assert response.getBody().asString().contains("Unauthorized") : "Response body does not contain error message";2829 // Uncomment the code below to connect to remote client with desired capabilities3031 /*32 DesiredCapabilities capabilities = new DesiredCapabilities();33 capabilities.setBrowserName("Chrome");34 capabilities.setPlatform(Platform.LINUX);35 RemoteWebDriver driver = new RemoteWebDriver(new URL("<remote_client_url>"), capabilities);36 driver.get("https://example.com/api/");37 // Perform API testing using Rest assured38 */3940 }41}
Language: Javascript
1// Mocha with Chai Assertion Library.23//Assuming that the API endpoint is '/api/v1/resource'4//Assuming that the unauthorized resource returns a status code of 401 and an error message in the response body5//Assuming that the API request is a GET method67const request = require('supertest');8const app = require('../app'); //Assuming that the app is already running on localhost:3000910describe('API Testing - Check unauthorized resource', function() {11 it('should return an error message if the requested resource is not authorized', function(done) {12 request(app)13 .get('/api/v1/resource')14 .expect(401)15 .end(function(err, res) {16 if (err) return done(err);17 expect(res.body.message).to.equal('Unauthorized request'); //Assuming that the error message is 'Unauthorized request'18 done();19 });20 });21});2223//Sample code to connect to remote client with desired capabilities:2425const {Builder, By, Capabilities, until} = require('selenium-webdriver');2627const chromeCapabilities = Capabilities.chrome();28chromeCapabilities.set('chromeOptions', {29 'args': ['--headless', '--disable-gpu'] //Assuming we want to run the tests in headless mode30});3132const driver = new Builder()33 .usingServer('http://remoteclient:4444/wd/hub') //Assuming the remote client URL is http://remoteclient:4444/wd/hub34 .withCapabilities(chromeCapabilities)35 .build();
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