Search in sources :

Example 1 with MultiValueMap

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();
}
Also used : DnsServer(org.rx.net.dns.DnsServer) Bytes(org.rx.io.Bytes) java.util(java.util) SneakyThrows(lombok.SneakyThrows) org.rx.test.bean(org.rx.test.bean) Extends.eq(org.rx.core.Extends.eq) Upstream(org.rx.net.socks.upstream.Upstream) InvalidException(org.rx.exception.InvalidException) DnsClient(org.rx.net.dns.DnsClient) CipherKind(org.rx.net.shadowsocks.encryption.CipherKind) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) org.rx.net.http(org.rx.net.http) CollectionUtils(org.apache.commons.collections4.CollectionUtils) InetAddress(java.net.InetAddress) TripleAction(org.rx.util.function.TripleAction) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) org.rx.net.socks(org.rx.net.socks) Arrays(org.rx.core.Arrays) NameserverConfig(org.rx.net.nameserver.NameserverConfig) Extends.sleep(org.rx.core.Extends.sleep) LogStrategy(org.rx.bean.LogStrategy) Socks5UdpUpstream(org.rx.net.socks.upstream.Socks5UdpUpstream) AESUtil(org.rx.security.AESUtil) App(org.rx.core.App) Main(org.rx.Main) org.rx.core(org.rx.core) ShadowsocksConfig(org.rx.net.shadowsocks.ShadowsocksConfig) RandomList(org.rx.bean.RandomList) NameserverImpl(org.rx.net.nameserver.NameserverImpl) org.rx.net.rpc(org.rx.net.rpc) FileUpload(io.netty.handler.codec.http.multipart.FileUpload) NameserverClient(org.rx.net.nameserver.NameserverClient) java.util.concurrent(java.util.concurrent) ShadowsocksServer(org.rx.net.shadowsocks.ShadowsocksServer) org.rx.net.support(org.rx.net.support) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) MultiValueMap(org.rx.bean.MultiValueMap) Slf4j(lombok.extern.slf4j.Slf4j) JSONObject(com.alibaba.fastjson.JSONObject) SUID(org.rx.bean.SUID) IOStream(org.rx.io.IOStream) org.rx.net(org.rx.net) Socks5Upstream(org.rx.net.socks.upstream.Socks5Upstream) IOException(java.io.IOException) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) IOStream(org.rx.io.IOStream) MultiValueMap(org.rx.bean.MultiValueMap) FileUpload(io.netty.handler.codec.http.multipart.FileUpload) Test(org.junit.jupiter.api.Test) SneakyThrows(lombok.SneakyThrows)

Aggregations

JSONObject (com.alibaba.fastjson.JSONObject)1 ByteBuf (io.netty.buffer.ByteBuf)1 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 StandardCharsets (java.nio.charset.StandardCharsets)1 java.util (java.util)1 java.util.concurrent (java.util.concurrent)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 SneakyThrows (lombok.SneakyThrows)1 Slf4j (lombok.extern.slf4j.Slf4j)1 CollectionUtils (org.apache.commons.collections4.CollectionUtils)1 Test (org.junit.jupiter.api.Test)1 Main (org.rx.Main)1 LogStrategy (org.rx.bean.LogStrategy)1 MultiValueMap (org.rx.bean.MultiValueMap)1 RandomList (org.rx.bean.RandomList)1 SUID (org.rx.bean.SUID)1