JAL-1054 Add option to always follow redirects and not just http->https ones in jalvi... bug/JAL-1054_broken_test
authorBen Soares <b.soares@dundee.ac.uk>
Mon, 2 Sep 2024 16:02:38 +0000 (17:02 +0100)
committerBen Soares <b.soares@dundee.ac.uk>
Mon, 2 Sep 2024 16:02:38 +0000 (17:02 +0100)
src/jalview/util/HttpUtils.java
test/jalview/util/HttpUtilsTest.java

index 1d9f8d0..a074a39 100644 (file)
@@ -142,11 +142,24 @@ public class HttpUtils
   public static HttpURLConnection followConnection(HttpURLConnection conn0)
           throws IOException
   {
+    return followConnection(conn0, false);
+  }
+
+  public static HttpURLConnection followConnection(HttpURLConnection conn0,
+          boolean followAnyway) throws IOException
+  {
     URL url = conn0.getURL();
     // we are only checking for a redirect from http to https otherwise the java
     // connection will follow when called (if not unset)
-    if (url == null || !"http".equals(url.getProtocol())
-            || !conn0.getInstanceFollowRedirects())
+    if (url == null)
+    {
+      return conn0;
+    }
+    if (!conn0.getInstanceFollowRedirects())
+    {
+      return conn0;
+    }
+    if (!"http".equals(url.getProtocol()) && !followAnyway)
     {
       return conn0;
     }
@@ -168,14 +181,20 @@ public class HttpUtils
         url = loc;
       }
     }
+    else if (followAnyway)
+    {
+      // checkConn might have followed a https->https redirect
+      url = checkConn.getURL();
+    }
 
-    if (!redirectToHttps)
+    if (!redirectToHttps && !followAnyway)
     {
       return conn0;
     }
 
-    // We want to return an HttpURLConnection to the new https URL that is
-    // unconnected in case further manipulation of the request is required
+    // We want to return an HttpURLConnection to the new (probably https) URL
+    // that is unconnected in case further manipulation of the request is
+    // required.
     HttpURLConnection conn = (HttpURLConnection) url.openConnection();
     httpURLConnectionCopyAttributes(conn0, conn);
     return conn;
@@ -202,6 +221,12 @@ public class HttpUtils
    */
   public static URLConnection openConnection(URL url) throws IOException
   {
+    return openConnection(url, false);
+  }
+
+  public static URLConnection openConnection(URL url, boolean followAnyway)
+          throws IOException
+  {
     if (url == null)
     {
       Console.debug("HttpUtils.openConnection(url) called with null url");
@@ -216,7 +241,7 @@ public class HttpUtils
       HttpURLConnection conn0 = (HttpURLConnection) url.openConnection();
       if (conn0 != null)
       {
-        conn = HttpUtils.followConnection(conn0);
+        conn = HttpUtils.followConnection(conn0, followAnyway);
       }
       else
       {
@@ -240,6 +265,12 @@ public class HttpUtils
    */
   public static InputStream openStream(URL url) throws IOException
   {
+    return openStream(url, false);
+  }
+
+  public static InputStream openStream(URL url, boolean followAnyway)
+          throws IOException
+  {
     if (url == null)
     {
       return null;
@@ -248,8 +279,8 @@ public class HttpUtils
     String protocol = url.getProtocol();
     if ("http".equals(protocol) || "https".equals(protocol))
     {
-      HttpURLConnection conn = HttpUtils
-              .followConnection((HttpURLConnection) url.openConnection());
+      HttpURLConnection conn = HttpUtils.followConnection(
+              (HttpURLConnection) url.openConnection(), followAnyway);
       if (conn != null)
       {
         is = conn.getInputStream();
index d8b757d..6054ca6 100644 (file)
@@ -16,35 +16,38 @@ public class HttpUtilsTest
 {
   @Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations")
   public void testFollowConnection(String targetUrl, String finalUrl,
-          String notUsed0, String notUsed1) throws IOException
+          String notUsed0, String notUsed1, boolean followAnyway)
+          throws IOException
   {
     URL tUrl = new URL(targetUrl);
     URL fUrl = new URL(finalUrl);
-    HttpURLConnection conn1 = HttpUtils
-            .followConnection((HttpURLConnection) tUrl.openConnection());
+    HttpURLConnection conn1 = HttpUtils.followConnection(
+            (HttpURLConnection) tUrl.openConnection(), followAnyway);
     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
+          String notUsed0, String notUsed1, boolean followAnyway)
+          throws IOException
   {
     URL tUrl = new URL(targetUrl);
     URL fUrl = new URL(finalUrl);
     HttpURLConnection conn1 = (HttpURLConnection) HttpUtils
-            .openConnection(tUrl);
+            .openConnection(tUrl, followAnyway);
     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
+          String inFirstLine, String inDocument, boolean followAnyway)
+          throws IOException
   {
     URL tUrl = new URL(targetUrl);
     URL fUrl = new URL(finalUrl);
-    InputStream is1 = HttpUtils.openStream(tUrl);
+    InputStream is1 = HttpUtils.openStream(tUrl, followAnyway);
     BufferedReader br1 = new BufferedReader(new InputStreamReader(is1));
     String firstLine = br1.readLine().toLowerCase(Locale.ROOT);
     Assert.assertTrue(
@@ -74,17 +77,24 @@ public class HttpUtilsTest
     String finalUrl, // the URL you end up at
     String foundInFirstLine, // some text found in the first line
     String foundInDocument, // some text found in the document (and won't be in an error page)
+    boolean followAnyway, // whether to follow redirects even if they're not http->https
     */
     return new Object[][] {
         //
         /*
          */
         { "http://jalview.org/", "https://www.jalview.org/", "<!doctype",
-            "Jalview is a" },
+            "Jalview is a", false },
         { "http://www.jalview.org/", "https://www.jalview.org/",
-            "<!doctype", "Jalview is a" },
+            "<!doctype", "Jalview is a", false },
+        { "https://jalview.org/", "https://jalview.org/", "<!doctype",
+            "Jalview is a", false },
+        { "http://jalview.org/", "https://www.jalview.org/", "<!doctype",
+            "Jalview is a", true },
+        { "http://www.jalview.org/", "https://www.jalview.org/",
+            "<!doctype", "Jalview is a", true },
         { "https://jalview.org", "https://www.jalview.org/", "<!doctype",
-            "Jalview is a" },
+            "Jalview is a", true },
         /*
          */
         //