Search in sources :

Example 11 with NettyHttpClientRequestBuilder

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

the class VerifySmartHttpContentCompressorComponentTest method response_should_be_compressed_based_on_payload_size_and_accept_encoding_header.

@DataProvider(value = { "GZIP       |   499 |   false", "DEFLATE    |   499 |   false", "IDENTITY   |   499 |   false", "GZIP       |   500 |   false", "DEFLATE    |   500 |   false", "IDENTITY   |   500 |   false", "GZIP       |   501 |   true", "DEFLATE    |   501 |   true", "IDENTITY   |   501 |   false" }, splitBy = "\\|")
@Test
public void response_should_be_compressed_based_on_payload_size_and_accept_encoding_header(CompressionType compressionType, int desiredUncompressedPayloadSize, boolean expectCompressed) 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, desiredUncompressedPayloadSize);
    // when
    NettyHttpClientResponse serverResponse = request.execute(serverConfig.endpointsPort(), incompleteCallTimeoutMillis);
    // then
    assertThat(serverResponse.statusCode).isEqualTo(HttpResponseStatus.OK.code());
    String contentEncodingHeader = serverResponse.headers.get(CONTENT_ENCODING);
    String decompressedPayload;
    if (expectCompressed) {
        assertThat(contentEncodingHeader).isEqualTo(compressionType.contentEncodingHeaderValue);
        decompressedPayload = compressionType.decompress(serverResponse.payloadBytes);
    } else {
        assertThat(contentEncodingHeader).isNull();
        decompressedPayload = serverResponse.payload;
    }
    assertThat(decompressedPayload).hasSize(desiredUncompressedPayloadSize);
    assertThat(decompressedPayload).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)

Example 12 with NettyHttpClientRequestBuilder

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

the class VerifyProxyRequestCornerCasesComponentTest method proxy_router_endpoints_should_immediately_stop_downstream_call_on_unexpected_request_side_error.

/**
 * This test verifies the corner case where a proxy/router call fails on the request side due to an exception, and
 * then a new request is fired immediately on the same channel while the downstream call from the original
 * request is still active. When the exception occurred on the original request, it should shut down and disable
 * anything from the original downstream call from proceeding, otherwise the original downstream call's response
 * data might get fired down the pipeline for the second response (most likely triggering a HTTP state error in
 * Netty's HttpResponseEncoder and causing the channel to be closed, breaking the second response).
 */
@Test
public void proxy_router_endpoints_should_immediately_stop_downstream_call_on_unexpected_request_side_error() throws IOException, InterruptedException, TimeoutException, ExecutionException {
    Bootstrap bootstrap = createNettyHttpClientBootstrap();
    // We reuse the channel to guarantee the requests travel over the same keep-alive connection.
    Channel proxyServerChannel = connectNettyHttpClientToLocalServer(bootstrap, proxyServerConfig.endpointsPort());
    long maxRequestTimeout = LongerDelayEndpoint.DELAY_MILLIS + 1000;
    try {
        // Execute a request that routes to the normal-delay endpoint that triggers the request-side-short-circuit-error.
        {
            NettyHttpClientRequestBuilder request = request().withMethod(HttpMethod.POST).withUri(RouterEndpointForwardingToDelayEndpoint.MATCHING_PATH).withHeader(INTENTIONAL_EXPLOSION_AFTER_LAST_CHUNK_HEADER_KEY, "true");
            NettyHttpClientResponse response = request.execute(proxyServerChannel, maxRequestTimeout);
            verifyErrorReceived(response.payload, response.statusCode, INTENTIONAL_EXPLOSION_AFTER_LAST_CHUNK_API_ERROR);
        }
        // Then immediately make another call that routes to the longer-delay endpoint. The response should be
        // from the longer-delay endpoint, *not* the normal-delay endpoint.
        {
            NettyHttpClientRequestBuilder request = request().withMethod(HttpMethod.POST).withUri(RouterEndpointForwardingToLongerDelayEndpoint.MATCHING_PATH);
            long beforeMillis = System.currentTimeMillis();
            NettyHttpClientResponse response = request.execute(proxyServerChannel, maxRequestTimeout);
            long afterMillis = System.currentTimeMillis();
            assertThat(response.statusCode).isEqualTo(200);
            assertThat(response.payload).isEqualTo(LongerDelayEndpoint.RESPONSE_PAYLOAD);
            assertThat(afterMillis - beforeMillis).isGreaterThanOrEqualTo(LongerDelayEndpoint.DELAY_MILLIS);
        }
    } finally {
        proxyServerChannel.close();
        bootstrap.config().group().shutdownGracefully();
    }
}
Also used : Channel(io.netty.channel.Channel) ComponentTestUtils.createNettyHttpClientBootstrap(com.nike.riposte.server.testutils.ComponentTestUtils.createNettyHttpClientBootstrap) Bootstrap(io.netty.bootstrap.Bootstrap) NettyHttpClientResponse(com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientResponse) NettyHttpClientRequestBuilder(com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientRequestBuilder) Test(org.junit.Test)

Example 13 with NettyHttpClientRequestBuilder

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

the class VerifyProxyRequestCornerCasesComponentTest method proxy_endpoints_should_successfully_return_short_circuited_downstream_response.

/**
 * This test verifies the corner case where the downstream system short circuits and returns a response before
 * it receives the full request (i.e. the request chunks are still streaming when the response is returned).
 * This should cause request chunks to fail to stream with an error, but the downstream response should still
 * be returned to the original caller successfully.
 */
@Test
public void proxy_endpoints_should_successfully_return_short_circuited_downstream_response() throws Exception {
    // Do this test a bunch of times to try and catch all the race condition possibilities.
    for (int i = 0; i < 20; i++) {
        // given
        int payloadSize = 1024 * 1000;
        String payload = generatePayload(payloadSize);
        NettyHttpClientRequestBuilder request = request().withMethod(HttpMethod.POST).withUri(RouterEndpointForwardingToShortCircuitError.MATCHING_PATH).withPaylod(payload).withHeader(HttpHeaders.Names.CONTENT_LENGTH, payloadSize);
        // when
        NettyHttpClientResponse serverResponse = request.execute(proxyServerConfig.endpointsPort(), 3000);
        // then
        verifyErrorReceived(serverResponse.payload, serverResponse.statusCode, FAIL_FAST_API_ERROR);
    }
}
Also used : NettyHttpClientResponse(com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientResponse) ProxyRouterEndpoint(com.nike.riposte.server.http.ProxyRouterEndpoint) Endpoint(com.nike.riposte.server.http.Endpoint) SimpleProxyRouterEndpoint(com.nike.riposte.server.http.impl.SimpleProxyRouterEndpoint) StandardEndpoint(com.nike.riposte.server.http.StandardEndpoint) NettyHttpClientRequestBuilder(com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientRequestBuilder) Test(org.junit.Test)

Example 14 with NettyHttpClientRequestBuilder

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

the class VerifyRequestSizeValidationComponentTest method should_return_expected_response_when_chunked_request_not_exceeding_endpoint_overridden_request_size.

@Test
public void should_return_expected_response_when_chunked_request_not_exceeding_endpoint_overridden_request_size() throws Exception {
    NettyHttpClientRequestBuilder request = request().withMethod(HttpMethod.POST).withUri(BasicEndpointWithRequestSizeValidationOverride.MATCHING_PATH).withPaylod(generatePayloadOfSizeInBytes(BasicEndpointWithRequestSizeValidationOverride.MAX_REQUEST_SIZE)).withHeader(HttpHeaders.Names.TRANSFER_ENCODING, CHUNKED);
    // when
    NettyHttpClientResponse serverResponse = request.execute(serverConfig.endpointsPort(), incompleteCallTimeoutMillis);
    // then
    assertThat(serverResponse.statusCode).isEqualTo(HttpResponseStatus.OK.code());
    assertThat(serverResponse.payload).isEqualTo(BasicEndpointWithRequestSizeValidationOverride.RESPONSE_PAYLOAD);
}
Also used : NettyHttpClientResponse(com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientResponse) NettyHttpClientRequestBuilder(com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientRequestBuilder) Test(org.junit.Test)

Example 15 with NettyHttpClientRequestBuilder

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

the class VerifyRequestSizeValidationComponentTest method should_return_bad_request_when_chunked_request_exceeds_global_configured_max_request_size.

@Test
public void should_return_bad_request_when_chunked_request_exceeds_global_configured_max_request_size() throws Exception {
    NettyHttpClientRequestBuilder request = request().withMethod(HttpMethod.POST).withUri(BasicEndpoint.MATCHING_PATH).withPaylod(generatePayloadOfSizeInBytes(GLOBAL_MAX_REQUEST_SIZE + 1)).withHeader(HttpHeaders.Names.TRANSFER_ENCODING, CHUNKED);
    // when
    NettyHttpClientResponse serverResponse = request.execute(serverConfig.endpointsPort(), incompleteCallTimeoutMillis);
    // then
    assertThat(serverResponse.statusCode).isEqualTo(HttpResponseStatus.BAD_REQUEST.code());
    assertBadRequestErrorMessageAndMetadata(serverResponse.payload);
}
Also used : NettyHttpClientResponse(com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientResponse) NettyHttpClientRequestBuilder(com.nike.riposte.server.testutils.ComponentTestUtils.NettyHttpClientRequestBuilder) 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