|
| 1 | +/* |
| 2 | + * Tencent is pleased to support the open source community by making tRPC available. |
| 3 | + * |
| 4 | + * Copyright (C) 2023 THL A29 Limited, a Tencent company. |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * If you have downloaded a copy of the tRPC source code from Tencent, |
| 8 | + * please note that tRPC source code is licensed under the Apache 2.0 License, |
| 9 | + * A copy of the Apache 2.0 License can be found in the LICENSE file. |
| 10 | + */ |
| 11 | + |
| 12 | +package com.tencent.trpc.proto.http.common; |
| 13 | + |
| 14 | +import static com.tencent.trpc.proto.http.common.HttpConstants.HTTP_SCHEME; |
| 15 | +import static com.tencent.trpc.proto.http.constant.Constant.TEST_BYTES_REQ_KEY; |
| 16 | +import static com.tencent.trpc.proto.http.constant.Constant.TEST_BYTES_REQ_VALUE; |
| 17 | +import static com.tencent.trpc.proto.http.constant.Constant.TEST_BYTES_RSP_KEY; |
| 18 | +import static com.tencent.trpc.proto.http.constant.Constant.TEST_BYTES_RSP_VALUE; |
| 19 | +import static com.tencent.trpc.proto.http.constant.Constant.TEST_MESSAGE; |
| 20 | +import static com.tencent.trpc.proto.http.constant.Constant.TEST_RSP_MESSAGE; |
| 21 | +import static com.tencent.trpc.proto.http.constant.Constant.TEST_STRING_REQ_KEY; |
| 22 | +import static com.tencent.trpc.proto.http.constant.Constant.TEST_STRING_REQ_VALUE; |
| 23 | +import static com.tencent.trpc.proto.http.constant.Constant.TEST_STRING_RSP_KEY; |
| 24 | +import static com.tencent.trpc.proto.http.constant.Constant.TEST_STRING_RSP_VALUE; |
| 25 | +import static com.tencent.trpc.transport.http.common.Constants.HTTP2_SCHEME; |
| 26 | + |
| 27 | +import com.tencent.trpc.core.common.ConfigManager; |
| 28 | +import com.tencent.trpc.core.common.config.BackendConfig; |
| 29 | +import com.tencent.trpc.core.common.config.ConsumerConfig; |
| 30 | +import com.tencent.trpc.core.common.config.ProviderConfig; |
| 31 | +import com.tencent.trpc.core.common.config.ServerConfig; |
| 32 | +import com.tencent.trpc.core.common.config.ServiceConfig; |
| 33 | +import com.tencent.trpc.core.rpc.RpcClientContext; |
| 34 | +import com.tencent.trpc.core.utils.NetUtils; |
| 35 | +import java.nio.charset.StandardCharsets; |
| 36 | +import java.util.HashMap; |
| 37 | +import org.apache.http.HttpHeaders; |
| 38 | +import org.junit.AfterClass; |
| 39 | +import org.junit.Assert; |
| 40 | +import org.junit.BeforeClass; |
| 41 | +import org.junit.Test; |
| 42 | +import tests.service.GreeterService; |
| 43 | +import tests.service.HelloRequestProtocol.HelloRequest; |
| 44 | +import tests.service.HelloRequestProtocol.HelloResponse; |
| 45 | +import tests.service.impl1.GreeterServiceImpl2; |
| 46 | + |
| 47 | +public class HttpTransparentInfoTest { |
| 48 | + |
| 49 | + private static ServerConfig serverConfig; |
| 50 | + |
| 51 | + private static int TRPC_JAVA_TEST_HTTP2_PORT; |
| 52 | + |
| 53 | + private static int TRPC_JAVA_TEST_HTTP_PORT; |
| 54 | + |
| 55 | + @BeforeClass |
| 56 | + public static void startHttpServer() { |
| 57 | + ConfigManager.stopTest(); |
| 58 | + ConfigManager.startTest(); |
| 59 | + |
| 60 | + ProviderConfig<GreeterService> providerConfig = new ProviderConfig<>(); |
| 61 | + providerConfig.setServiceInterface(GreeterService.class); |
| 62 | + providerConfig.setRef(new GreeterServiceImpl2()); |
| 63 | + HashMap<String, ServiceConfig> providers = new HashMap<>(); |
| 64 | + |
| 65 | + TRPC_JAVA_TEST_HTTP2_PORT = NetUtils.getAvailablePort(); |
| 66 | + ServiceConfig serviceConfig1 = getServiceConfig(providerConfig, "trpc.java.test.http2", |
| 67 | + NetUtils.LOCAL_HOST, TRPC_JAVA_TEST_HTTP2_PORT, HTTP2_SCHEME, "jetty"); |
| 68 | + providers.put(serviceConfig1.getName(), serviceConfig1); |
| 69 | + |
| 70 | + TRPC_JAVA_TEST_HTTP_PORT = NetUtils.getAvailablePort(); |
| 71 | + ServiceConfig serviceConfig2 = getServiceConfig(providerConfig, "trpc.java.test.http", |
| 72 | + NetUtils.LOCAL_HOST, TRPC_JAVA_TEST_HTTP_PORT, HTTP_SCHEME, "jetty"); |
| 73 | + providers.put(serviceConfig2.getName(), serviceConfig2); |
| 74 | + |
| 75 | + ServerConfig sc = new ServerConfig(); |
| 76 | + sc.setServiceMap(providers); |
| 77 | + sc.setApp("http-test-app"); |
| 78 | + sc.setLocalIp("127.0.0.1"); |
| 79 | + sc.init(); |
| 80 | + |
| 81 | + serverConfig = sc; |
| 82 | + } |
| 83 | + |
| 84 | + private static ServiceConfig getServiceConfig(ProviderConfig gspc, String name, |
| 85 | + String ip, int port, String protocol, String transport) { |
| 86 | + ServiceConfig serviceConfig = new ServiceConfig(); |
| 87 | + serviceConfig.setName(name); |
| 88 | + serviceConfig.getProviderConfigs().add(gspc); |
| 89 | + serviceConfig.setIp(ip); |
| 90 | + serviceConfig.setPort(port); |
| 91 | + serviceConfig.setProtocol(protocol); |
| 92 | + serviceConfig.setTransporter(transport); |
| 93 | + return serviceConfig; |
| 94 | + } |
| 95 | + |
| 96 | + @AfterClass |
| 97 | + public static void stopHttpServer() { |
| 98 | + ConfigManager.stopTest(); |
| 99 | + if (serverConfig != null) { |
| 100 | + serverConfig.stop(); |
| 101 | + serverConfig = null; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private static HelloRequest createPbRequest(String msg) { |
| 106 | + HelloRequest.Builder builder = HelloRequest.newBuilder(); |
| 107 | + builder.setMessage(msg); |
| 108 | + return builder.build(); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testHttp2RpcClient() { |
| 113 | + BackendConfig backendConfig = new BackendConfig(); |
| 114 | + ConsumerConfig<GreeterService> consumerConfig = new ConsumerConfig<>(); |
| 115 | + backendConfig.setName("serviceId"); |
| 116 | + backendConfig.setRequestTimeout(10000); |
| 117 | + consumerConfig.setServiceInterface(GreeterService.class); |
| 118 | + consumerConfig.setBackendConfig(backendConfig); |
| 119 | + backendConfig.setNamingUrl("ip://127.0.0.1:" + TRPC_JAVA_TEST_HTTP2_PORT); |
| 120 | + backendConfig.setKeepAlive(false); |
| 121 | + backendConfig.setConnsPerAddr(4); |
| 122 | + backendConfig.setProtocol(HTTP2_SCHEME); |
| 123 | + try { |
| 124 | + GreeterService proxy = consumerConfig.getProxy(); |
| 125 | + |
| 126 | + RpcClientContext context = new RpcClientContext(); |
| 127 | + // client tran info to server |
| 128 | + context.getReqAttachMap().put(TEST_STRING_REQ_KEY, TEST_STRING_REQ_VALUE); |
| 129 | + context.getReqAttachMap().put(TEST_BYTES_REQ_KEY, TEST_BYTES_REQ_VALUE); |
| 130 | + context.getReqAttachMap().put(HttpHeaders.CONTENT_LENGTH, TEST_MESSAGE.length()); |
| 131 | + |
| 132 | + HelloResponse helloResponse = proxy.sayHello(context, createPbRequest(TEST_MESSAGE)); |
| 133 | + // get server tran info |
| 134 | + byte[] bytesRspValue = (byte[]) context.getRspAttachMap().get(TEST_BYTES_RSP_KEY); |
| 135 | + Assert.assertArrayEquals(bytesRspValue, TEST_BYTES_RSP_VALUE); |
| 136 | + |
| 137 | + byte[] stringRspValue = (byte[]) context.getRspAttachMap().get(TEST_STRING_RSP_KEY); |
| 138 | + Assert.assertEquals(new String(stringRspValue, StandardCharsets.UTF_8), TEST_STRING_RSP_VALUE); |
| 139 | + |
| 140 | + Assert.assertNotNull(helloResponse); |
| 141 | + String rspMessage = helloResponse.getMessage(); |
| 142 | + Assert.assertEquals(rspMessage, TEST_RSP_MESSAGE); |
| 143 | + } finally { |
| 144 | + backendConfig.stop(); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void testHttpRpcClient() { |
| 150 | + BackendConfig backendConfig = new BackendConfig(); |
| 151 | + ConsumerConfig<GreeterService> consumerConfig = new ConsumerConfig<>(); |
| 152 | + backendConfig.setName("serviceId"); |
| 153 | + backendConfig.setRequestTimeout(10000); |
| 154 | + consumerConfig.setServiceInterface(GreeterService.class); |
| 155 | + consumerConfig.setBackendConfig(backendConfig); |
| 156 | + backendConfig.setNamingUrl("ip://127.0.0.1:" + TRPC_JAVA_TEST_HTTP_PORT); |
| 157 | + backendConfig.setKeepAlive(false); |
| 158 | + backendConfig.setConnsPerAddr(4); |
| 159 | + backendConfig.setProtocol(HTTP_SCHEME); |
| 160 | + try { |
| 161 | + GreeterService proxy = consumerConfig.getProxy(); |
| 162 | + |
| 163 | + RpcClientContext context = new RpcClientContext(); |
| 164 | + context.getReqAttachMap().put(TEST_STRING_REQ_KEY, TEST_STRING_REQ_VALUE); |
| 165 | + context.getReqAttachMap().put(TEST_BYTES_REQ_KEY, TEST_BYTES_REQ_VALUE); |
| 166 | + |
| 167 | + HelloResponse helloResponse = proxy.sayHello(context, createPbRequest(TEST_MESSAGE)); |
| 168 | + // get server tran info |
| 169 | + byte[] bytesRspValue = (byte[]) context.getRspAttachMap().get(TEST_BYTES_RSP_KEY); |
| 170 | + Assert.assertArrayEquals(bytesRspValue, TEST_BYTES_RSP_VALUE); |
| 171 | + |
| 172 | + byte[] stringRspValue = (byte[]) context.getRspAttachMap().get(TEST_STRING_RSP_KEY); |
| 173 | + Assert.assertEquals(new String(stringRspValue, StandardCharsets.UTF_8), TEST_STRING_RSP_VALUE); |
| 174 | + |
| 175 | + Assert.assertNotNull(helloResponse); |
| 176 | + String rspMessage = helloResponse.getMessage(); |
| 177 | + Assert.assertEquals(rspMessage, TEST_RSP_MESSAGE); |
| 178 | + } finally { |
| 179 | + backendConfig.stop(); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments