Search in sources :

Example 6 with HttpService

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);
    }
}
Also used : EchoResponse(com.alipay.sofa.rpc.server.bolt.pb.EchoResponse) EchoRequest(com.alipay.sofa.rpc.server.bolt.pb.EchoRequest) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) ResponseFuture(com.alipay.sofa.rpc.message.ResponseFuture) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServiceImpl(com.alipay.sofa.rpc.server.http.HttpServiceImpl) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) HttpService(com.alipay.sofa.rpc.server.http.HttpService) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 7 with HttpService

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());
    }
}
Also used : EchoResponse(com.alipay.sofa.rpc.server.bolt.pb.EchoResponse) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) HttpService(com.alipay.sofa.rpc.server.http.HttpService) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ClientTransportConfig(com.alipay.sofa.rpc.transport.ClientTransportConfig) HttpServiceImpl(com.alipay.sofa.rpc.server.http.HttpServiceImpl) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Aggregations

ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)7 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)7 HttpService (com.alipay.sofa.rpc.server.http.HttpService)7 HttpServiceImpl (com.alipay.sofa.rpc.server.http.HttpServiceImpl)7 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)7 Test (org.junit.Test)7 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)6 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)6 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)5 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)5 ResponseFuture (com.alipay.sofa.rpc.message.ResponseFuture)5 EchoRequest (com.alipay.sofa.rpc.server.bolt.pb.EchoRequest)5 EchoResponse (com.alipay.sofa.rpc.server.bolt.pb.EchoResponse)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)2 ExampleObj (com.alipay.sofa.rpc.server.http.ExampleObj)2 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)1 ClientTransportConfig (com.alipay.sofa.rpc.transport.ClientTransportConfig)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1