use of io.netty.handler.codec.http.DefaultLastHttpContent in project ambry by linkedin.
the class NettyMessageProcessorTest method doRequestHandlerExceptionTest.
// requestHandlerExceptionTest() helpers.
/**
* Does a test where the request handler inside {@link NettyMessageProcessor} fails. Checks for the right error code
* in the response.
* @param httpMethod the {@link HttpMethod} to use for the request.
* @param expectedStatus the excepted {@link HttpResponseStatus} in the response.
*/
private void doRequestHandlerExceptionTest(HttpMethod httpMethod, HttpResponseStatus expectedStatus) {
EmbeddedChannel channel = createChannel();
channel.writeInbound(RestTestUtils.createRequest(httpMethod, "/", null));
channel.writeInbound(new DefaultLastHttpContent());
// first outbound has to be response.
HttpResponse response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", expectedStatus, response.status());
}
use of io.netty.handler.codec.http.DefaultLastHttpContent in project ambry by linkedin.
the class NettyMessageProcessorTest method sendRequestCheckResponse.
/**
* Sends the provided {@code httpRequest} and verifies that the response is an echo of the {@code restMethod}.
* @param channel the {@link EmbeddedChannel} to send the request over.
* @param httpMethod the {@link HttpMethod} for the request.
* @param restMethod the equivalent {@link RestMethod} for {@code httpMethod}. Used to check for correctness of
* response.
* @param isKeepAlive if the request needs to be keep-alive.
* @throws IOException
*/
private void sendRequestCheckResponse(EmbeddedChannel channel, HttpMethod httpMethod, RestMethod restMethod, boolean isKeepAlive) throws IOException {
long requestId = REQUEST_ID_GENERATOR.getAndIncrement();
String uri = MockRestRequestService.ECHO_REST_METHOD + requestId;
HttpRequest httpRequest = RestTestUtils.createRequest(httpMethod, uri, null);
HttpUtil.setKeepAlive(httpRequest, isKeepAlive);
channel.writeInbound(httpRequest);
channel.writeInbound(new DefaultLastHttpContent());
HttpResponse response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
// MockRestRequestService echoes the RestMethod + request id.
String expectedResponse = restMethod.toString() + requestId;
assertEquals("Unexpected content", expectedResponse, RestTestUtils.getContentString((HttpContent) channel.readOutbound()));
assertTrue("End marker was expected", channel.readOutbound() instanceof LastHttpContent);
}
use of io.netty.handler.codec.http.DefaultLastHttpContent in project ambry by linkedin.
the class MockChannelHandlerContext method createContent.
// helpers
// general
/**
* Creates {@link HttpContent} wrapping the {@code content}.
* @param content the content to wrap.
* @param isLast {@code true} if this is the last piece of content. {@code false} otherwise.
* @return a {@link HttpContent} wrapping the {@code content}.
*/
private HttpContent createContent(String content, boolean isLast) {
ByteBuf buf = PooledByteBufAllocator.DEFAULT.heapBuffer(content.getBytes().length);
buf.writeBytes(content.getBytes());
if (isLast) {
return new DefaultLastHttpContent(buf);
} else {
return new DefaultHttpContent(buf);
}
}
use of io.netty.handler.codec.http.DefaultLastHttpContent in project ambry by linkedin.
the class CopyForcingByteBuf method splitContent.
/**
* Splits the given {@code contentBytes} into {@code numChunks} chunks and stores them in {@code httpContents}.
* @param contentBytes the content that needs to be split.
* @param numChunks the number of chunks to split {@code contentBytes} into.
* @param httpContents the {@link List<HttpContent>} that will contain all the content in parts.
* @param useCopyForcingByteBuf if {@code true}, uses {@link CopyForcingByteBuf} instead of the default
* {@link ByteBuf}.
*/
private void splitContent(byte[] contentBytes, int numChunks, List<HttpContent> httpContents, boolean useCopyForcingByteBuf) {
int individualPartSize = contentBytes.length / numChunks;
ByteBuf content;
for (int addedContentCount = 0; addedContentCount < numChunks - 1; addedContentCount++) {
if (useCopyForcingByteBuf) {
content = CopyForcingByteBuf.wrappedBuffer(contentBytes, addedContentCount * individualPartSize, individualPartSize);
} else {
content = Unpooled.wrappedBuffer(contentBytes, addedContentCount * individualPartSize, individualPartSize);
}
httpContents.add(new DefaultHttpContent(content));
}
if (useCopyForcingByteBuf) {
content = CopyForcingByteBuf.wrappedBuffer(contentBytes, (numChunks - 1) * individualPartSize, individualPartSize);
} else {
content = Unpooled.wrappedBuffer(contentBytes, (numChunks - 1) * individualPartSize, individualPartSize);
}
httpContents.add(new DefaultLastHttpContent(content));
}
use of io.netty.handler.codec.http.DefaultLastHttpContent in project riposte by Nike-Inc.
the class RequestInfoImplTest method addContentChunk_and_getRawConent_and_getRawContentBytes_work_as_expected_for_last_chunk.
@Test
public void addContentChunk_and_getRawConent_and_getRawContentBytes_work_as_expected_for_last_chunk() throws IOException {
// given
RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
requestInfo.isCompleteRequestWithAllChunks = false;
String chunk1String = UUID.randomUUID().toString();
String lastChunkString = UUID.randomUUID().toString();
byte[] chunk1Bytes = chunk1String.getBytes();
byte[] lastChunkBytes = lastChunkString.getBytes();
HttpContent chunk1 = new DefaultHttpContent(Unpooled.copiedBuffer(chunk1Bytes));
HttpContent lastChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(lastChunkBytes));
assertThat(chunk1.refCnt(), is(1));
assertThat(lastChunk.refCnt(), is(1));
assertThat(requestInfo.getRawContentBytes(), nullValue());
assertThat(requestInfo.getRawContent(), nullValue());
// when
requestInfo.addContentChunk(chunk1);
requestInfo.addContentChunk(lastChunk);
// then
assertThat(chunk1.refCnt(), is(2));
assertThat(lastChunk.refCnt(), is(2));
assertThat(requestInfo.contentChunks.size(), is(2));
assertThat(requestInfo.isCompleteRequestWithAllChunks(), is(true));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(chunk1Bytes);
baos.write(lastChunkBytes);
assertThat(requestInfo.getRawContentBytes(), is(baos.toByteArray()));
String rawContentString = requestInfo.getRawContent();
assertThat(requestInfo.getRawContent(), is(chunk1String + lastChunkString));
// Verify that the raw content string is cached the first time it's loaded and reused for subsequent calls
assertThat(requestInfo.getRawContent() == rawContentString, is(true));
assertThat(chunk1.refCnt(), is(1));
assertThat(lastChunk.refCnt(), is(1));
}
Aggregations