@Test
  public void responsesWithTransferEncodingChunkedTest() throws Exception {
    String content = "@@randomContent@@@";
    String lastContent = "@@randomLastContent@@@";
    EmbeddedChannel channel = createEmbeddedChannel();
    MockNettyMessageProcessor processor = channel.pipeline().get(MockNettyMessageProcessor.class);
    AtomicLong contentIdGenerator = new AtomicLong(0);

    final int ITERATIONS = 10;
    for (int i = 0; i < ITERATIONS; i++) {
      boolean isKeepAlive = i != (ITERATIONS - 1);
      HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", null);
      HttpUtil.setKeepAlive(httpRequest, isKeepAlive);
      channel.writeInbound(httpRequest);
      ArrayList<String> contents = new ArrayList<>();
      for (int j = 0; j <= i; j++) {
        String contentToSend = content + contentIdGenerator.getAndIncrement();
        channel.writeInbound(createContent(contentToSend, false));
        contents.add(contentToSend);
      }
      channel.writeInbound(createContent(lastContent, true));
      verifyCallbacks(processor);
      // first outbound has to be response.
      HttpResponse response = channel.readOutbound();
      assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
      assertTrue("Response must say 'Transfer-Encoding : chunked'", HttpUtil.isTransferEncodingChunked(response));
      // content echoed back.
      for (String srcOfTruth : contents) {
        HttpContent responseContent = channel.readOutbound();
        String returnedContent = RestTestUtils.getContentString(responseContent);
        responseContent.release();
        assertEquals("Content does not match with expected content", srcOfTruth, returnedContent);
      }
      HttpContent responseContent = channel.readOutbound();
      // last content echoed back.
      String returnedContent = RestTestUtils.getContentString(responseContent);
      responseContent.release();
      assertEquals("Content does not match with expected content", lastContent, returnedContent);
      // When the Transfer-Encoding is Chunked, response channel would send the last http content in the request as
      // regular http content and then send an empty last http content after that.
      assertTrue("Did not receive end marker", channel.readOutbound() 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(ByteBuf, Callback)} and then complete the response.
<p/>
These responses have the header Transfer-Encoding set to chunked.
@throws Exception
ambry-rest/src/test/java/com/github/ambry/rest/NettyResponseChannelTest.java