JAL-4420 Tests for new FileUtils methods
[jalview.git] / test / jalview / util / HttpUtilsTest.java
1 package jalview.util;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.net.HttpURLConnection;
8 import java.net.URL;
9 import java.util.Locale;
10
11 import org.testng.Assert;
12 import org.testng.annotations.DataProvider;
13 import org.testng.annotations.Test;
14
15 public class HttpUtilsTest
16 {
17   @Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations")
18   public void testFollowConnection(String targetUrl, String finalUrl,
19           String notUsed0, String notUsed1) throws IOException
20   {
21     URL tUrl = new URL(targetUrl);
22     URL fUrl = new URL(finalUrl);
23     HttpURLConnection conn1 = HttpUtils
24             .followConnection((HttpURLConnection) tUrl.openConnection());
25     URL url1 = conn1.getURL();
26     Assert.assertEquals(url1, fUrl, "Final URL is not the same.");
27   }
28
29   @Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations")
30   public void testOpenConnection(String targetUrl, String finalUrl,
31           String notUsed0, String notUsed1) throws IOException
32   {
33     URL tUrl = new URL(targetUrl);
34     URL fUrl = new URL(finalUrl);
35     HttpURLConnection conn1 = HttpUtils.openConnection(tUrl);
36     URL url1 = conn1.getURL();
37     Assert.assertEquals(url1, fUrl, "Final URL is not the same.");
38   }
39
40   @Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations")
41   public void testOpenStream(String targetUrl, String finalUrl,
42           String inFirstLine, String inDocument) throws IOException
43   {
44     URL tUrl = new URL(targetUrl);
45     URL fUrl = new URL(finalUrl);
46     InputStream is1 = HttpUtils.openStream(tUrl);
47     BufferedReader br1 = new BufferedReader(new InputStreamReader(is1));
48     String firstLine = br1.readLine().toLowerCase(Locale.ROOT);
49     Assert.assertTrue(
50             firstLine.contains(inFirstLine.toLowerCase(Locale.ROOT)),
51             "First line of text '" + firstLine + "' does not contain '"
52                     + inFirstLine + "'");
53     String inDocumentLC = inDocument.toLowerCase(Locale.ROOT);
54     boolean found = false;
55     String line = null;
56     while ((line = br1.readLine()) != null)
57     {
58       if (line.toLowerCase(Locale.ROOT).contains(inDocumentLC))
59       {
60         found = true;
61         break;
62       }
63     }
64     Assert.assertTrue(found,
65             "Text '" + inDocument + "' not found in '" + finalUrl + "'");
66   }
67
68   @DataProvider(name = "urlTargetsAndDestinations")
69   public Object[][] urlTargetsAndDestinations()
70   {
71     /*
72     String targetUrl, // the URL you ask for
73     String finalUrl, // the URL you end up at
74     String foundInFirstLine, // some text found in the first line
75     String foundInDocument, // some text found in the document (and won't be in an error page)
76     */
77     return new Object[][] {
78         //
79         /*
80          */
81         { "http://jalview.org/", "https://www.jalview.org/", "<!doctype",
82             "Jalview is a" },
83         { "http://www.jalview.org/", "https://www.jalview.org/",
84             "<!doctype", "Jalview is a" },
85         { "https://jalview.org", "https://www.jalview.org/", "<!doctype",
86             "Jalview is a" },
87         /*
88          */
89         //
90     };
91   }
92
93 }