Verify that the API correctly handles broken links and returns the correct HTTP status code and error message.
Language: Java
Framework: Rest assured
1//Assuming the API endpoint is http://api.example.com2//Assuming a list of links to test is provided in a file named links.txt34import org.junit.Test;5import static io.restassured.RestAssured.*;6import static org.hamcrest.Matchers.*;78public class BrokenLinksTest {910 @Test11 public void testBrokenLinks() {12 13 //Read links from file14 List<String> links = readLinksFromFile("links.txt");1516 for (String link : links) {17 18 //Connect to API endpoint19 given()20 .baseUri("http://api.example.com")21 22 //Make HTTP request to link23 when()24 .get(link)25 26 //Verify that status code is NOT 404 (Not Found)27 //Assuming that 404 is the only status code for broken links28 then()29 .assertThat()30 .statusCode(not(404))31 32 //Verify that error message is returned33 //Assuming that the API returns an error message for broken links34 .assertThat()35 .body(not(emptyString()));36 }37 }38 39 //Method to read links from file40 private List<String> readLinksFromFile(String fileName) {41 List<String> links = new ArrayList<>();42 try {43 File file = new File(fileName);44 Scanner scanner = new Scanner(file);45 while (scanner.hasNextLine()) {46 links.add(scanner.nextLine());47 }48 scanner.close();49 } catch (FileNotFoundException e) {50 e.printStackTrace();51 }52 return links;53 }54 55 //Code to connect to remote client with desired capabilities56 /*57 DesiredCapabilities capabilities = new DesiredCapabilities();58 capabilities.setBrowserName("chrome");59 capabilities.setVersion("88.0");60 capabilities.setPlatform(Platform.WIN10);61 62 RemoteWebDriver driver = new RemoteWebDriver(new URL("http://<REMOTE_CLIENT_IP>:<PORT>/wd/hub"), capabilities);63 */64}
Language: Javascript
1//Mocha and Chai.23/*4Assumptions:51. The API endpoint is already known.62. The API is expected to return a HTTP status code and error message in case of broken links.73. An array of links is provided for testing.8*/910const request = require('request');11const { expect } = require('chai');1213describe('API Testing for broken links', () => {14 it('should handle broken links and return correct HTTP status code and error message', (done) => {15 const links = ['https://www.example.com/', 'https://www.google.com/', 'https://www.example.com/broken-link'];16 const endpoint = 'http://api.example.com/check-broken-links';1718 // Assuming that the API endpoint is called with POST method and data is sent in JSON format19 request.post(endpoint, { json: links }, (error, response, body) => {20 // Assuming that the API will return a JSON object with 'status' and 'message' properties21 expect(response.statusCode).to.equal(200);22 expect(body.status).to.equal('success');23 expect(body.message).to.equal('All links are working');2425 done();26 });27 });28});
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