Verify that the API correctly handles redirects and returns the correct HTTP status code.
Language: Java
Framework: Rest assured
1//Assumptions: API endpoint is already set up and working23//Using local driver4import io.restassured.RestAssured;5import io.restassured.response.Response;6import org.testng.Assert;78public class APITesting {9 10 @Test11 public void checkRedirectHandling() {12 //Add remote client with desired capabilities to enable testing in a remote location13 14 RestAssured.baseURI = "http://example.com";15 Response response = RestAssured.given().redirects().follow(false).when().get("/redirect");16 17 //Verify correct HTTP status code is returned18 Assert.assertEquals(response.getStatusCode(), 302);19 }20}
Language: Javascript
1// Mocha with Chai and Axios.23//Assuming API endpoint is http://example.com/api4//Assuming a valid redirect endpoint is https://example.com/redirect5//Assuming invalid redirect endpoint is https://example.com/undefined67const axios = require('axios');8const { expect } = require('chai');910describe('API Redirect Handling Test', () => {11 it('should follow redirect and return correct status code', async () => {12 const response = await axios.get('http://example.com/api');13 expect(response.status).to.equal(200);14 const redirectResponse = await axios.get('https://example.com/redirect', { maxRedirects: 0 });15 expect(redirectResponse.status).to.equal(302);16 expect(redirectResponse.headers.location).to.equal('https://example.com/undefined');17 const followRedirectResponse = await axios.get('https://example.com/redirect', { maxRedirects: 5 });18 expect(followRedirectResponse.status).to.equal(200);19 expect(followRedirectResponse.data).to.equal('Success');20 });21});2223//Uncomment to connect to remote client with desired capabilities24/*25const webdriver = require('selenium-webdriver');26const { Builder } = webdriver;2728const capabilities = {29 browserName: 'Chrome',30 version: 'latest',31 platformName: 'Windows 10',32 //Add any other desired capabilities here33};3435const driver = new Builder()36 .usingServer('http://localhost:4444/wd/hub')37 .withCapabilities(capabilities)38 .build();39*/
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