API Testing : Check SSL-TLS handling

Verify that the API correctly handles SSL/TLS certificates and returns the correct HTTP status code.

Language: Java

Framework: Rest assured

copy
1import org.junit.Test;2import io.restassured.RestAssured;3import io.restassured.response.Response;4import io.restassured.specification.RequestSpecification;56public class SSLTLSHandlerTest {7    @Test8    public void testSSlTLSHandling() {9        /​/​Assumptions: API endpoint url is https:/​/​example.com/​api10        /​/​Assumptions: SSL/​TLS certificate is valid and signed by a trusted certificate authority11        /​/​Assumptions: HTTP status code 200 should be returned if SSL/​TLS certificate is valid12        /​/​Assumptions: HTTP status code 400 should be returned if SSL/​TLS certificate is invalid13        /​/​Assumptions: Remote client desired capabilities have been set to allow handling of SSL/​TLS certificates1415        /​/​Connecting to remote client with desired capabilities16        /​/​DesiredCapabilities capabilities = DesiredCapabilities.chrome();17        /​/​capabilities.setCapability("acceptSslCerts", true);18        /​/​WebDriver driver = new RemoteWebDriver(new URL("http:/​/​remote.client.url:4444/​wd/​hub"), capabilities);19        /​/​System.setProperty("webdriver.chrome.driver", "path/​to/​chromedriver");2021        /​/​Setting up request for API endpoint22        RestAssured.baseURI = "https:/​/​example.com/​api";23        RequestSpecification request = RestAssured.given();24        request.relaxedHTTPSValidation();2526        /​/​Making GET request to the API endpoint27        Response response = request.get();2829        /​/​Checking if SSL/​TLS certificate is valid and correct HTTP status code is returned30        if (response.statusCode() == 200) {31            System.out.println("SSL/​TLS certificate is valid and API returns HTTP status code 200");32        } else if (response.statusCode() == 400) {33            System.out.println("SSL/​TLS certificate is invalid and API returns HTTP status code 400");34        }35    }36}

Language: Javascript

copy
1/​/​Mocha with Chai Assertion Library.23/​/​Assuming that the API endpoint is https:/​/​example.com/​api and SSL/​TLS certificates are enabled and valid.45const request = require('request');6const { expect } = require('chai');78describe('API SSL/​TLS Handling Test', function() {9 it('Should return correct HTTP status code when SSL/​TLS certificates are enabled and valid', function(done) {10 const options = {11 url: 'https:/​/​example.com/​api',12 method: 'GET',13 rejectUnauthorized: true14 };15 request(options, function(err, res, body) {16 expect(res.statusCode).to.equal(200);17 done();18 });19 });2021 it('Should return error when SSL/​TLS certificates are invalid', function(done) {22 const options = {23 url: 'https:/​/​example.com/​api',24 method: 'GET',25 rejectUnauthorized: false26 };27 request(options, function(err, res, body) {28 expect(err).to.exist;29 expect(res.statusCode).to.not.equal(200);30 done();31 });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