-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUrlBuilderUnitTest.java
183 lines (157 loc) · 7.1 KB
/
UrlBuilderUnitTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package cn.imkarl.urlbuilder;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
/**
* UrlBuilder单元测试
*/
public class UrlBuilderUnitTest {
private static final String TAG = "UrlBuilder";
private static void log(Object msg) {
System.out.println(TAG+": "+(msg==null ? "[NULL]" : String.valueOf(msg)));
}
@Test
public void testSimple() throws Exception {
final String url = "http://www.baidu.com/";
String buildUrl = new UrlBuilder().host("www.baidu.com").build();
log("------------ testSimple ------------");
log("original=\t"+url);
log("buildUrl=\t"+buildUrl);
assertTrue(url.equals(buildUrl));
}
@Test
public void testHost() throws Exception {
final String url = "https://www.baidu.com/";
String buildUrl = new UrlBuilder().scheme("https").host("www.baidu.com").build();
log("------------ testHost ------------");
log("original=\t"+url);
log("buildUrl=\t"+buildUrl);
assertTrue(url.equals(buildUrl));
}
@Test
public void testHost2() throws Exception {
final String url = "https://www.baidu.com/";
String buildUrl = new UrlBuilder().scheme("https").host("www.baidu.com").path(new UrlPath().endTag(false)).build();
log("------------ testHost2 ------------");
log("original=\t"+url);
log("buildUrl=\t"+buildUrl);
assertTrue(url.equals(buildUrl));
}
@Test
public void testHostByPort() throws Exception {
final String url = "https://www.baidu.com:8090/";
String buildUrl = new UrlBuilder().scheme("https").host("www.baidu.com").port(8090).build();
log("------------ testHost2 ------------");
log("original=\t"+url);
log("buildUrl=\t"+buildUrl);
assertTrue(url.equals(buildUrl));
}
@Test
public void testQuery() throws Exception {
final String url = "https://www.baidu.com/s?ie=UTF-8&wd=test";
String buildUrl = new UrlBuilder().scheme("https").host("www.baidu.com").appendPath("/s").appendQuery("ie", "UTF-8").appendQuery("wd", "test").build();
log("------------ testQuery ------------");
log("original=\t"+url);
log("buildUrl=\t"+buildUrl);
assertTrue(url.equals(buildUrl));
}
@Test
public void testQueryByChinese() throws Exception {
final String url = "https://www.baidu.com/s?ie=UTF-8&wd=%E6%B5%8B%E8%AF%95";
String buildUrl = new UrlBuilder().scheme("https").host("www.baidu.com").appendPath("/s").appendQuery("ie", "UTF-8").appendQuery("wd", "测试").build();
log("------------ testQueryByChinese ------------");
log("original=\t"+url);
log("buildUrl=\t"+buildUrl);
assertTrue(url.equals(buildUrl));
}
@Test
public void testQueryByMulti() throws Exception {
final String url = "https://www.baidu.com/s?ie=UTF-8&ie=GBK&wd=%E6%B5%8B%E8%AF%95";
String buildUrl = new UrlBuilder().scheme("https").host("www.baidu.com").appendPath("/s").appendQuery("ie", "UTF-8").appendQuery("ie", "GBK").appendQuery("wd", "测试").build();
log("------------ testQueryByMulti ------------");
log("original=\t"+url);
log("buildUrl=\t"+buildUrl);
assertTrue(url.equals(buildUrl));
}
@Test
public void testQueryByOverride() throws Exception {
final String url = "https://www.baidu.com/s?ie=GBK&wd=%E6%B5%8B%E8%AF%95";
String buildUrl = new UrlBuilder().scheme("https").host("www.baidu.com").appendPath("/s").putQuery("ie", "UTF-8").putQuery("ie", "GBK").putQuery("wd", "测试").build();
log("------------ testQueryByOverride ------------");
log("original=\t"+url);
log("buildUrl=\t"+buildUrl);
assertTrue(url.equals(buildUrl));
}
@Test
public void testFragment() throws Exception {
final String url = "https://www.baidu.com/#abc";
String buildUrl = new UrlBuilder().scheme("https").host("www.baidu.com").fragment("abc").build();
log("------------ testFragment ------------");
log("original=\t"+url);
log("buildUrl=\t"+buildUrl);
assertTrue(url.equals(buildUrl));
}
@Test
public void testFragmentByChinese() throws Exception {
final String url = "https://www.baidu.com/#%E6%B5%8B%E8%AF%95";
String buildUrl = new UrlBuilder().scheme("https").host("www.baidu.com").fragment("测试").build();
log("------------ testFragmentByPath ------------");
log("original=\t"+url);
log("buildUrl=\t"+buildUrl);
assertTrue(url.equals(buildUrl));
}
@Test
public void testFragmentByPath() throws Exception {
final String url = "https://www.baidu.com/#abc";
String buildUrl = new UrlBuilder().scheme("https").host("www.baidu.com").fragment("abc").build();
log("------------ testFragmentByPath ------------");
log("original=\t"+url);
log("buildUrl=\t"+buildUrl);
assertTrue(url.equals(buildUrl));
}
@Test
public void testFragmentByPath2() throws Exception {
final String url = "https://www.baidu.com/s#abc";
String buildUrl = new UrlBuilder().scheme("https").host("www.baidu.com").appendPath("/s").fragment("abc").build();
log("------------ testFragmentByPath ------------");
log("original=\t"+url);
log("buildUrl=\t"+buildUrl);
assertTrue(url.equals(buildUrl));
}
@Test
public void testFragmentByPathAndQuery() throws Exception {
final String url = "https://www.baidu.com/s?wd=test#abc";
String buildUrl = new UrlBuilder().scheme("https").host("www.baidu.com").appendPath("/s").appendQuery("wd", "test").fragment("abc").build();
log("------------ testFragmentByPathAndQuery ------------");
log("original=\t"+url);
log("buildUrl=\t"+buildUrl);
assertTrue(url.equals(buildUrl));
}
@Test
public void testFragmentByQuery() throws Exception {
final String url = "https://www.baidu.com/?wd=test#abc";
String buildUrl = new UrlBuilder().scheme("https").host("www.baidu.com").appendQuery("wd", "test").fragment("abc").build();
log("------------ testFragmentByPathAndQuery ------------");
log("original=\t"+url);
log("buildUrl=\t"+buildUrl);
assertTrue(url.equals(buildUrl));
}
@Test
public void testComplex() throws Exception {
final String url = "https://www.baidu.com/s?ie=UTF-8&wd=%E6%B5%8B%E8%AF%95&tfflag=1&abc=one&abc=two#bbb";
String buildUrl = new UrlBuilder().scheme("https").host("www.baidu.com").appendPath("/s")
.appendQuery("ie", "UTF-8")
.putQuery("wd", "test")
.putQuery("wd", "测试")
.appendQuery("tfflag", "0")
.putQuery("tfflag", "1")
.appendQuery("abc", "one")
.appendQuery("abc", "two")
.fragment("aaa")
.fragment("bbb")
.build();
log("------------ testComplex ------------");
log("original=\t"+url);
log("buildUrl=\t"+buildUrl);
assertTrue(url.equals(buildUrl));
}
}