Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix stream suspending with FeedableBodyGenerator.
netty expects null as outgoing buffer to suspend the stream. see ChunkedWriteHandler#222 - Added unit test for feedable body generator - Fix netty 4 issue with feedable body generator - provide integration test
- Loading branch information
Showing
6 changed files
with
198 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
api/src/test/java/org/asynchttpclient/request/body/generator/FeedableBodyGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package org.asynchttpclient.request.body.generator; | ||
|
||
import org.asynchttpclient.request.body.Body; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import static org.testng.Assert.*; | ||
|
||
public class FeedableBodyGeneratorTest { | ||
|
||
private FeedableBodyGenerator feedableBodyGenerator; | ||
private TestFeedListener listener; | ||
|
||
@BeforeMethod | ||
public void setUp() throws Exception { | ||
feedableBodyGenerator = new FeedableBodyGenerator(); | ||
listener = new TestFeedListener(); | ||
feedableBodyGenerator.setListener(listener); | ||
} | ||
|
||
@Test(groups = "standalone") | ||
public void feedNotifiesListener() throws Exception { | ||
feedableBodyGenerator.feed(ByteBuffer.allocate(0), false); | ||
feedableBodyGenerator.feed(ByteBuffer.allocate(0), true); | ||
assertEquals(listener.getCalls(), 2); | ||
} | ||
|
||
@Test(groups = "standalone") | ||
public void readingBytesReturnsFedContentWithEmptyLastBuffer() throws Exception { | ||
byte[] content = "Test123".getBytes(StandardCharsets.US_ASCII); | ||
feedableBodyGenerator.feed(ByteBuffer.wrap(content), false); | ||
feedableBodyGenerator.feed(ByteBuffer.allocate(0), true); | ||
Body body = feedableBodyGenerator.createBody(); | ||
assertEquals(readFromBody(body), "7\r\nTest123\r\n".getBytes(StandardCharsets.US_ASCII)); | ||
assertEquals(readFromBody(body), "0\r\n\r\n".getBytes(StandardCharsets.US_ASCII)); | ||
assertEquals(body.read(ByteBuffer.allocate(1)), -1); | ||
|
||
} | ||
|
||
@Test(groups = "standalone") | ||
public void readingBytesReturnsFedContentWithFilledLastBuffer() throws Exception { | ||
byte[] content = "Test123".getBytes(StandardCharsets.US_ASCII); | ||
feedableBodyGenerator.feed(ByteBuffer.wrap(content), true); | ||
Body body = feedableBodyGenerator.createBody(); | ||
assertEquals(readFromBody(body), "7\r\nTest123\r\n".getBytes(StandardCharsets.US_ASCII)); | ||
assertEquals(readFromBody(body), "0\r\n\r\n".getBytes(StandardCharsets.US_ASCII)); | ||
assertEquals(body.read(ByteBuffer.allocate(1)), -1); | ||
|
||
} | ||
|
||
@Test(groups = "standalone") | ||
public void readingBytesReturnsFedContentWithoutChunkBoundariesWhenDisabled() throws Exception { | ||
byte[] content = "Test123".getBytes(StandardCharsets.US_ASCII); | ||
feedableBodyGenerator.setWriteChunkBoundaries(false); | ||
feedableBodyGenerator.feed(ByteBuffer.wrap(content), true); | ||
Body body = feedableBodyGenerator.createBody(); | ||
assertEquals(readFromBody(body), "Test123".getBytes(StandardCharsets.US_ASCII)); | ||
assertEquals(body.read(ByteBuffer.allocate(1)), -1); | ||
|
||
} | ||
|
||
private byte[] readFromBody(Body body) throws IOException { | ||
ByteBuffer byteBuffer = ByteBuffer.allocate(512); | ||
long read = body.read(byteBuffer); | ||
byteBuffer.flip(); | ||
byte[] readBytes = new byte[byteBuffer.remaining()]; | ||
byteBuffer.get(readBytes); | ||
return readBytes; | ||
} | ||
|
||
private static class TestFeedListener implements FeedableBodyGenerator.FeedListener { | ||
|
||
private int calls; | ||
@Override | ||
public void onContentAdded() { | ||
calls++; | ||
} | ||
|
||
public int getCalls() { | ||
return calls; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters