Repository navigation
Expand file tree
/
Copy pathSimpleTests.java
More file actions
86 lines (75 loc) · 2.93 KB
/
Copy pathSimpleTests.java
File metadata and controls
86 lines (75 loc) · 2.93 KB
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
package cn.zhxu.okhttps;
import okhttp3.mockwebserver.MockResponse;
import org.junit.Assert;
import org.junit.Test;
public class SimpleTests extends BaseTest {
@Test
public void testSyncPost() throws InterruptedException {
http.async("/").bodyType(OkHttps.JSON).post();
Assert.assertEquals(server.takeRequest().getHeader("Content-Type"), "application/json; charset=UTF-8");
http.async("/").bodyType(OkHttps.FORM).post();
Assert.assertEquals(server.takeRequest().getHeader("Content-Type"), "application/x-www-form-urlencoded; charset=UTF-8");
}
@Test
public void test() {
HttpResult res = HTTP.builder()
.build()
.sync("http://www.baidu.com")
.bodyType("application/x-www-form-urlencoded")
.addBodyPara("name", "Jack")
.addBodyPara("age", 11)
.post()
.close();
System.out.println(res);
}
/**
* 同步请求示例
* 同步请求直接得到结果,无需设置回调
*/
@Test
public void testSyncToString() {
server.enqueue(new MockResponse().setBody("Hello World!"));
String hello = http.sync("/users") // http://localhost:8080/users
.get() // GET请求
.getBody() // 获取响应报文体
.toString(); // 得到目标数据
Assert.assertEquals(hello, "Hello World!");
}
@Test
public void testUserAgent() throws InterruptedException {
http.async("/user").addHeader("User-Agent", "123456").get();
String userAgent = server.takeRequest().getHeader("User-Agent");
Assert.assertEquals("123456", userAgent);
}
// @Test
// public void testSyncToList() {
// User u1 = new User(1, "Jack");
// User u2 = new User(2, "Tom");
// List<User> list = Arrays.asList(u1, u2);
// server.enqueue(new MockResponse().setBody(JSON.toJSONString(list)));
//
// List<User> users = http.sync("/users") // http://localhost:8080/users
// .get() // GET请求
// .getBody() // 获取响应报文体
// .toList(User.class); // 得到目标数据
//
// assertEquals(u1, users.get(0));
// assertEquals(u2, users.get(1));
// }
//
// /**
// * 异步请求示例
// * 异步请求在OnResponse里接收请求结果
// */
// @Test
// public void testAsync() {
// http.async("/users/1") // http://api.demo.com/users/1
// .setOnResponse((HttpResult result) -> {
// // 得到目标数据
// User user = result.getBody().toBean(User.class);
// System.out.println("user = " + user);
// })
// .get();
// sleep(5000);
// }
}