@Test
  public void responsesWithContentLengthTest() throws Exception {
    EmbeddedChannel channel = createEmbeddedChannel();
    MockNettyMessageProcessor processor = channel.pipeline().get(MockNettyMessageProcessor.class);
    final int ITERATIONS = 10;
    for (int i = 0; i < ITERATIONS; i++) {
      boolean isKeepAlive = i != (ITERATIONS - 1);
      HttpHeaders httpHeaders = new DefaultHttpHeaders();
      httpHeaders.set(MockNettyMessageProcessor.CHUNK_COUNT_HEADER_NAME, i);
      HttpRequest httpRequest =
          RestTestUtils.createRequest(HttpMethod.POST, TestingUri.ResponseWithContentLength.toString(), httpHeaders);
      HttpUtil.setKeepAlive(httpRequest, isKeepAlive);
      channel.writeInbound(httpRequest);
      verifyCallbacks(processor);

      // first outbound has to be response.
      HttpResponse response = channel.readOutbound();
      assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
      long contentLength = HttpUtil.getContentLength(response, NettyRequest.UNKNOWN_CONTENT_LENGTH);
      assertEquals("Unexpected Content-Length", MockNettyMessageProcessor.CHUNK.length * i, contentLength);
      if (contentLength == 0) {
        // special case. Since Content-Length is set, the response should be an instance of FullHttpResponse.
        assertTrue("Response not instance of FullHttpResponse", response instanceof FullHttpResponse);
      } else {
        HttpContent httpContent = null;
        for (int j = 0; j < i; j++) {
          httpContent = channel.readOutbound();
          byte[] returnedContent = new byte[httpContent.content().readableBytes()];
          httpContent.content().readBytes(returnedContent);
          httpContent.release();
          assertArrayEquals("Content does not match with expected content", MockNettyMessageProcessor.CHUNK,
              returnedContent);
        }
        // When we know the content-length, the last httpContent would be an instance of LastHttpContent and there is no
        // empty last http content following it.
        // the last HttpContent should also be an instance of LastHttpContent
        assertTrue("The last part of the content is not LastHttpContent", httpContent instanceof LastHttpContent);
      }
      assertEquals("Unexpected channel state on the server", isKeepAlive, channel.isActive());
    }
  }
Tests the common workflow of the {@link NettyResponseChannel} i.e., add some content to response body via
{@link NettyResponseChannel#write(ByteBuffer, Callback)} and then complete the response.
<p/>
These responses have the header Content-Length set.
@throws Exception
ambry-rest/src/test/java/com/github/ambry/rest/NettyResponseChannelTest.java