From 0cde1c07fee9f43548745b2399d96b0775e8a9c2 Mon Sep 17 00:00:00 2001 From: saranrnair Date: Fri, 9 May 2025 12:39:41 +0200 Subject: [PATCH] test case added --- src/test/java/com/scalesec/vulnado/test.java | 60 ++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/test/java/com/scalesec/vulnado/test.java diff --git a/src/test/java/com/scalesec/vulnado/test.java b/src/test/java/com/scalesec/vulnado/test.java new file mode 100644 index 00000000..5fc7f97a --- /dev/null +++ b/src/test/java/com/scalesec/vulnado/test.java @@ -0,0 +1,60 @@ +import org.junit.Test; +import org.mockito.Mockito; +import java.io.IOException; +import java.util.List; +import static org.junit.Assert.*; + +public class LinkListerTest { + + // Helper method to mock Jsoup connection and document + private void mockJsoupConnection(String url, String htmlContent) throws IOException { + Document mockDocument = Jsoup.parse(htmlContent); + Mockito.when(Jsoup.connect(url).get()).thenReturn(mockDocument); + } + + @Test + public void getLinks_ShouldReturnLinks() throws IOException { + String url = "http://example.com"; + String htmlContent = "Link1Link2"; + mockJsoupConnection(url, htmlContent); + + List links = LinkLister.getLinks(url); + + assertEquals("Expected two links", 2, links.size()); + assertTrue("Expected link1", links.contains("http://example.com/link1")); + assertTrue("Expected link2", links.contains("http://example.com/link2")); + } + + @Test(expected = IOException.class) + public void getLinks_ShouldThrowIOException() throws IOException { + String url = "http://invalid-url.com"; + Mockito.when(Jsoup.connect(url).get()).thenThrow(new IOException("Invalid URL")); + + LinkLister.getLinks(url); + } + + @Test + public void getLinksV2_ShouldReturnLinks() throws BadRequest, IOException { + String url = "http://example.com"; + String htmlContent = "Link1Link2"; + mockJsoupConnection(url, htmlContent); + + List links = LinkLister.getLinksV2(url); + + assertEquals("Expected two links", 2, links.size()); + assertTrue("Expected link1", links.contains("http://example.com/link1")); + assertTrue("Expected link2", links.contains("http://example.com/link2")); + } + + @Test(expected = BadRequest.class) + public void getLinksV2_ShouldThrowBadRequestForPrivateIP() throws BadRequest { + String url = "http://192.168.0.1"; + LinkLister.getLinksV2(url); + } + + @Test(expected = BadRequest.class) + public void getLinksV2_ShouldThrowBadRequestForInvalidURL() throws BadRequest { + String url = "invalid-url"; + LinkLister.getLinksV2(url); + } +}