Search in sources :

Example 1 with YarMessageRouter

use of com.weibo.api.motan.protocol.yar.YarMessageRouter in project motan by weibocom.

the class YarMessageHandlerWarpperTest method testHandle.

@Test
public void testHandle() throws Exception {
    YarRequest yarRequest = new YarRequest(123, "JSON", "testmethod", new Object[] { "params", 456 });
    final YarResponse yarResponse = YarProtocolUtil.buildDefaultErrorResponse("test err", "JSON");
    YarMessageHandlerWarpper handler = new YarMessageHandlerWarpper(new YarMessageRouter() {

        @Override
        public Object handle(Channel channel, Object message) {
            AttachmentRequest request = (AttachmentRequest) message;
            verifyAttachments(request.getAttachments());
            return yarResponse;
        }
    });
    FullHttpResponse httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(yarRequest, uri));
    assertNotNull(httpResponse);
    assertNotNull(httpResponse.content());
    YarResponse retYarResponse = getYarResponse(httpResponse);
    assertNotNull(retYarResponse);
    assertEquals(yarResponse, retYarResponse);
}
Also used : YarMessageRouter(com.weibo.api.motan.protocol.yar.YarMessageRouter) YarRequest(com.weibo.yar.YarRequest) Channel(com.weibo.api.motan.transport.Channel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) YarResponse(com.weibo.yar.YarResponse) AttachmentRequest(com.weibo.api.motan.protocol.yar.AttachmentRequest) Test(org.junit.Test)

Example 2 with YarMessageRouter

use of com.weibo.api.motan.protocol.yar.YarMessageRouter in project motan by weibocom.

the class YarMessageHandlerWarpperTest method testAbnormal.

@Test
public void testAbnormal() throws Exception {
    final String errmsg = "rpc process error";
    YarMessageHandlerWarpper handler = new YarMessageHandlerWarpper(new YarMessageRouter() {

        @Override
        public Object handle(Channel channel, Object message) {
            throw new RuntimeException(errmsg);
        }
    });
    // yar协议无法解析
    FullHttpResponse httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(null, uri));
    assertNotNull(httpResponse);
    assertEquals(HttpResponseStatus.OK, httpResponse.getStatus());
    YarResponse retYarResponse = getYarResponse(httpResponse);
    assertNotNull(retYarResponse);
    assertNotNull(retYarResponse.getError());
    // yar协议可以正常解析,但后续处理异常
    YarRequest yarRequest = new YarRequest(123, "JSON", "testmethod", new Object[] { "params", 456 });
    httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(yarRequest, uri));
    assertNotNull(httpResponse);
    assertEquals(HttpResponseStatus.OK, httpResponse.getStatus());
    retYarResponse = getYarResponse(httpResponse);
    assertNotNull(retYarResponse);
    assertEquals(errmsg, retYarResponse.getError());
}
Also used : YarMessageRouter(com.weibo.api.motan.protocol.yar.YarMessageRouter) YarRequest(com.weibo.yar.YarRequest) Channel(com.weibo.api.motan.transport.Channel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) YarResponse(com.weibo.yar.YarResponse) Test(org.junit.Test)

Aggregations

YarMessageRouter (com.weibo.api.motan.protocol.yar.YarMessageRouter)2 Channel (com.weibo.api.motan.transport.Channel)2 YarRequest (com.weibo.yar.YarRequest)2 YarResponse (com.weibo.yar.YarResponse)2 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)2 Test (org.junit.Test)2 AttachmentRequest (com.weibo.api.motan.protocol.yar.AttachmentRequest)1