Search in sources :

Example 41 with HttpRequest

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

the class VerifyDecoderFailedResultIsHandledTest method proxy_endpoints_should_handle_decode_exception.

@Test
public void proxy_endpoints_should_handle_decode_exception() throws Exception {
    // given
    int payloadSize = 10000;
    ByteBuf payload = ComponentTestUtils.createByteBufPayload(payloadSize);
    HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, RouterEndpoint.MATCHING_PATH, payload);
    //leave off content-length and transfer-encoding to trigger DecoderFailedResult
    request.headers().set(HttpHeaders.Names.HOST, "localhost");
    // when
    Pair<Integer, String> serverResponse = executeRequest(request, proxyServerConfig.endpointsPort(), incompleteCallTimeoutMillis);
    // then
    assertErrorResponse(serverResponse);
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) ProxyRouterEndpoint(com.nike.riposte.server.http.ProxyRouterEndpoint) StandardEndpoint(com.nike.riposte.server.http.StandardEndpoint) Endpoint(com.nike.riposte.server.http.Endpoint) Test(org.junit.Test)

Example 42 with HttpRequest

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

the class HttpUtilsTest method extractCookies_works_if_cookies_defined_in_headers.

@Test
public void extractCookies_works_if_cookies_defined_in_headers() {
    // given
    Cookie cookie1 = new DefaultCookie(UUID.randomUUID().toString(), UUID.randomUUID().toString());
    Cookie cookie2 = new DefaultCookie(UUID.randomUUID().toString(), UUID.randomUUID().toString());
    HttpHeaders headers = new DefaultHttpHeaders().add(HttpHeaders.Names.COOKIE, ClientCookieEncoder.LAX.encode(cookie1, cookie2));
    HttpRequest nettyRequestMock = mock(HttpRequest.class);
    doReturn(headers).when(nettyRequestMock).headers();
    // when
    Set<Cookie> extractedCookies = HttpUtils.extractCookies(nettyRequestMock);
    // then
    assertThat(extractedCookies.contains(cookie1), is(true));
    assertThat(extractedCookies.contains(cookie2), is(true));
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Test(org.junit.Test)

Example 43 with HttpRequest

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

the class HttpUtilsTest method extractCookies_does_not_use_trailing_headers_if_trailing_headers_is_null.

@Test
public void extractCookies_does_not_use_trailing_headers_if_trailing_headers_is_null() {
    // given
    HttpRequest nettyRequestMock = mock(HttpRequest.class);
    doReturn(new DefaultHttpHeaders()).when(nettyRequestMock).headers();
    // when
    Set<Cookie> extractedCookies = HttpUtils.extractCookies(nettyRequestMock);
    // then
    assertThat(extractedCookies, notNullValue());
    assertThat(extractedCookies.isEmpty(), is(true));
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Cookie(io.netty.handler.codec.http.cookie.Cookie) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Test(org.junit.Test)

Example 44 with HttpRequest

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

the class RequestInfoImpl method getMultipartParts.

/**
     * {@inheritDoc}
     */
@Override
public synchronized List<InterfaceHttpData> getMultipartParts() {
    if (!isMultipartRequest() || !isCompleteRequestWithAllChunks())
        return null;
    if (multipartData == null) {
        byte[] contentBytes = getRawContentBytes();
        HttpRequest fullHttpRequestForMultipartDecoder = (contentBytes == null) ? new DefaultFullHttpRequest(getProtocolVersion(), getMethod(), getUri()) : new DefaultFullHttpRequest(getProtocolVersion(), getMethod(), getUri(), Unpooled.wrappedBuffer(contentBytes));
        fullHttpRequestForMultipartDecoder.headers().add(getHeaders());
        multipartData = new HttpPostMultipartRequestDecoder(new DefaultHttpDataFactory(false), fullHttpRequestForMultipartDecoder, getContentCharset());
    }
    return multipartData.getBodyHttpDatas();
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpPostMultipartRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostMultipartRequestDecoder) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory)

Example 45 with HttpRequest

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

the class VerifyProxyRequestsDoNotAlterRequestToDownstreamServiceTest method proxy_endpoints_should_honor_chunked_transfer_encoding.

@Test
public void proxy_endpoints_should_honor_chunked_transfer_encoding() throws Exception {
    // given
    int payloadSize = 10000;
    ByteBuf payload = createPayload(payloadSize);
    String payloadString = payload.toString(CharsetUtil.UTF_8);
    HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, RouterEndpoint.MATCHING_PATH, payload);
    request.headers().set(HttpHeaders.Names.HOST, "localhost");
    request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    request.headers().set(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
    // when
    Pair<Integer, String> serverResponse = executeRequest(request, proxyServerConfig.endpointsPort(), incompleteCallTimeoutMillis);
    // then
    assertThat(serverResponse.getRight()).isEqualTo(DownstreamEndpoint.RESPONSE_PAYLOAD);
    assertThat(serverResponse.getLeft()).isEqualTo(HttpResponseStatus.OK.code());
    assertProxyAndDownstreamServiceHeadersAndTracingHeadersAdded();
    String proxyBody = extractBodyFromRawRequest(proxyServerRequest.toString());
    String downstreamBody = extractBodyFromRawRequest(downstreamServerRequest.toString());
    //assert request was sent in chunks
    //https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1
    assertThat(downstreamBody).contains("\r\n");
    assertThat(proxyBody).contains("\r\n");
    //assert bodies are equal
    String proxyBodyMinusChunkInfo = extractFullBodyFromChunks(proxyBody);
    String downstreamBodyMinusChunkInfo = extractFullBodyFromChunks(downstreamBody);
    //assert proxy and downstream have same bodies
    assertThat(proxyBodyMinusChunkInfo).isEqualTo(downstreamBodyMinusChunkInfo);
    //assert input payload matches proxy and downstream payloads
    assertThat(proxyBodyMinusChunkInfo).isEqualTo(payloadString);
    assertThat(downstreamBodyMinusChunkInfo).isEqualTo(payloadString);
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) ProxyRouterEndpoint(com.nike.riposte.server.http.ProxyRouterEndpoint) Endpoint(com.nike.riposte.server.http.Endpoint) StandardEndpoint(com.nike.riposte.server.http.StandardEndpoint) Test(org.junit.Test)

Aggregations

HttpRequest (io.netty.handler.codec.http.HttpRequest)114 Test (org.junit.Test)38 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)29 HttpResponse (io.netty.handler.codec.http.HttpResponse)26 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)22 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)19 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)17 ByteBuf (io.netty.buffer.ByteBuf)13 HttpContent (io.netty.handler.codec.http.HttpContent)13 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)13 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)12 URI (java.net.URI)12 Channel (io.netty.channel.Channel)10 ChannelFuture (io.netty.channel.ChannelFuture)9 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)9 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)9 Map (java.util.Map)9 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)7 Endpoint (com.nike.riposte.server.http.Endpoint)6 Bootstrap (io.netty.bootstrap.Bootstrap)6