public void sizeLimitationTest() throws Exception {
    int blobPartSize = 1024;
    byte[] bytes = TestUtils.getRandomBytes(blobPartSize);
    int encodedSize = getEncodedSize(bytes);
    long[] maxSizesAllowed = {encodedSize + 1, encodedSize, encodedSize - 1, 0};
    for (long maxSizeAllowed : maxSizesAllowed) {
      InMemoryFile[] files = {new InMemoryFile(RestUtils.MultipartPost.BLOB_PART, ByteBuffer.wrap(bytes))};
      HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
      HttpPostRequestEncoder encoder = createEncoder(httpRequest, files);
      NettyMultipartRequest request =
          new NettyMultipartRequest(encoder.finalizeRequest(), new MockChannel(), NETTY_METRICS, Collections.emptySet(),
              maxSizeAllowed);
      assertTrue("Request channel is not open", request.isOpen());
      long currentSizeAdded = 0;
      boolean failedToAdd = false;
      while (!encoder.isEndOfInput()) {
        HttpContent httpContent = encoder.readChunk(PooledByteBufAllocator.DEFAULT);
        int readableBytes = httpContent.content().readableBytes();
        if (currentSizeAdded + readableBytes <= maxSizeAllowed) {
          request.addContent(httpContent);
        } else {
          assertTrue("Max size [" + maxSizeAllowed + "] must be lesser than content size: " + encodedSize,
              maxSizeAllowed < encodedSize);
          try {
            request.addContent(httpContent);
            fail("Should have failed to add content of size [" + encodedSize
                + "] because it is over the max size allowed: " + maxSizeAllowed);
          } catch (RestServiceException e) {
            failedToAdd = true;
            assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.RequestTooLarge, e.getErrorCode());
            break;
          }
        }
        currentSizeAdded += readableBytes;
      }
      assertEquals(
          "Success state not as expected. maxSizeAllowed=[" + maxSizeAllowed + "], encodedSize expected=[" + encodedSize
              + "], actual size added=[" + currentSizeAdded + "]", maxSizeAllowed < encodedSize, failedToAdd);
    }
  }
Tests to make sure the max allowed size for multipart requests is enforced.
@throws Exception
ambry-rest/src/test/java/com/github/ambry/rest/NettyMultipartRequestTest.java