JAL-1054 Tests for the HttpUtils redirection wrappers
authorBen Soares <b.soares@dundee.ac.uk>
Tue, 14 May 2024 16:37:49 +0000 (17:37 +0100)
committerBen Soares <b.soares@dundee.ac.uk>
Tue, 14 May 2024 16:37:49 +0000 (17:37 +0100)
test/jalview/util/HttpUtilsTest.java [new file with mode: 0644]

diff --git a/test/jalview/util/HttpUtilsTest.java b/test/jalview/util/HttpUtilsTest.java
new file mode 100644 (file)
index 0000000..34e838a
--- /dev/null
@@ -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/", "<!doctype",
+            "Jalview is a" },
+        { "http://www.jalview.org/", "https://www.jalview.org/",
+            "<!doctype", "Jalview is a" },
+        { "https://jalview.org/", "https://www.jalview.org/", "<!doctype",
+            "Jalview is a" },
+        /*
+         */
+        //
+    };
+  }
+
+}