Skip to content

Commit

Permalink
Replace apache httpclient with NettyHttpClient in test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunquakes committed Oct 19, 2024
1 parent 89602bd commit c95671c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sunquakes.jsonrpc4j.discovery;
package com.sunquakes.jsonrpc4j.client;

import com.sunquakes.jsonrpc4j.JsonRpcProtocol;
import com.sunquakes.jsonrpc4j.utils.RequestUtils;
Expand Down Expand Up @@ -30,9 +30,9 @@ public class NettyHttpClient {

private static final int DEFAULT_HTTPS_PORT = 443;

private URI uri;
private final URI uri;

NettyHttpClient(String host) {
public NettyHttpClient(String host) {
uri = URI.create(host);
}

Expand Down Expand Up @@ -107,7 +107,7 @@ protected void initChannel(Channel ch) throws Exception {
}
}

class HttpResponseHandler extends SimpleChannelInboundHandler<FullHttpResponse> {
static class HttpResponseHandler extends SimpleChannelInboundHandler<FullHttpResponse> {

Promise<FullHttpResponse> promise;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sunquakes.jsonrpc4j.discovery;

import com.sunquakes.jsonrpc4j.JsonRpcProtocol;
import com.sunquakes.jsonrpc4j.client.NettyHttpClient;
import com.sunquakes.jsonrpc4j.utils.AddressUtils;
import com.sunquakes.jsonrpc4j.utils.JSONUtils;
import io.netty.buffer.ByteBuf;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/sunquakes/jsonrpc4j/discovery/Nacos.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sunquakes.jsonrpc4j.discovery;

import com.sunquakes.jsonrpc4j.client.NettyHttpClient;
import com.sunquakes.jsonrpc4j.exception.JsonRpcException;
import com.sunquakes.jsonrpc4j.utils.JSONUtils;
import io.netty.buffer.ByteBuf;
Expand Down Expand Up @@ -43,7 +44,7 @@ public class Nacos implements Driver {

private String ephemeral = "true";

private List<Map.Entry<String, Service>> heartbeatList = new ArrayList<>();
private final List<Map.Entry<String, Service>> heartbeatList = new ArrayList<>();

@Override
public Nacos newClient(String url) {
Expand Down Expand Up @@ -163,7 +164,7 @@ public class GetResp {
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Service {
public static class Service {
private String ip;
private Integer port;
private String instanceId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@

import com.sunquakes.jsonrpc4j.dto.RequestDto;
import com.sunquakes.jsonrpc4j.utils.JSONUtils;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.util.CharsetUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -22,12 +15,14 @@
import org.springframework.test.context.junit.jupiter.SpringExtension;

import javax.annotation.Resource;
import java.io.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -52,7 +47,7 @@ public void beforeTest() throws UnsupportedEncodingException {
}

@Test
void testRequest() throws IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
void testRequest() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, ExecutionException, InterruptedException {
@Data
@AllArgsConstructor
class Params {
Expand All @@ -63,19 +58,11 @@ class Params {
RequestDto requestDto = new RequestDto("1234567890", "2.0", "JsonRpc/add", params);
String request = JSONUtils.toString(requestDto);

SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
});
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
HttpPost httpPost = new HttpPost("https://localhost:3205");
httpPost.setEntity(new StringEntity(request, ContentType.APPLICATION_JSON));
HttpResponse response = httpClient.execute(httpPost);
assertEquals("{\"id\":\"1234567890\",\"jsonrpc\":\"2.0\",\"result\":3}", EntityUtils.toString(response.getEntity()));
NettyHttpClient httpClient = new NettyHttpClient("https://localhost:3205");
FullHttpResponse res = httpClient.post("", request);
ByteBuf buf = res.content();
String body = buf.toString(CharsetUtil.UTF_8);
assertEquals("{\"id\":\"1234567890\",\"jsonrpc\":\"2.0\",\"result\":3}", body);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sunquakes.jsonrpc4j.discovery;

import com.sunquakes.jsonrpc4j.client.NettyHttpClient;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package com.sunquakes.jsonrpc4j.server;

import com.sunquakes.jsonrpc4j.client.NettyHttpClient;
import com.sunquakes.jsonrpc4j.dto.RequestDto;
import com.sunquakes.jsonrpc4j.utils.JSONUtils;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.util.CharsetUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand All @@ -28,34 +25,34 @@ class JsonRpcHttpServerTest {

@Data
@AllArgsConstructor
class Params {
static class Params {
int a;
int b;
}

@Test
void testHandle() throws IOException {
void testHandle() throws ExecutionException, InterruptedException {
Params params = new Params(1, 2);
RequestDto requestDto = new RequestDto("1234567890", "2.0", "JsonRpc/add", params);
String request = JSONUtils.toString(requestDto);

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://localhost:3200");
httpPost.setEntity(new StringEntity(request, ContentType.APPLICATION_JSON));
HttpResponse response = httpClient.execute(httpPost);
assertEquals("{\"id\":\"1234567890\",\"jsonrpc\":\"2.0\",\"result\":3}", EntityUtils.toString(response.getEntity()));
NettyHttpClient httpClient = new NettyHttpClient("http://localhost:3200");
FullHttpResponse res = httpClient.post("", request);
ByteBuf buf = res.content();
String body = buf.toString(CharsetUtil.UTF_8);
assertEquals("{\"id\":\"1234567890\",\"jsonrpc\":\"2.0\",\"result\":3}", body);
}

@Test
void testMethod() throws IOException {
void testMethod() throws ExecutionException, InterruptedException {
Params params = new Params(3, 4);
RequestDto requestDto = new RequestDto("1234567890", "2.0", "json_rpc/add", params);
String request = JSONUtils.toString(requestDto);

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://localhost:3200");
httpPost.setEntity(new StringEntity(request.toString(), ContentType.APPLICATION_JSON));
HttpResponse response = httpClient.execute(httpPost);
assertEquals("{\"id\":\"1234567890\",\"jsonrpc\":\"2.0\",\"result\":7}", EntityUtils.toString(response.getEntity()));
NettyHttpClient httpClient = new NettyHttpClient("http://localhost:3200");
FullHttpResponse res = httpClient.post("", request);
ByteBuf buf = res.content();
String body = buf.toString(CharsetUtil.UTF_8);
assertEquals("{\"id\":\"1234567890\",\"jsonrpc\":\"2.0\",\"result\":7}", body);
}
}

0 comments on commit c95671c

Please sign in to comment.