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);
}
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();
}
}
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);
}
}
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);
}
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);
}
Aggregations