2020import java .nio .charset .StandardCharsets ;
2121
2222import io .fusionauth .http .BaseTest ;
23- import io .fusionauth .http .HTTPValues .ContentEncodings ;
2423import io .fusionauth .http .HTTPValues .Headers ;
2524import io .fusionauth .http .server .HTTPRequest ;
2625import io .fusionauth .http .server .HTTPServerConfiguration ;
2726import io .fusionauth .http .server .io .HTTPInputStream ;
28- import org .testng .annotations .DataProvider ;
2927import org .testng .annotations .Test ;
3028import static org .testng .Assert .assertEquals ;
3129
@@ -37,6 +35,7 @@ public class HTTPInputStreamTest extends BaseTest {
3735 public void read_chunked_withPushback (String contentEncoding ) throws Exception {
3836 // Ensure that when we read a chunked encoded body that the InputStream returns the correct number of bytes read even when
3937 // we read past the end of the current request and use the PushbackInputStream.
38+ // - Test with optional compression as well.
4039
4140 String content = "These pretzels are making me thirsty. These pretzels are making me thirsty. These pretzels are making me thirsty." ;
4241 byte [] payload = content .getBytes (StandardCharsets .UTF_8 );
@@ -46,26 +45,29 @@ public void read_chunked_withPushback(String contentEncoding) throws Exception {
4645 payload = compressUsingContentEncoding (payload , contentEncoding );
4746
4847 // Chunk the content, add part of the next request
49- payload = chunkEncoded (payload , 38 , null );
48+ payload = chunkEncode (payload , 38 , null );
5049
5150 ByteArrayOutputStream out = new ByteArrayOutputStream ();
5251 out .write (payload );
5352
5453 // Add part of the next request
55- out .write ("GET / HTTP/1.1\r \n " .getBytes (StandardCharsets .UTF_8 ));
54+ String nextRequest = "GET / HTTP/1.1\n \r " ;
55+ byte [] nextRequestBytes = nextRequest .getBytes (StandardCharsets .UTF_8 );
56+ out .write (nextRequestBytes );
5657
5758 HTTPRequest request = new HTTPRequest ();
5859 request .setHeader (Headers .ContentEncoding , contentEncoding );
5960 request .setHeader (Headers .TransferEncoding , "chunked" );
6061
6162 byte [] bytes = out .toByteArray ();
62- assertReadWithPushback (bytes , content , contentLength , 16 , request );
63+ assertReadWithPushback (bytes , content , contentLength , nextRequestBytes , request );
6364 }
6465
6566 @ Test (dataProvider = "contentEncoding" )
6667 public void read_fixedLength_withPushback (String contentEncoding ) throws Exception {
6768 // Ensure that when we read a fixed length body that the InputStream returns the correct number of bytes read even when
6869 // we read past the end of the current request and use the PushbackInputStream.
70+ // - Test with optional compression as well.
6971
7072 // Fixed length body with the start of the next request in the buffer
7173 String content = "These pretzels are making me thirsty. These pretzels are making me thirsty. These pretzels are making me thirsty." ;
@@ -82,7 +84,9 @@ public void read_fixedLength_withPushback(String contentEncoding) throws Excepti
8284 out .write (payload );
8385
8486 // Add part of the next request
85- out .write ("GET / HTTP/1.1\r \n " .getBytes (StandardCharsets .UTF_8 ));
87+ String nextRequest = "GET / HTTP/1.1\n \r " ;
88+ byte [] nextRequestBytes = nextRequest .getBytes (StandardCharsets .UTF_8 );
89+ out .write (nextRequestBytes );
8690
8791 HTTPRequest request = new HTTPRequest ();
8892 request .setHeader (Headers .ContentEncoding , contentEncoding );
@@ -91,10 +95,10 @@ public void read_fixedLength_withPushback(String contentEncoding) throws Excepti
9195 // body length is 113, when compressed it is 68 (gzip) or 78 (deflate)
9296 // The number of bytes available is 129.
9397 byte [] bytes = out .toByteArray ();
94- assertReadWithPushback (bytes , content , contentLength , 16 , request );
98+ assertReadWithPushback (bytes , content , contentLength , nextRequestBytes , request );
9599 }
96100
97- private void assertReadWithPushback (byte [] bytes , String content , int contentLength , int pushedBack , HTTPRequest request )
101+ private void assertReadWithPushback (byte [] bytes , String content , int contentLength , byte [] pushedBackBytes , HTTPRequest request )
98102 throws Exception {
99103 int bytesAvailable = bytes .length ;
100104 HTTPServerConfiguration configuration = new HTTPServerConfiguration ().withRequestBufferSize (bytesAvailable + 100 );
@@ -106,7 +110,9 @@ private void assertReadWithPushback(byte[] bytes, String content, int contentLen
106110 byte [] buffer = new byte [configuration .getRequestBufferSize ()];
107111 int read = httpInputStream .read (buffer );
108112
109- // TODO : Hmm.. with compression, this is returning the compressed bytes read instead of the un-compressed bytes read. WTF?
113+ // Note that the HTTPInputStream read will return the number of uncompressed bytes read. So the contentLength passed in
114+ // needs to represent the actual length of the request body, not the value of the Content-Length sent on the request which
115+ // would represent the size of the compressed entity body.
110116 assertEquals (read , contentLength );
111117 assertEquals (new String (buffer , 0 , read ), content );
112118
@@ -115,12 +121,12 @@ private void assertReadWithPushback(byte[] bytes, String content, int contentLen
115121 assertEquals (secondRead , -1 );
116122
117123 // We have 16 bytes left over
118- assertEquals (pushbackInputStream .getAvailableBufferedBytesRemaining (), pushedBack );
124+ assertEquals (pushbackInputStream .getAvailableBufferedBytesRemaining (), pushedBackBytes . length );
119125
120126 // Next read should start at the next request
121127 byte [] leftOverBuffer = new byte [100 ];
122128 int leftOverRead = pushbackInputStream .read (leftOverBuffer );
123- assertEquals (leftOverRead , pushedBack );
124- assertEquals (new String (leftOverBuffer , 0 , leftOverRead ), "GET / HTTP/1.1 \r \n " );
129+ assertEquals (leftOverRead , pushedBackBytes . length );
130+ assertEquals (new String (leftOverBuffer , 0 , leftOverRead ), new String ( pushedBackBytes ) );
125131 }
126132}
0 commit comments