Clover icon

Coverage Report

  1. Project Clover database Thu Nov 28 2024 11:45:30 GMT
  2. Package jalview.util

File HttpUtilsTest.java

 

Code metrics

4
25
4
1
104
84
6
0.24
6.25
4
1.5

Classes

Class Line # Actions
HttpUtilsTest 15 25 6
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

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  0 toggle @Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations")
18    public void testFollowConnection(String targetUrl, String finalUrl,
19    String notUsed0, String notUsed1, boolean followAnyway)
20    throws IOException
21    {
22  0 URL tUrl = new URL(targetUrl);
23  0 URL fUrl = new URL(finalUrl);
24  0 HttpURLConnection conn1 = HttpUtils.followConnection(
25    (HttpURLConnection) tUrl.openConnection(), followAnyway);
26  0 URL url1 = conn1.getURL();
27  0 Assert.assertEquals(url1, fUrl, "Final URL is not the same.");
28    }
29   
 
30  0 toggle @Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations")
31    public void testOpenConnection(String targetUrl, String finalUrl,
32    String notUsed0, String notUsed1, boolean followAnyway)
33    throws IOException
34    {
35  0 URL tUrl = new URL(targetUrl);
36  0 URL fUrl = new URL(finalUrl);
37  0 HttpURLConnection conn1 = (HttpURLConnection) HttpUtils
38    .openConnection(tUrl, followAnyway);
39  0 URL url1 = conn1.getURL();
40  0 Assert.assertEquals(url1, fUrl, "Final URL is not the same.");
41    }
42   
 
43  0 toggle @Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations")
44    public void testOpenStream(String targetUrl, String finalUrl,
45    String inFirstLine, String inDocument, boolean followAnyway)
46    throws IOException
47    {
48  0 URL tUrl = new URL(targetUrl);
49  0 URL fUrl = new URL(finalUrl);
50  0 InputStream is1 = HttpUtils.openStream(tUrl, followAnyway);
51  0 BufferedReader br1 = new BufferedReader(new InputStreamReader(is1));
52  0 String firstLine = br1.readLine().toLowerCase(Locale.ROOT);
53  0 Assert.assertTrue(
54    firstLine.contains(inFirstLine.toLowerCase(Locale.ROOT)),
55    "First line of text '" + firstLine + "' does not contain '"
56    + inFirstLine + "'");
57  0 String inDocumentLC = inDocument.toLowerCase(Locale.ROOT);
58  0 boolean found = false;
59  0 String line = null;
60  0 while ((line = br1.readLine()) != null)
61    {
62  0 if (line.toLowerCase(Locale.ROOT).contains(inDocumentLC))
63    {
64  0 found = true;
65  0 break;
66    }
67    }
68  0 Assert.assertTrue(found,
69    "Text '" + inDocument + "' not found in '" + finalUrl + "'");
70    }
71   
 
72  0 toggle @DataProvider(name = "urlTargetsAndDestinations")
73    public Object[][] urlTargetsAndDestinations()
74    {
75    /*
76    String targetUrl, // the URL you ask for
77    String finalUrl, // the URL you end up at
78    String foundInFirstLine, // some text found in the first line
79    String foundInDocument, // some text found in the document (and won't be in an error page)
80    boolean followAnyway, // whether to follow redirects even if they're not http->https
81    */
82  0 return new Object[][] {
83    //
84    /*
85    */
86    { "http://jalview.org/", "https://www.jalview.org/", "<!doctype",
87    "Jalview is a", false },
88    { "http://www.jalview.org/", "https://www.jalview.org/",
89    "<!doctype", "Jalview is a", false },
90    { "https://jalview.org/", "https://jalview.org/", "<!doctype",
91    "Jalview is a", false },
92    { "http://jalview.org/", "https://www.jalview.org/", "<!doctype",
93    "Jalview is a", true },
94    { "http://www.jalview.org/", "https://www.jalview.org/",
95    "<!doctype", "Jalview is a", true },
96    { "https://jalview.org", "https://www.jalview.org/", "<!doctype",
97    "Jalview is a", true },
98    /*
99    */
100    //
101    };
102    }
103   
104    }