@Test
public void createsEntityUsingGivenSource() throws IOException {

    // given
    HCRequestFactory factory = createDefaultTestObject();
    String expectedUrl = UUID.randomUUID().toString();
    Request request = createDefaultMockRequest(expectedUrl, "POST");
    ByteBuf byteBuf = new CompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, false, 2);
    byte[] expectedBytes = UUID.randomUUID().toString().getBytes();
    byteBuf.writeBytes(expectedBytes);
    ItemSource<ByteBuf> itemSource = mock(ItemSource.class);
    when(itemSource.getSource()).thenReturn(byteBuf);
    when(request.serialize()).thenReturn(itemSource);
    // when
    HttpEntityEnclosingRequest result = (HttpEntityEnclosingRequest) factory.create(expectedUrl, request);
    // then
    assertEquals(expectedBytes.length, result.getEntity().getContentLength());
    ByteBuf source = (ByteBuf) request.serialize().getSource();
    source.readBytes(new byte[source.writerIndex()]);
    InputStream inputStream = result.getEntity().getContent();
    assertEquals(0, inputStream.available());
}