-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetTest.java
More file actions
140 lines (119 loc) · 4.18 KB
/
Copy pathNetTest.java
File metadata and controls
140 lines (119 loc) · 4.18 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
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
/*
* Copyright (c) 2020 Copyright bp All Rights Reserved.
* Author: pengxiang.li
* Desc:
*/
package cn.brainpoint.febs;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.junit.Test;
import cn.brainpoint.febs.libs.net.Request;
import cn.brainpoint.febs.libs.promise.IPromise;
/**
* @author pengxiang.li
*/
public class NetTest {
@Test
public void testGet() {
String tag = "Net get: ";
Log.out("========================================");
Log.out(tag + "begin");
Request req = new Request("https://www.baidu.com", "sdfsfddsf", "get");
try {
Febs.Net.fetch(req).then((res) -> {
return res.text();
}).then((res) -> {
Log.out(tag + res);
}).execute().get();
} catch (ExecutionException e) {
Log.err(tag + e.getMessage());
}
}
@Test
public void testText() {
String tag = "Net text: ";
Log.out("========================================");
Log.out(tag + "begin");
ArrayList<IPromise> all = new ArrayList<>();
for (int i = 0; i < 1; i++) {// code.
Log.out(tag + " " + i);
IPromise p = Febs.Net.fetch("https://www.baidu.com").then(res -> {
// code.
Log.out(tag + res.getStatusCode() + " " + res.getStatusMsg());
// headers.
Set<String> keySet = res.getHeaderKeySet();
Log.out(tag + "header: " + keySet.size());
Log.out(tag + "header: " + keySet.toString());
Iterator<String> it1 = keySet.iterator();
while (it1.hasNext()) {
String ID = it1.next();
Log.out(tag + "header: " + ID);
Collection<String> el = res.getHeaders(ID);
Log.out(tag + el);
}
return res.text();
}).then(res -> {
Log.out(tag + res);
return res;
}).fail((e) -> {
e.printStackTrace(System.err);
Log.err(tag + e.getMessage());
}).finish(() -> {
Log.out(tag + "dump: " + Promise.dumpDebug());
});
all.add(p);
}
try {
String ret = (String) Promise.all(all).then((res) -> {
Log.out(tag + " finish: " + res.toString());
return res.toString();
}).fail(e -> {
e.printStackTrace();
}).execute().get();
Log.out(tag + " show in future", ret);
} catch (Exception e) {
Log.err(tag + e.getMessage());
}
}
@Test
public void testBlob() {
String tag = "Net Blob: ";
Log.out("========================================");
Log.out(tag + "begin");
try {
String ret = (String) Febs.Net.fetch("http://www.baidu.com").then(res -> {
return res.blob();
}).then((res) -> {
BufferedReader in = (BufferedReader) res;
char buf[] = new char[1024];
while (in.read(buf, 0, buf.length) != -1) {
Log.out(tag + " %s ", Arrays.toString(buf));
Arrays.fill(buf, '\0');
}
// important to call close().
in.close();
return "success";
}).fail((e) -> {
Log.err(tag + e.getMessage());
}).finish(() -> {
Log.out(tag + "dump: " + Promise.dumpDebug());
}).execute().get(1000, TimeUnit.MILLISECONDS);
if (!"success".equals(ret)) {
Log.err(tag + "return error");
} else {
Log.out(tag + "success");
}
} catch (TimeoutException e) {
Log.err(tag + "timeout");
} catch (ExecutionException e) {
Log.err(tag + e.getMessage());
}
}
}