Search in sources :

Example 6 with NettyHttpClientResponse

use of com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientResponse in project riposte by Nike-Inc.

the class VerifyDecoderFailedResultIsHandledTest method endpoints_should_handle_decode_exception_for_invalid_http_request.

@DataProvider(value = { "STANDARD_ENDPOINT", "PROXY_ENDPOINT" })
@Test
public void endpoints_should_handle_decode_exception_for_invalid_http_request(EndpointTypeScenario scenario) throws Exception {
    // given
    int payloadSize = CUSTOM_REQUEST_DECODER_CONFIG.maxInitialLineLength() + 1;
    String payload = generatePayload(payloadSize);
    // leave off content-length and transfer-encoding headers to trigger DecoderFailedResult
    NettyHttpClientRequestBuilder request = request().withMethod(HttpMethod.POST).withUri(scenario.matchingPathBase).withPaylod(payload);
    // when
    NettyHttpClientResponse serverResponse = request.execute(scenario.serverPort, incompleteCallTimeoutMillis);
    // then
    assertTooLongFrameErrorResponse(serverResponse, EXPECTED_TOO_LONG_FRAME_LINE_API_ERROR);
    // The EXPECTED_TOO_LONG_FRAME_LINE_API_ERROR check above should have verified 400 status code, but do a
    // sanity check here just for test readability.
    assertThat(serverResponse.statusCode).isEqualTo(400);
}
Also used : NettyHttpClientResponse(com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientResponse) ProxyRouterEndpoint(com.nike.riposte.server.http.ProxyRouterEndpoint) StandardEndpoint(com.nike.riposte.server.http.StandardEndpoint) Endpoint(com.nike.riposte.server.http.Endpoint) NettyHttpClientRequestBuilder(com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientRequestBuilder) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 7 with NettyHttpClientResponse

use of com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientResponse in project riposte by Nike-Inc.

the class VerifyDecoderFailedResultIsHandledTest method endpoints_should_throw_decode_exception_for_multiple_headers_that_are_too_long_when_summed.

@DataProvider(value = { "STANDARD_ENDPOINT", "PROXY_ENDPOINT" })
@Test
public void endpoints_should_throw_decode_exception_for_multiple_headers_that_are_too_long_when_summed(EndpointTypeScenario scenario) throws Exception {
    // given
    Pair<String, Object> halfMaxLengthHeader = generateHeaderForHeaderLineLength("foo", CUSTOM_REQUEST_DECODER_CONFIG.maxHeaderSize() / 2);
    Pair<String, Object> halfMaxLengthHeaderPlusOne = generateHeaderForHeaderLineLength("bar", // trailing \r, but since we're doing two headers we don't want to consider the \r twice.
    (CUSTOM_REQUEST_DECODER_CONFIG.maxHeaderSize() / 2) + 2);
    NettyHttpClientRequestBuilder request = request().withMethod(HttpMethod.GET).withUri(scenario.matchingPathBase).withHeaders(halfMaxLengthHeader, halfMaxLengthHeaderPlusOne);
    // when
    NettyHttpClientResponse serverResponse = request.execute(scenario.serverPort, incompleteCallTimeoutMillis);
    // then
    assertTooLongFrameErrorResponse(serverResponse, EXPECTED_TOO_LONG_FRAME_HEADER_API_ERROR);
    // The EXPECTED_TOO_LONG_FRAME_HEADER_API_ERROR check above should have verified 431 status code, but do a
    // sanity check here just for test readability.
    assertThat(serverResponse.statusCode).isEqualTo(431);
}
Also used : NettyHttpClientResponse(com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientResponse) NettyHttpClientRequestBuilder(com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientRequestBuilder) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 8 with NettyHttpClientResponse

use of com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientResponse in project riposte by Nike-Inc.

the class VerifyCornerCasesComponentTest method invalid_http_call_that_causes_Netty_DecoderFailure_should_result_in_expected_400_error.

@Test
public void invalid_http_call_that_causes_Netty_DecoderFailure_should_result_in_expected_400_error() throws Exception {
    // given
    // Normal request, but fiddle with the first chunk as it's going out to remove the HTTP version and make it an
    // invalid HTTP call. This will cause Netty to mark the HttpRequest with a DecoderFailure.
    NettyHttpClientRequestBuilder request = request().withMethod(HttpMethod.GET).withUri(BasicEndpoint.MATCHING_PATH).withPipelineAdjuster(p -> p.addFirst(new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            String msgAsString = ((ByteBuf) msg).toString(CharsetUtil.UTF_8);
            if (msgAsString.contains("HTTP/1.1")) {
                msg = Unpooled.copiedBuffer(msgAsString.replace("HTTP/1.1", ""), CharsetUtil.UTF_8);
            }
            super.write(ctx, msg, promise);
        }
    }));
    // when
    NettyHttpClientResponse response = request.execute(downstreamServerConfig.endpointsPort(), 3000);
    // then
    verifyErrorReceived(response.payload, response.statusCode, new ApiErrorWithMetadata(SampleCoreApiError.MALFORMED_REQUEST, Pair.of("cause", "Invalid HTTP request")));
}
Also used : ApiErrorWithMetadata(com.nike.backstopper.apierror.ApiErrorWithMetadata) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) NettyHttpClientResponse(com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientResponse) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) NettyHttpClientRequestBuilder(com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientRequestBuilder) Test(org.junit.Test)

Example 9 with NettyHttpClientResponse

use of com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientResponse in project riposte by Nike-Inc.

the class VerifyProxyRequestsDoNotAlterRequestToDownstreamServiceTest method proxy_endpoints_should_honor_non_chunked_transfer_encoding.

@Test
public void proxy_endpoints_should_honor_non_chunked_transfer_encoding() throws Exception {
    // given
    int payloadSize = 10000;
    String payload = generatePayload(payloadSize);
    NettyHttpClientRequestBuilder request = request().withMethod(HttpMethod.POST).withUri(RouterEndpoint.MATCHING_PATH).withPaylod(payload).withHeader(HttpHeaders.Names.CONTENT_LENGTH, payloadSize).withHeader(HttpHeaders.Names.HOST, "localhost");
    // when
    NettyHttpClientResponse serverResponse = request.execute(proxyServerConfig.endpointsPort(), incompleteCallTimeoutMillis);
    // then
    assertThat(serverResponse.payload).isEqualTo(DownstreamEndpoint.RESPONSE_PAYLOAD);
    assertThat(serverResponse.statusCode).isEqualTo(HttpResponseStatus.OK.code());
    assertProxyAndDownstreamServiceHeadersAndTracingHeadersAdded();
    String proxyBody = extractBodyFromRawRequest(proxyServerRequest.toString());
    String downstreamBody = extractBodyFromRawRequest(downstreamServerRequest.toString());
    // assert request was NOT sent in chunks
    // https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1
    assertThat(proxyBody).doesNotContain("\r\n");
    assertThat(downstreamBody).doesNotContain("\r\n");
    // assert bodies are equal
    assertThat(proxyBody).isEqualTo(downstreamBody);
    // assert input payload matches proxy and downstream payloads
    assertThat(proxyBody).isEqualTo(payload);
    assertThat(downstreamBody).isEqualTo(payload);
}
Also used : NettyHttpClientResponse(com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientResponse) ProxyRouterEndpoint(com.nike.riposte.server.http.ProxyRouterEndpoint) StandardEndpoint(com.nike.riposte.server.http.StandardEndpoint) Endpoint(com.nike.riposte.server.http.Endpoint) NettyHttpClientRequestBuilder(com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientRequestBuilder) Test(org.junit.Test)

Example 10 with NettyHttpClientResponse

use of com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientResponse in project riposte by Nike-Inc.

the class VerifySmartHttpContentCompressorComponentTest method response_should_not_be_compressed_when_ResponseInfo_disables_compression.

@DataProvider(value = { "GZIP", "DEFLATE", "IDENTITY" }, splitBy = "\\|")
@Test
public void response_should_not_be_compressed_when_ResponseInfo_disables_compression(CompressionType compressionType) throws Exception {
    // given
    NettyHttpClientRequestBuilder request = request().withMethod(HttpMethod.GET).withUri(BasicEndpoint.MATCHING_PATH).withHeader(ACCEPT_ENCODING, compressionType.contentEncodingHeaderValue).withHeader(BasicEndpoint.DESIRED_UNCOMPRESSED_PAYLOAD_SIZE_HEADER_KEY, 1000).withHeader(BasicEndpoint.DISABLE_COMPRESSION_HEADER_KEY, "true");
    // when
    NettyHttpClientResponse serverResponse = request.execute(serverConfig.endpointsPort(), incompleteCallTimeoutMillis);
    // then
    assertThat(serverResponse.statusCode).isEqualTo(HttpResponseStatus.OK.code());
    assertThat(serverResponse.headers.get(CONTENT_ENCODING)).isNull();
    assertThat(serverResponse.payload).hasSize(1000);
    assertThat(serverResponse.payload).startsWith(BasicEndpoint.RESPONSE_PAYLOAD_PREFIX);
}
Also used : NettyHttpClientResponse(com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientResponse) NettyHttpClientRequestBuilder(com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientRequestBuilder) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Aggregations

NettyHttpClientRequestBuilder (com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientRequestBuilder)22 NettyHttpClientResponse (com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientResponse)22 Test (org.junit.Test)22 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)10 Endpoint (com.nike.riposte.server.http.Endpoint)5 ProxyRouterEndpoint (com.nike.riposte.server.http.ProxyRouterEndpoint)5 StandardEndpoint (com.nike.riposte.server.http.StandardEndpoint)5 ApiErrorWithMetadata (com.nike.backstopper.apierror.ApiErrorWithMetadata)3 ByteBuf (io.netty.buffer.ByteBuf)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 ChannelOutboundHandlerAdapter (io.netty.channel.ChannelOutboundHandlerAdapter)2 ChannelPromise (io.netty.channel.ChannelPromise)2 SimpleProxyRouterEndpoint (com.nike.riposte.server.http.impl.SimpleProxyRouterEndpoint)1 ComponentTestUtils.createNettyHttpClientBootstrap (com.nike.riposte.server.testutils.ComponentTestUtils.createNettyHttpClientBootstrap)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 Channel (io.netty.channel.Channel)1 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)1