Search in sources :

Example 91 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project riposte by Nike-Inc.

the class RequestInfoImplTest method netty_helper_constructor_populates_request_info_appropriately.

@Test
public void netty_helper_constructor_populates_request_info_appropriately() {
    // given
    String uri = "/some/uri/path/%24foobar%26?foo=bar&secondparam=secondvalue";
    Map<String, List<String>> expectedQueryParamMap = new HashMap<>();
    expectedQueryParamMap.put("foo", Arrays.asList("bar"));
    expectedQueryParamMap.put("secondparam", Arrays.asList("secondvalue"));
    HttpMethod method = HttpMethod.PATCH;
    String cookieName = UUID.randomUUID().toString();
    String cookieValue = UUID.randomUUID().toString();
    String content = UUID.randomUUID().toString();
    byte[] contentBytes = content.getBytes();
    Charset contentCharset = CharsetUtil.UTF_8;
    ByteBuf contentByteBuf = Unpooled.copiedBuffer(contentBytes);
    HttpHeaders headers = new DefaultHttpHeaders().add("header1", "val1").add(HttpHeaders.Names.CONTENT_TYPE, contentCharset).add(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE).add(HttpHeaders.Names.COOKIE, ClientCookieEncoder.LAX.encode(cookieName, cookieValue));
    HttpHeaders trailingHeaders = new DefaultHttpHeaders().add("trailingHeader1", "trailingVal1");
    HttpVersion protocolVersion = HttpVersion.HTTP_1_1;
    FullHttpRequest nettyRequestMock = mock(FullHttpRequest.class);
    doReturn(uri).when(nettyRequestMock).getUri();
    doReturn(method).when(nettyRequestMock).getMethod();
    doReturn(headers).when(nettyRequestMock).headers();
    doReturn(trailingHeaders).when(nettyRequestMock).trailingHeaders();
    doReturn(contentByteBuf).when(nettyRequestMock).content();
    doReturn(protocolVersion).when(nettyRequestMock).getProtocolVersion();
    // when
    RequestInfoImpl<?> requestInfo = new RequestInfoImpl<>(nettyRequestMock);
    // then
    assertThat("getUri was not the same value sent in", requestInfo.getUri(), is(uri));
    assertThat("getPath did not decode as expected", requestInfo.getPath(), is("/some/uri/path/$foobar&"));
    assertThat(requestInfo.getMethod(), is(method));
    assertThat(requestInfo.getHeaders(), is(headers));
    assertThat(requestInfo.getTrailingHeaders(), is(trailingHeaders));
    assertThat(requestInfo.getQueryParams(), notNullValue());
    assertThat(requestInfo.getQueryParams().parameters(), is(expectedQueryParamMap));
    assertThat(requestInfo.getCookies(), is(Sets.newHashSet(new DefaultCookie(cookieName, cookieValue))));
    assertThat(requestInfo.pathTemplate, nullValue());
    assertThat(requestInfo.pathParams.isEmpty(), is(true));
    assertThat(requestInfo.getRawContentBytes(), is(contentBytes));
    assertThat(requestInfo.getRawContent(), is(content));
    assertThat(requestInfo.content, nullValue());
    assertThat(requestInfo.getContentCharset(), is(contentCharset));
    assertThat(requestInfo.getProtocolVersion(), is(protocolVersion));
    assertThat(requestInfo.isKeepAliveRequested(), is(true));
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HashMap(java.util.HashMap) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) List(java.util.List) HttpVersion(io.netty.handler.codec.http.HttpVersion) HttpMethod(io.netty.handler.codec.http.HttpMethod) Test(org.junit.Test)

Example 92 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project motan by weibocom.

the class YarMessageHandlerWarpper method handle.

@Override
public Object handle(Channel channel, Object message) {
    FullHttpRequest httpRequest = (FullHttpRequest) message;
    String uri = httpRequest.getUri();
    // should not be null
    int index = uri.indexOf("?");
    String requestPath = uri;
    Map<String, String> attachments = null;
    if (index > -1) {
        requestPath = uri.substring(0, index);
        if (index != uri.length() - 1) {
            attachments = getAttachMents(uri.substring(index + 1, uri.length()));
        }
    }
    YarResponse yarResponse = null;
    String packagerName = "JSON";
    try {
        ByteBuf buf = httpRequest.content();
        final byte[] contentBytes = new byte[buf.readableBytes()];
        buf.getBytes(0, contentBytes);
        YarRequest yarRequest = new AttachmentRequest(YarProtocol.buildRequest(contentBytes), attachments);
        yarRequest.setRequestPath(requestPath);
        yarResponse = (YarResponse) orgHandler.handle(channel, yarRequest);
    } catch (Exception e) {
        LoggerUtil.error("YarMessageHandlerWarpper handle yar request fail.", e);
        yarResponse = YarProtocolUtil.buildDefaultErrorResponse(e.getMessage(), packagerName);
    }
    byte[] responseBytes;
    try {
        responseBytes = YarProtocol.toProtocolBytes(yarResponse);
    } catch (IOException e) {
        throw new MotanFrameworkException("convert yar response to bytes fail.", e);
    }
    FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(responseBytes));
    httpResponse.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/x-www-form-urlencoded");
    httpResponse.headers().set(HttpHeaders.Names.CONTENT_LENGTH, httpResponse.content().readableBytes());
    if (HttpHeaders.isKeepAlive(httpRequest)) {
        httpResponse.headers().set(HttpHeaders.Names.CONNECTION, Values.KEEP_ALIVE);
    } else {
        httpResponse.headers().set(HttpHeaders.Names.CONNECTION, Values.CLOSE);
    }
    return httpResponse;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) YarRequest(com.weibo.yar.YarRequest) YarResponse(com.weibo.yar.YarResponse) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) IOException(java.io.IOException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) AttachmentRequest(com.weibo.api.motan.protocol.yar.AttachmentRequest)

Example 93 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project motan by weibocom.

the class NettyHttpRequestHandlerTest method testServerStatus.

@Test
public void testServerStatus() throws Exception {
    final MessageHandler messageHandler = mockery.mock(MessageHandler.class);
    final ChannelHandlerContext ctx = mockery.mock(ChannelHandlerContext.class);
    mockery.checking(new Expectations() {

        {
            allowing(ctx).write(with(any(FullHttpResponse.class)));
            will(new CustomAction("verify") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    verifyStatus((FullHttpResponse) invocation.getParameter(0));
                    return null;
                }
            });
            allowing(ctx).flush();
            will(returnValue(null));
            allowing(ctx).close();
            will(returnValue(null));
            allowing(messageHandler).handle(with(any(Channel.class)), with(anything()));
            will(returnValue(null));
        }
    });
    FullHttpRequest httpRequest = buildHttpRequest(NettyHttpRequestHandler.ROOT_PATH);
    NettyHttpRequestHandler handler = new NettyHttpRequestHandler(null, messageHandler);
    // 关闭心跳开关
    MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, false);
    handler.channelRead0(ctx, httpRequest);
    // 打开心跳开关
    MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);
    handler.channelRead0(ctx, httpRequest);
}
Also used : Expectations(org.jmock.Expectations) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) MessageHandler(com.weibo.api.motan.transport.MessageHandler) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) Channel(com.weibo.api.motan.transport.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Test(org.junit.Test)

Example 94 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project motan by weibocom.

the class NettyHttpRequestHandlerTest method buildHttpRequest.

private FullHttpRequest buildHttpRequest(String requestPath) throws Exception {
    PooledByteBufAllocator allocator = new PooledByteBufAllocator();
    ByteBuf buf = allocator.buffer(0);
    FullHttpRequest httpReqeust = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, requestPath, buf);
    return httpReqeust;
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator)

Example 95 with FullHttpRequest

use of io.netty.handler.codec.http.FullHttpRequest in project motan by weibocom.

the class YarMessageHandlerWarpperTest method buildHttpRequest.

private FullHttpRequest buildHttpRequest(YarRequest yarRequest, String requestPath) throws Exception {
    PooledByteBufAllocator allocator = new PooledByteBufAllocator();
    ByteBuf buf = allocator.buffer(2048, 1024 * 1024);
    if (yarRequest != null) {
        buf.writeBytes(YarProtocol.toProtocolBytes(yarRequest));
    }
    FullHttpRequest httpReqeust = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, requestPath, buf);
    return httpReqeust;
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator)

Aggregations

FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)97 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)65 Test (org.junit.Test)51 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)32 AsciiString (io.netty.util.AsciiString)25 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 ByteBuf (io.netty.buffer.ByteBuf)19 ChannelPromise (io.netty.channel.ChannelPromise)16 HttpResponse (io.netty.handler.codec.http.HttpResponse)13 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)12 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)11 FullHttpMessage (io.netty.handler.codec.http.FullHttpMessage)10 URI (java.net.URI)10 Http2CodecUtil.getEmbeddedHttp2Exception (io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception)9 Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)9 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)8 ChannelFuture (io.netty.channel.ChannelFuture)7 Channel (io.netty.channel.Channel)6 NullDispatcher (org.elasticsearch.http.NullDispatcher)6 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)5