private void doHeaderAndContentSizeMismatchTest(HttpHeaders httpHeaders, List<HttpContent> httpContents)
      throws Exception {
    Channel channel = new MockChannel();
    NettyRequest nettyRequest = createNettyRequest(HttpMethod.POST, "/", httpHeaders, channel);
    AsyncWritableChannel writeChannel = new ByteBufferAsyncWritableChannel();
    ReadIntoCallback callback = new ReadIntoCallback();
    Future<Long> future = nettyRequest.readInto(writeChannel, callback);

    int bytesAdded = 0;
    HttpContent httpContentToAdd = null;
    for (HttpContent httpContent : httpContents) {
      httpContentToAdd = httpContent;
      int contentBytes = httpContentToAdd.content().readableBytes();
      if (!(httpContentToAdd instanceof LastHttpContent) && (bytesAdded + contentBytes <= nettyRequest.getSize())) {
        nettyRequest.addContent(httpContentToAdd);
        assertEquals("Reference count is not as expected", 2, httpContentToAdd.refCnt());
        bytesAdded += contentBytes;
      } else {
        break;
      }
    }

    // the addition of the next content should throw an exception.
    try {
      nettyRequest.addContent(httpContentToAdd);
      fail("Adding content should have failed because there was a mismatch in size");
    } catch (RestServiceException e) {
      assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.BadRequest, e.getErrorCode());
    }
    closeRequestAndValidate(nettyRequest, channel);
    writeChannel.close();
    verifyRefCnts(httpContents);
    callback.awaitCallback();
    assertNotNull("There should be a RestServiceException in the callback", callback.exception);
    assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.BadRequest,
        ((RestServiceException) callback.exception).getErrorCode());
    try {
      future.get();
      fail("Should have thrown exception because the future is expected to have been given one");
    } catch (ExecutionException e) {
      RestServiceException restServiceException = (RestServiceException) Utils.getRootCause(e);
      assertNotNull("There should be a RestServiceException in the future", restServiceException);
      assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.BadRequest,
          restServiceException.getErrorCode());
    }
  }
Tests reaction of NettyRequest when content size is different from the size specified in the headers.
@param httpHeaders {@link HttpHeaders} that need to be a part of the request.
@param httpContents the {@link List<HttpContent>} that needs to be added to {@code nettyRequest}.
@throws Exception
ambry-rest/src/test/java/com/github/ambry/rest/NettyRequestTest.java