use of io.netty.handler.codec.http.HttpObject in project riposte by Nike-Inc.
the class VerifyTimeoutsAndProxyConnectionPoolingWorksComponentTest method verify_incomplete_call_is_timed_out.
@Test
public void verify_incomplete_call_is_timed_out() throws InterruptedException, TimeoutException, ExecutionException, IOException {
Bootstrap bootstrap = new Bootstrap();
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
CompletableFuture<Pair<String, String>> responseFromServer = new CompletableFuture<>();
// Create a raw netty HTTP client so we can fiddle with headers and intentionally create a bad request
// that should trigger the bad call timeout.
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new HttpClientCodec());
p.addLast(new HttpObjectAggregator(Integer.MAX_VALUE));
p.addLast(new SimpleChannelInboundHandler<HttpObject>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
if (msg instanceof FullHttpResponse) {
// Store the server response for asserting on later.
FullHttpResponse responseMsg = (FullHttpResponse) msg;
responseFromServer.complete(Pair.of(responseMsg.content().toString(CharsetUtil.UTF_8), responseMsg.headers().get(HttpHeaders.Names.CONNECTION)));
} else {
// Should never happen.
throw new RuntimeException("Received unexpected message type: " + msg.getClass());
}
}
});
}
});
// Connect to the server.
Channel ch = bootstrap.connect("localhost", downstreamServerConfig.endpointsPort()).sync().channel();
// Create a bad HTTP request. This one will be bad because it has a non-zero content-length header,
// but we're sending no payload. The server should (correctly) sit and wait for payload bytes to
// arrive until it hits the timeout, at which point it should return the correct error response.
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, LongDelayTestEndpoint.MATCHING_PATH);
request.headers().set(HttpHeaders.Names.HOST, "localhost");
request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, "100");
long beforeCallTimeNanos = System.nanoTime();
// Send the bad request.
ch.writeAndFlush(request);
// Wait for the response to be received and the connection to be closed.
try {
ch.closeFuture().get(incompleteCallTimeoutMillis * 10, TimeUnit.MILLISECONDS);
responseFromServer.get(incompleteCallTimeoutMillis * 10, TimeUnit.MILLISECONDS);
} catch (TimeoutException ex) {
fail("The call took much longer than expected without receiving a response. " + "Cancelling this test - it's not working properly", ex);
}
// If we reach here then the call should be complete.
long totalCallTimeMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - beforeCallTimeNanos);
// Verify that we got back the correct error response.
// It should be a MALFORMED_REQUEST with extra metadata explaining that the call was bad.
Pair<String, String> responseInfo = responseFromServer.get();
DefaultErrorContractDTO errorContract = objectMapper.readValue(responseInfo.getLeft(), DefaultErrorContractDTO.class);
assertThat(errorContract).isNotNull();
assertThat(errorContract.errors.size()).isEqualTo(1);
DefaultErrorDTO error = errorContract.errors.get(0);
ApiError expectedApiError = SampleCoreApiError.MALFORMED_REQUEST;
Map<String, Object> expectedMetadata = MapBuilder.builder("cause", (Object) "Unfinished/invalid HTTP request").build();
assertThat(error.code).isEqualTo(expectedApiError.getErrorCode());
assertThat(error.message).isEqualTo(expectedApiError.getMessage());
assertThat(error.metadata).isEqualTo(expectedMetadata);
// The server should have closed the connection even though we asked for keep-alive.
assertThat(responseInfo.getRight()).isEqualTo(HttpHeaders.Values.CLOSE);
// Total call time should be pretty close to incompleteCallTimeoutMillis give or take a few
// milliseconds, but due to the inability to account for slow machines running the unit tests,
// a server that isn't warmed up, etc, we can't put a ceiling on the wiggle room we'd need, so
// we'll just verify it took at least the minimum necessary amount of time.
assertThat(totalCallTimeMillis).isGreaterThanOrEqualTo(incompleteCallTimeoutMillis);
} finally {
eventLoopGroup.shutdownGracefully();
}
}
use of io.netty.handler.codec.http.HttpObject in project riposte by Nike-Inc.
the class RequestInfoSetterHandlerTest method doChannelRead_checks_for_fully_send_responses_but_does_nothing_else_if_msg_is_not_HttpRequest_or_HttpContent.
@Test
public void doChannelRead_checks_for_fully_send_responses_but_does_nothing_else_if_msg_is_not_HttpRequest_or_HttpContent() {
// given
HttpObject msgMock = mock(HttpObject.class);
// when
PipelineContinuationBehavior result = handler.doChannelRead(ctxMock, msgMock);
// then
verify(ctxMock, times(2)).channel();
verifyNoMoreInteractions(ctxMock);
verify(stateMock).isResponseSendingLastChunkSent();
verifyNoMoreInteractions(stateMock);
verifyNoMoreInteractions(msgMock);
assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE);
}
use of io.netty.handler.codec.http.HttpObject in project ambry by linkedin.
the class FrontendIntegrationTest method assertNoContent.
// helpers
// general
/**
* Discards all the content in {@code contents} and checks none of the chunks have actual content
* @param contents the content to discard.
* @param expectedDiscardCount the number of {@link HttpObject}s that are expected to discarded.
*/
void assertNoContent(Queue<HttpObject> contents, int expectedDiscardCount) {
assertEquals("Objects that will be discarded differ from expected", expectedDiscardCount, contents.size());
boolean endMarkerFound = false;
for (HttpObject object : contents) {
assertFalse("There should have been no more data after the end marker was found", endMarkerFound);
HttpContent content = (HttpContent) object;
assertEquals("No content expected ", 0, content.content().readableBytes());
endMarkerFound = object instanceof LastHttpContent;
ReferenceCountUtil.release(object);
}
assertTrue("There should have been an end marker", endMarkerFound);
}
use of io.netty.handler.codec.http.HttpObject in project ambry by linkedin.
the class FrontendIntegrationTestBase method assertNoContent.
// BeforeClass helpers
/**
* Discards all the content in {@code contents} and checks none of the chunks have actual content
* @param contents the content to discard.
* @param expectedDiscardCount the number of {@link HttpObject}s that are expected to discarded.
*/
void assertNoContent(Queue<HttpObject> contents, int expectedDiscardCount) {
assertEquals("Objects that will be discarded differ from expected", expectedDiscardCount, contents.size());
boolean endMarkerFound = false;
for (HttpObject object : contents) {
assertFalse("There should have been no more data after the end marker was found", endMarkerFound);
HttpContent content = (HttpContent) object;
assertEquals("No content expected ", 0, content.content().readableBytes());
endMarkerFound = object instanceof LastHttpContent;
ReferenceCountUtil.release(object);
}
assertTrue("There should have been an end marker", endMarkerFound);
}
use of io.netty.handler.codec.http.HttpObject in project ambry by linkedin.
the class FrontendIntegrationTestBase method getContent.
/**
* Combines all the parts in {@code contents} into one {@link ByteBuffer}.
* @param contents the content of the response.
* @param expectedContentLength the length of the contents in bytes.
* @return a {@link ByteBuffer} that contains all the data in {@code contents}.
*/
ByteBuffer getContent(Queue<HttpObject> contents, long expectedContentLength) {
ByteBuffer buffer = ByteBuffer.allocate((int) expectedContentLength);
boolean endMarkerFound = false;
for (HttpObject object : contents) {
assertFalse("There should have been no more data after the end marker was found", endMarkerFound);
HttpContent content = (HttpContent) object;
buffer.put(content.content().nioBuffer());
endMarkerFound = object instanceof LastHttpContent;
content.release();
}
assertEquals("Content length did not match expected", expectedContentLength, buffer.position());
assertTrue("End marker was not found", endMarkerFound);
buffer.flip();
return buffer;
}
Aggregations