use of org.rx.bean.MultiValueMap in project rxlib by RockyLOMO.
the class SocksTester method httpServer.
@SneakyThrows
@Test
public void httpServer() {
ManualResetEvent wait = new ManualResetEvent();
Map<String, Object> qs = new HashMap<>();
qs.put("a", "1");
qs.put("b", "乐之");
Map<String, Object> f = new HashMap<>();
f.put("a", "1");
f.put("b", "乐之");
Map<String, IOStream<?, ?>> fi = new HashMap<>();
fi.put("a", IOStream.wrap("1.dat", new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }));
String j = "{\"a\":1,\"b\":\"乐之\"}";
String hbody = "<html><body>hello world</body></html>";
String jbody = "{\"code\":0,\"msg\":\"hello world\"}";
HttpServer server = new HttpServer(8081, true);
server.requestMapping("/api", (request, response) -> {
MultiValueMap<String, String> queryString = request.getQueryString();
for (Map.Entry<String, Object> entry : qs.entrySet()) {
assert entry.getValue().equals(queryString.getFirst(entry.getKey()));
}
MultiValueMap<String, String> form = request.getForm();
for (Map.Entry<String, Object> entry : f.entrySet()) {
assert entry.getValue().equals(form.getFirst(entry.getKey()));
}
MultiValueMap<String, FileUpload> files = request.getFiles();
for (Map.Entry<String, IOStream<?, ?>> entry : fi.entrySet()) {
FileUpload fileUpload = files.getFirst(entry.getKey());
try {
Arrays.equals(fileUpload.get(), entry.getValue().toArray());
} catch (IOException e) {
e.printStackTrace();
}
}
response.htmlBody(hbody);
}).requestMapping("/json", (request, response) -> {
String json = request.jsonBody();
assert j.equals(json);
response.jsonBody(jbody);
wait.set();
});
RxConfig.INSTANCE.setLogStrategy(LogStrategy.ALWAYS);
HttpClient client = new HttpClient();
client.setEnableLog(true);
assert hbody.equals(client.post(HttpClient.buildUrl("https://127.0.0.1:8081/api", qs), f, fi).toString());
String resJson = client.postJson("https://127.0.0.1:8081/json", j).toString();
JSONObject jobj = client.postJson("https://127.0.0.1:8081/json", j).toJson();
System.out.println(jobj);
System.out.println(resJson);
assert jbody.equals(resJson);
wait.waitOne();
}
Aggregations