BAEL-5741 Download a Webpage in Java (#13531)
* BAEL-5741 Download a Webpage in Java * BAEL-5741 Download a Webpage in Java * BAEL-5741 Download a Webpage in Java * BAEL-5741 Download a Webpage in Java * BAEL-5741 Download a Webpage in Java
This commit is contained in:
parent
a713d96d4d
commit
41b3460af6
|
@ -20,6 +20,12 @@
|
|||
<artifactId>commons-validator</artifactId>
|
||||
<version>${apache.commons-validator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jsoup</groupId>
|
||||
<artifactId>jsoup</artifactId>
|
||||
<version>${jsoup.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -28,6 +34,7 @@
|
|||
|
||||
<properties>
|
||||
<apache.commons-validator.version>1.7</apache.commons-validator.version>
|
||||
<jsoup.version>1.15.4</jsoup.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.downloadwebpage;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class DownloadWebpageUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenURLConnection_whenRetrieveWebpage_thenWebpageIsNotNullAndContainsHtmlTag() throws IOException {
|
||||
URL url = new URL("https://example.com");
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
|
||||
StringBuilder responseBuilder = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
responseBuilder.append(line);
|
||||
}
|
||||
|
||||
assertNotNull(responseBuilder);
|
||||
assertTrue(responseBuilder.toString()
|
||||
.contains("<html>"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJsoup_whenRetrievingWebpage_thenWebpageDocumentIsNotNullAndContainsHtmlTag() throws IOException {
|
||||
|
||||
Document document = Jsoup.connect("https://example.com")
|
||||
.get();
|
||||
String webpage = document.html();
|
||||
|
||||
assertNotNull(webpage);
|
||||
assertTrue(webpage.contains("<html>"));
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue