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 |
|
|
|
|
| 0% |
Uncovered Elements: 33 (33) |
Complexity: 6 |
Complexity Density: 0.24 |
|
15 |
|
public class HttpUtilsTest |
16 |
|
{ |
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
4-
|
|
17 |
0 |
@Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations")... |
18 |
|
public void testFollowConnection(String targetUrl, String finalUrl, |
19 |
|
String notUsed0, String notUsed1) throws IOException |
20 |
|
{ |
21 |
0 |
URL tUrl = new URL(targetUrl); |
22 |
0 |
URL fUrl = new URL(finalUrl); |
23 |
0 |
HttpURLConnection conn1 = HttpUtils |
24 |
|
.followConnection((HttpURLConnection) tUrl.openConnection()); |
25 |
0 |
URL url1 = conn1.getURL(); |
26 |
0 |
Assert.assertEquals(url1, fUrl, "Final URL is not the same."); |
27 |
|
} |
28 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
4-
|
|
29 |
0 |
@Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations")... |
30 |
|
public void testOpenConnection(String targetUrl, String finalUrl, |
31 |
|
String notUsed0, String notUsed1) throws IOException |
32 |
|
{ |
33 |
0 |
URL tUrl = new URL(targetUrl); |
34 |
0 |
URL fUrl = new URL(finalUrl); |
35 |
0 |
HttpURLConnection conn1 = (HttpURLConnection) HttpUtils |
36 |
|
.openConnection(tUrl); |
37 |
0 |
URL url1 = conn1.getURL(); |
38 |
0 |
Assert.assertEquals(url1, fUrl, "Final URL is not the same."); |
39 |
|
} |
40 |
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 3 |
Complexity Density: 0.21 |
4-
|
|
41 |
0 |
@Test(groups = { "Network" }, dataProvider = "urlTargetsAndDestinations")... |
42 |
|
public void testOpenStream(String targetUrl, String finalUrl, |
43 |
|
String inFirstLine, String inDocument) throws IOException |
44 |
|
{ |
45 |
0 |
URL tUrl = new URL(targetUrl); |
46 |
0 |
URL fUrl = new URL(finalUrl); |
47 |
0 |
InputStream is1 = HttpUtils.openStream(tUrl); |
48 |
0 |
BufferedReader br1 = new BufferedReader(new InputStreamReader(is1)); |
49 |
0 |
String firstLine = br1.readLine().toLowerCase(Locale.ROOT); |
50 |
0 |
Assert.assertTrue( |
51 |
|
firstLine.contains(inFirstLine.toLowerCase(Locale.ROOT)), |
52 |
|
"First line of text '" + firstLine + "' does not contain '" |
53 |
|
+ inFirstLine + "'"); |
54 |
0 |
String inDocumentLC = inDocument.toLowerCase(Locale.ROOT); |
55 |
0 |
boolean found = false; |
56 |
0 |
String line = null; |
57 |
0 |
while ((line = br1.readLine()) != null) |
58 |
|
{ |
59 |
0 |
if (line.toLowerCase(Locale.ROOT).contains(inDocumentLC)) |
60 |
|
{ |
61 |
0 |
found = true; |
62 |
0 |
break; |
63 |
|
} |
64 |
|
} |
65 |
0 |
Assert.assertTrue(found, |
66 |
|
"Text '" + inDocument + "' not found in '" + finalUrl + "'"); |
67 |
|
} |
68 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
69 |
0 |
@DataProvider(name = "urlTargetsAndDestinations")... |
70 |
|
public Object[][] urlTargetsAndDestinations() |
71 |
|
{ |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
0 |
return new Object[][] { |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
{ "http://jalview.org/", "https://www.jalview.org/", "<!doctype", |
83 |
|
"Jalview is a" }, |
84 |
|
{ "http://www.jalview.org/", "https://www.jalview.org/", |
85 |
|
"<!doctype", "Jalview is a" }, |
86 |
|
{ "https://jalview.org", "https://www.jalview.org/", "<!doctype", |
87 |
|
"Jalview is a" }, |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
}; |
92 |
|
} |
93 |
|
|
94 |
|
} |