From ac9d20888857c782d9c49251a943c389afc10039 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Tue, 14 May 2024 17:37:49 +0100 Subject: [PATCH] JAL-1054 Tests for the HttpUtils redirection wrappers --- test/jalview/util/HttpUtilsTest.java | 92 ++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 test/jalview/util/HttpUtilsTest.java diff --git a/test/jalview/util/HttpUtilsTest.java b/test/jalview/util/HttpUtilsTest.java new file mode 100644 index 0000000..34e838a --- /dev/null +++ b/test/jalview/util/HttpUtilsTest.java @@ -0,0 +1,92 @@ +package jalview.util; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Locale; + +import org.testng.Assert; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +public class HttpUtilsTest +{ + @Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations") + public void testFollowConnection(String targetUrl, String finalUrl, + String notUsed0, String notUsed1) throws IOException + { + URL tUrl = new URL(targetUrl); + URL fUrl = new URL(finalUrl); + HttpURLConnection conn1 = HttpUtils + .followConnection((HttpURLConnection) tUrl.openConnection()); + URL url1 = conn1.getURL(); + Assert.assertEquals(url1, fUrl, "Final URL is not the same."); + } + + @Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations") + public void testOpenConnection(String targetUrl, String finalUrl, + String notUsed0, String notUsed1) throws IOException + { + URL tUrl = new URL(targetUrl); + URL fUrl = new URL(finalUrl); + HttpURLConnection conn1 = HttpUtils.openConnection(tUrl); + URL url1 = conn1.getURL(); + Assert.assertEquals(url1, fUrl, "Final URL is not the same."); + } + + @Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations") + public void testOpenStream(String targetUrl, String finalUrl, + String inFirstLine, String inDocument) throws IOException + { + URL tUrl = new URL(targetUrl); + URL fUrl = new URL(finalUrl); + InputStream is1 = HttpUtils.openStream(tUrl); + BufferedReader br1 = new BufferedReader(new InputStreamReader(is1)); + String firstLine = br1.readLine().toLowerCase(Locale.ROOT); + Assert.assertTrue( + firstLine.contains(inFirstLine.toLowerCase(Locale.ROOT)), + "First line of text '" + firstLine + "' does not contain '" + + inFirstLine + "'"); + String inDocumentLC = inDocument.toLowerCase(Locale.ROOT); + boolean found = false; + String line = null; + while ((line = br1.readLine()) != null) + { + if (line.toLowerCase(Locale.ROOT).contains(inDocumentLC)) + { + found = true; + break; + } + } + Assert.assertTrue(found, + "Text '" + inDocument + "' not found in '" + finalUrl + "'"); + } + + @DataProvider(name = "urlTargetsAndDestinations") + public Object[][] urlTargetsAndDestinations() + { + /* + String targetUrl, + String finalUrl, + String foundInFirstLine, + */ + return new Object[][] { + // + /* + */ + { "http://jalview.org/", "https://www.jalview.org/", "