-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from sleekweasel/chunking-support
Add chunked support from PipeInputStreams
- Loading branch information
Showing
2 changed files
with
109 additions
and
17 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
59 changes: 59 additions & 0 deletions
59
core/src/test/java/fi/iki/elonen/HttpChunkedResponseTest.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,59 @@ | ||
package fi.iki.elonen; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PipedInputStream; | ||
|
||
import static fi.iki.elonen.NanoHTTPD.Response.Status.OK; | ||
|
||
public class HttpChunkedResponseTest extends HttpServerTest { | ||
@org.junit.Test | ||
public void thatChunkedContentIsChunked() throws Exception { | ||
PipedInputStream pipedInputStream = new ChunkedInputStream(new String[]{ | ||
"some", | ||
"thing which is longer than sixteen characters", | ||
"whee!", | ||
"" | ||
}); | ||
String[] expected = { | ||
"HTTP/1.1 200 OK", | ||
"Content-Type: what/ever", | ||
"Date: .*", | ||
"Connection: keep-alive", | ||
"Transfer-Encoding: chunked", | ||
"", | ||
"4", | ||
"some", | ||
"2d", | ||
"thing which is longer than sixteen characters", | ||
"5", | ||
"whee!", | ||
"0", | ||
"" | ||
}; | ||
testServer.response = new NanoHTTPD.Response(OK, "what/ever", pipedInputStream); | ||
testServer.response.setChunkedTransfer(true); | ||
|
||
ByteArrayOutputStream byteArrayOutputStream = invokeServer("GET / HTTP/1.0"); | ||
|
||
assertResponse(byteArrayOutputStream, expected); | ||
} | ||
|
||
private static class ChunkedInputStream extends PipedInputStream { | ||
int chunk = 0; | ||
String[] chunks; | ||
|
||
private ChunkedInputStream(String[] chunks) { | ||
this.chunks = chunks; | ||
} | ||
|
||
@Override | ||
public synchronized int read(byte[] buffer) throws IOException { | ||
// Too implementation-linked, but... | ||
for (int i = 0; i < chunks[chunk].length(); ++i) { | ||
buffer[i] = (byte) chunks[chunk].charAt(i); | ||
} | ||
return chunks[chunk++].length(); | ||
} | ||
} | ||
} |