diff --git a/README.md b/README.md index 3ee04e0b..6bd26d21 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ Run test: | 4 | Proto Root Directory | Root directory contains proto files | | 5 | Library Directory (Optional) | Using a different underlying library (googleapis) | | 6 | Full Method | Full Method to test | -| 7 | Metadata | Store token, authentication method, format: key1:value1,key2:value2 | +| 7 | Metadata | Store token, authentication method, format: key1:value1,key2:value2, value should url encode with utf-8 | | 8 | Deadline | How long gRPC clients are willing to wait for an RPC to complete | | 9 | Send JSON Format With the Request | Data request with JSON format | diff --git a/src/main/java/vn/zalopay/benchmark/core/ClientCaller.java b/src/main/java/vn/zalopay/benchmark/core/ClientCaller.java index 9033d937..99c9e469 100644 --- a/src/main/java/vn/zalopay/benchmark/core/ClientCaller.java +++ b/src/main/java/vn/zalopay/benchmark/core/ClientCaller.java @@ -21,6 +21,9 @@ import vn.zalopay.benchmark.core.protobuf.ServiceResolver; import vn.zalopay.benchmark.core.specification.GrpcResponse; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.util.LinkedHashMap; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -90,7 +93,13 @@ private Map buildHashMetadata(String metadata) { Preconditions.checkArgument(keyValue.length == 2, "Metadata entry must be defined in key1:value1,key2:value2 format: " + metadata); - metadataHash.put(keyValue[0], keyValue[1]); + String value = keyValue[1]; + try { + value = URLDecoder.decode(keyValue[1], StandardCharsets.UTF_8.name()); + } catch (UnsupportedEncodingException ignored) { + } + + metadataHash.put(keyValue[0], value); } return metadataHash; diff --git a/src/test/java/vn/zalopay/benchmark/core/client/ClientCallerTest.java b/src/test/java/vn/zalopay/benchmark/core/client/ClientCallerTest.java index 8f5cb46d..d0af1286 100644 --- a/src/test/java/vn/zalopay/benchmark/core/client/ClientCallerTest.java +++ b/src/test/java/vn/zalopay/benchmark/core/client/ClientCallerTest.java @@ -16,6 +16,10 @@ import javax.net.ssl.SSLException; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; + import static org.mockito.Mockito.any; public class ClientCallerTest extends BaseTest { @@ -108,6 +112,16 @@ public void testCanSendGrpcUnaryRequestWithMetaData() { Assert.assertTrue(resp.getGrpcMessageString().contains("\"theme\": \"Hello server")); } + @Test + public void testCanSendGrpcUnaryRequestWithEncodedMetaData() throws UnsupportedEncodingException { + clientCaller = new ClientCaller(HOST_PORT, PROTO_WITH_EXTERNAL_IMPORT_FOLDER.toString(), LIB_FOLDER.toString(), FULL_METHOD, false, false, "tracestate:" + URLEncoder.encode("a=3,b:4", StandardCharsets.UTF_8.name())); + clientCaller.buildRequest(REQUEST_JSON); + GrpcResponse resp = clientCaller.call("2000"); + clientCaller.shutdownNettyChannel(); + Assert.assertNotNull(resp); + Assert.assertTrue(resp.getGrpcMessageString().contains("\"theme\": \"Hello server")); + } + @Test public void testCanSendGrpcUnaryRequestWithSSLAndDisableSSLVerification() { clientCaller = new ClientCaller(HOST_PORT_TLS, PROTO_WITH_EXTERNAL_IMPORT_FOLDER.toString(), LIB_FOLDER.toString(), FULL_METHOD, true, true, METADATA);