use of com.alipay.sofa.rpc.server.http.HttpService in project sofa-rpc by sofastack.
the class Http2ClearTextTimeoutTest method testProtobuf.
@Test
public void testProtobuf() throws InterruptedException {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setDaemon(true);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl(1000)).setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setRegister(false);
providerConfig.export();
{
ConsumerConfig<HttpService> consumerConfig = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setTimeout(500).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C);
HttpService httpService = consumerConfig.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
try {
EchoResponse response = httpService.echoPb(request);
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof SofaTimeOutException);
}
}
{
ConsumerConfig<HttpService> consumerConfig2 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setTimeout(500).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY).setRepeatedReferLimit(-1);
HttpService httpService2 = consumerConfig2.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
try {
httpService2.echoPb(request);
// NOT SUPPORTED NOW, If want support this, need add key to head.
Assert.fail();
} catch (Exception e) {
}
}
{
ConsumerConfig<HttpService> consumerConfig3 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setTimeout(500).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setRepeatedReferLimit(-1);
HttpService httpService3 = consumerConfig3.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
EchoResponse response = httpService3.echoPb(request);
Assert.assertNull(response);
ResponseFuture future = RpcInvokeContext.getContext().getFuture();
try {
response = (EchoResponse) future.get();
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof SofaTimeOutException);
}
}
{
final Object[] result = new Object[1];
final CountDownLatch latch = new CountDownLatch(1);
ConsumerConfig<HttpService> consumerConfig4 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setTimeout(500).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setOnReturn(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
result[0] = appResponse;
latch.countDown();
}
@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
result[0] = throwable;
latch.countDown();
}
@Override
public void onSofaException(SofaRpcException exception, String methodName, RequestBase request) {
result[0] = exception;
latch.countDown();
}
}).setRepeatedReferLimit(-1);
HttpService httpService4 = consumerConfig4.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
EchoResponse response = httpService4.echoPb(request);
Assert.assertNull(response);
latch.await(2000, TimeUnit.MILLISECONDS);
Throwable throwable = (Throwable) result[0];
Assert.assertTrue(throwable instanceof SofaTimeOutException);
}
}
use of com.alipay.sofa.rpc.server.http.HttpService in project sofa-rpc by sofastack.
the class Http2ClearTextBadRequestTest method testAll.
@Test
public void testAll() throws Exception {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12333).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setDaemon(true);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setUniqueId("uuu").setRegister(false);
providerConfig.export();
ClientTransportConfig clientTransportConfig = new ClientTransportConfig();
clientTransportConfig.setProviderInfo(ProviderHelper.toProviderInfo("h2c://127.0.0.1:12333"));
Http2ClientTransport clientTransport = new Http2ClientTransport(clientTransportConfig);
clientTransport.connect();
{
// GET 图标
String url = "/favicon.ico";
FullHttpRequest httpRequest = buildHttpRequest(url);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(200, handler.response.status().code());
Assert.assertTrue(handler.content.length == 0);
}
{
// 其它未知命令
String url = "/com.alipay.sofa.rpc.server.http.HttpService/add";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.setMethod(HttpMethod.OPTIONS);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(400, handler.response.status().code());
Assert.assertTrue(StringUtils.isNotEmpty(getStringContent(handler)));
}
{
// HEAD 不存在的服务
String url = "/com.alipay.sofa.rpc.server.http.HttpService12313/add";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.setMethod(HttpMethod.HEAD);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(404, handler.response.status().code());
}
{
// HEAD 不存在的方法
String url = "/com.alipay.sofa.rpc.server.http.HttpService:uuu/xasdasdasd";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.setMethod(HttpMethod.HEAD);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(404, handler.response.status().code());
}
{
// HEAD 存在的方法
String url = "/com.alipay.sofa.rpc.server.http.HttpService:uuu/add";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.setMethod(HttpMethod.HEAD);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(200, handler.response.status().code());
}
{
// POST 异常:地址不对
String url = "/com.alipay.sofa";
FullHttpRequest httpRequest = buildHttpRequest(url);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(400, handler.response.status().code());
Assert.assertTrue(getStringContent(handler).contains("ip:port"));
}
{
// POST 未知序列化
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService/asdasdas?code=xxx";
FullHttpRequest httpRequest = buildHttpRequest(url);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(400, handler.response.status().code());
Assert.assertNotNull(getStringContent(handler));
}
{
// POST 不存在的接口
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService/echoPb";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.headers().add(RemotingConstants.HEAD_SERIALIZE_TYPE, "protobuf");
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(404, handler.response.status().code());
Assert.assertNotNull(getStringContent(handler));
}
{
// POST 不存在的方法
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/adasdada";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.headers().add(RemotingConstants.HEAD_SERIALIZE_TYPE, "protobuf");
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(404, handler.response.status().code());
Assert.assertNotNull(getStringContent(handler));
}
{
// POST 不传 HEAD_SERIALIZE_TYPE
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/echoPb";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.headers().add(HttpHeaderNames.CONTENT_TYPE, "application/x-protobuf");
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(200, handler.response.status().code());
Assert.assertNotNull(getStringContent(handler));
EchoResponse response = EchoResponse.parseFrom(handler.content);
Assert.assertEquals("helloxxx", response.getMessage());
}
{
// POST 正常
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/echoPb";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.headers().add(RemotingConstants.HEAD_SERIALIZE_TYPE, "protobuf");
httpRequest.headers().add(RemotingConstants.HEAD_TARGET_APP, "serverApp1");
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(200, handler.response.status().code());
EchoResponse response = EchoResponse.parseFrom(handler.content);
Assert.assertEquals("helloxxx", response.getMessage());
}
}
Aggregations