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;
9 import java.util.Locale;
11 import org.testng.Assert;
12 import org.testng.annotations.DataProvider;
13 import org.testng.annotations.Test;
15 public class HttpUtilsTest
17 @Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations")
18 public void testFollowConnection(String targetUrl, String finalUrl,
19 String notUsed0, String notUsed1) throws IOException
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.");
29 @Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations")
30 public void testOpenConnection(String targetUrl, String finalUrl,
31 String notUsed0, String notUsed1) throws IOException
33 URL tUrl = new URL(targetUrl);
34 URL fUrl = new URL(finalUrl);
35 HttpURLConnection conn1 = (HttpURLConnection) HttpUtils
36 .openConnection(tUrl);
37 URL url1 = conn1.getURL();
38 Assert.assertEquals(url1, fUrl, "Final URL is not the same.");
41 @Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations")
42 public void testOpenStream(String targetUrl, String finalUrl,
43 String inFirstLine, String inDocument) throws IOException
45 URL tUrl = new URL(targetUrl);
46 URL fUrl = new URL(finalUrl);
47 InputStream is1 = HttpUtils.openStream(tUrl);
48 BufferedReader br1 = new BufferedReader(new InputStreamReader(is1));
49 String firstLine = br1.readLine().toLowerCase(Locale.ROOT);
51 firstLine.contains(inFirstLine.toLowerCase(Locale.ROOT)),
52 "First line of text '" + firstLine + "' does not contain '"
54 String inDocumentLC = inDocument.toLowerCase(Locale.ROOT);
55 boolean found = false;
57 while ((line = br1.readLine()) != null)
59 if (line.toLowerCase(Locale.ROOT).contains(inDocumentLC))
65 Assert.assertTrue(found,
66 "Text '" + inDocument + "' not found in '" + finalUrl + "'");
69 @DataProvider(name = "urlTargetsAndDestinations")
70 public Object[][] urlTargetsAndDestinations()
73 String targetUrl, // the URL you ask for
74 String finalUrl, // the URL you end up at
75 String foundInFirstLine, // some text found in the first line
76 String foundInDocument, // some text found in the document (and won't be in an error page)
78 return new Object[][] {
82 { "http://jalview.org/", "https://www.jalview.org/", "<!doctype",
84 { "http://www.jalview.org/", "https://www.jalview.org/",
85 "<!doctype", "Jalview is a" },
86 { "https://jalview.org", "https://www.jalview.org/", "<!doctype",