Skip to content

Commit 89362fe

Browse files
committed
Tests
1 parent 6ec7fef commit 89362fe

File tree

3 files changed

+69
-55
lines changed

3 files changed

+69
-55
lines changed

src/test/java/io/fusionauth/http/BaseTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import java.util.zip.InflaterInputStream;
6161

6262
import io.fusionauth.http.HTTPValues.Connections;
63+
import io.fusionauth.http.HTTPValues.ContentEncodings;
6364
import io.fusionauth.http.log.FileLogger;
6465
import io.fusionauth.http.log.FileLoggerFactory;
6566
import io.fusionauth.http.log.Level;
@@ -104,7 +105,21 @@
104105
* @author Brian Pontarelli
105106
*/
106107
public abstract class BaseTest {
108+
protected byte[] compressUsingContentEncoding(byte[] bytes, String contentEncoding) throws Exception {
109+
if (!contentEncoding.isEmpty()) {
110+
var requestEncodings = contentEncoding.toLowerCase().trim().split(",");
111+
for (String part : requestEncodings) {
112+
String encoding = part.trim();
113+
if (encoding.equals(ContentEncodings.Deflate)) {
114+
bytes = deflate(bytes);
115+
} else if (encoding.equals(ContentEncodings.Gzip)) {
116+
bytes = gzip(bytes);
117+
}
118+
}
119+
}
107120

121+
return bytes;
122+
}
108123
/**
109124
* This timeout is used for the HttpClient during each test. If you are in a debugger, you will need to change this timeout to be much
110125
* larger, otherwise, the client might truncate the request to the server.
@@ -248,6 +263,17 @@ public Object[][] connections() {
248263
};
249264
}
250265

266+
@DataProvider(name = "contentEncoding")
267+
public Object[][] contentEncoding() {
268+
return new Object[][]{
269+
{""},
270+
{"gzip"},
271+
{"deflate"},
272+
{"gzip, deflate"},
273+
{"deflate, gzip"}
274+
};
275+
}
276+
251277
@AfterMethod
252278
public void flush() {
253279
FileLogger fl = (FileLogger) FileLoggerFactory.FACTORY.getLogger(BaseTest.class);

src/test/java/io/fusionauth/http/io/HTTPInputStreamTest.java

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,6 @@
3333
* @author Daniel DeGroff
3434
*/
3535
public class HTTPInputStreamTest extends BaseTest {
36-
@DataProvider(name = "contentEncoding")
37-
public Object[][] contentEncoding() {
38-
return new Object[][]{
39-
{""},
40-
{"gzip"},
41-
{"deflate"},
42-
{"gzip, deflate"},
43-
{"deflate, gzip"}
44-
};
45-
}
46-
4736
@Test(dataProvider = "contentEncoding")
4837
public void read_chunked_withPushback(String contentEncoding) throws Exception {
4938
// Ensure that when we read a chunked encoded body that the InputStream returns the correct number of bytes read even when
@@ -53,18 +42,8 @@ public void read_chunked_withPushback(String contentEncoding) throws Exception {
5342
byte[] payload = content.getBytes(StandardCharsets.UTF_8);
5443
int contentLength = payload.length;
5544

56-
// Optionally compress the payload
57-
if (!contentEncoding.isEmpty()) {
58-
var requestEncodings = contentEncoding.toLowerCase().trim().split(",");
59-
for (String part : requestEncodings) {
60-
String encoding = part.trim();
61-
if (encoding.equals(ContentEncodings.Deflate)) {
62-
payload = deflate(payload);
63-
} else if (encoding.equals(ContentEncodings.Gzip)) {
64-
payload = gzip(payload);
65-
}
66-
}
67-
}
45+
// Optionally compress
46+
payload = compressUsingContentEncoding(payload, contentEncoding);
6847

6948
// Chunk the content, add part of the next request
7049
payload = chunkEncoded(payload, 38, null);
@@ -93,18 +72,8 @@ public void read_fixedLength_withPushback(String contentEncoding) throws Excepti
9372
byte[] payload = content.getBytes(StandardCharsets.UTF_8);
9473
int contentLength = payload.length;
9574

96-
// Optionally compress the payload
97-
if (!contentEncoding.isEmpty()) {
98-
var requestEncodings = contentEncoding.toLowerCase().trim().split(",");
99-
for (String part : requestEncodings) {
100-
String encoding = part.trim();
101-
if (encoding.equals(ContentEncodings.Deflate)) {
102-
payload = deflate(payload);
103-
} else if (encoding.equals(ContentEncodings.Gzip)) {
104-
payload = gzip(payload);
105-
}
106-
}
107-
}
75+
// Optionally compress
76+
payload = compressUsingContentEncoding(payload, contentEncoding);
10877

10978
// Content-Length must be the compressed length
11079
int compressedLength = payload.length;

src/test/java/io/fusionauth/http/util/HTTPToolsTest.java

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.fusionauth.http.util;
1717

1818
import java.io.ByteArrayInputStream;
19+
import java.io.ByteArrayOutputStream;
1920
import java.net.URLEncoder;
2021
import java.nio.charset.Charset;
2122
import java.nio.charset.StandardCharsets;
@@ -26,6 +27,7 @@
2627
import java.util.Map;
2728

2829
import com.inversoft.json.ToString;
30+
import io.fusionauth.http.BaseTest;
2931
import io.fusionauth.http.HTTPMethod;
3032
import io.fusionauth.http.io.PushbackInputStream;
3133
import io.fusionauth.http.server.HTTPRequest;
@@ -42,7 +44,7 @@
4244
* @author Brian Pontarelli
4345
*/
4446
@Test
45-
public class HTTPToolsTest {
47+
public class HTTPToolsTest extends BaseTest {
4648
@Test
4749
public void getMaxRequestBodySize() {
4850
var configuration = Map.of(
@@ -183,16 +185,18 @@ public void parseHeaderValue() {
183185
assertEquals(HTTPTools.parseHeaderValue("value; f= f"), new HeaderValue("value", Map.of("f", " f")));
184186
}
185187

186-
@Test
187-
public void parsePreamble() throws Exception {
188+
@Test(dataProvider = "contentEncoding")
189+
public void parsePreamble(String contentEncoding) throws Exception {
188190
// Ensure that we can correctly read the preamble when the InputStream contains the next request.
191+
// - Optionally compress the body to ensure we can push back bytes on the preamble read w/out hosing it up.
189192

190-
//noinspection ExtractMethodRecommender
191-
String request = """
193+
ByteArrayOutputStream out = new ByteArrayOutputStream();
194+
String header = """
192195
GET / HTTP/1.1\r
193196
Host: localhost:42\r
194197
Connection: close\r
195-
Content-Length: 113\r
198+
Content-Encoding: {contentEncoding}\r
199+
Content-Length: {contentLength}\r
196200
Header1: Value1\r
197201
Header2: Value2\r
198202
Header3: Value3\r
@@ -204,17 +208,31 @@ public void parsePreamble() throws Exception {
204208
Header9: Value9\r
205209
Header10: Value10\r
206210
\r
207-
These pretzels are making me thirsty. These pretzels are making me thirsty. These pretzels are making me thirsty.GET / HTTP/1.1\r
208-
""";
211+
""".replace("{contentEncoding}", contentEncoding);
212+
213+
// Now add the body, optionally compressed
214+
String body = "These pretzels are making me thirsty. These pretzels are making me thirsty. These pretzels are making me thirsty.";
215+
byte[] bodyBytes = body.getBytes(StandardCharsets.UTF_8);
216+
int uncompressedBodyLength = bodyBytes.length;
217+
bodyBytes = compressUsingContentEncoding(bodyBytes, contentEncoding);
218+
219+
header = header.replace("{contentLength}", bodyBytes.length + "");
220+
out.write(header.getBytes(StandardCharsets.UTF_8));
221+
out.write(bodyBytes);
222+
223+
// Now add part of the next request
224+
String nextRequest = "GET / HTTP/1.1\n\r";
225+
byte[] nextRequestBytes = nextRequest.getBytes(StandardCharsets.UTF_8);
226+
out.write(nextRequestBytes);
209227

210228
// Fixed length body with the start of the next request in the buffer
211-
byte[] bytes = request.getBytes(StandardCharsets.UTF_8);
212-
int bytesAvailable = bytes.length;
229+
byte[] payload = out.toByteArray();
230+
int bytesAvailable = payload.length;
213231

214232
// Ensure the request buffer size will contain the entire request.
215233
HTTPServerConfiguration configuration = new HTTPServerConfiguration().withRequestBufferSize(bytesAvailable + 100);
216234

217-
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
235+
ByteArrayInputStream is = new ByteArrayInputStream(payload);
218236
PushbackInputStream pushbackInputStream = new PushbackInputStream(is, null);
219237

220238
HTTPRequest httpRequest = new HTTPRequest();
@@ -229,37 +247,38 @@ public void parsePreamble() throws Exception {
229247
assertEquals(httpRequest.getMethod(), HTTPMethod.GET);
230248
assertEquals(httpRequest.getHost(), "localhost");
231249
assertEquals(httpRequest.getPort(), 42);
232-
assertEquals(httpRequest.getContentLength(), 113);
233-
assertEquals(httpRequest.getHeaders().size(), 13);
234-
assertEquals(httpRequest.getHeader("Content-Length"), "113");
250+
assertEquals(httpRequest.getContentLength(), bodyBytes.length);
251+
assertEquals(httpRequest.getHeaders().size(), contentEncoding.isEmpty() ? 13 : 14);
252+
assertEquals(httpRequest.getHeader("Content-Encoding"), contentEncoding.isEmpty() ? null : contentEncoding);
253+
assertEquals(httpRequest.getHeader("Content-Length"), bodyBytes.length + "");
235254
assertEquals(httpRequest.getHeader("Connection"), "close");
236255
assertEquals(httpRequest.getHeader("Host"), "localhost:42");
237256
for (int i = 1; i <= 10; i++) {
238257
assertEquals(httpRequest.getHeader("Header" + i), "Value" + i);
239258
}
240259

241-
// Expect 129 bytes left over which is 113 for the body + 16 from the next request
242-
assertEquals(pushbackInputStream.getAvailableBufferedBytesRemaining(), 113 + 16);
260+
// Expect that the body bytes and the next request have been pushed into the pushback buffer
261+
assertEquals(pushbackInputStream.getAvailableBufferedBytesRemaining(), bodyBytes.length + nextRequestBytes.length);
243262

244263
// Read the remaining bytes for this request, we should still have some left over.
245264
HTTPInputStream httpInputStream = new HTTPInputStream(configuration, httpRequest, pushbackInputStream, -1);
246265
byte[] buffer = new byte[1024];
247266
int read = httpInputStream.read(buffer);
248-
assertEquals(read, 113);
267+
assertEquals(read, uncompressedBodyLength);
249268

250269
// Another read should return -1 because we are at the end of this request.
251270
int nextRead = httpInputStream.read(buffer);
252271
assertEquals(nextRead, -1);
253272

254273
// The next read from the pushback which will be used by the next request should return the remaining bytes.
255-
assertEquals(pushbackInputStream.getAvailableBufferedBytesRemaining(), 16);
274+
assertEquals(pushbackInputStream.getAvailableBufferedBytesRemaining(), nextRequestBytes.length);
256275
int nextRequestRead = pushbackInputStream.read(buffer);
257-
assertEquals(nextRequestRead, 16);
276+
// Expect that we are able to read the bytes for the next request header
277+
assertEquals(nextRequestRead, nextRequestBytes.length);
258278
}
259279

260280
private void assertEncodedData(String actualValue, String expectedValue, Charset charset) {
261281
assertEncodedData(actualValue, expectedValue, charset, charset);
262-
263282
}
264283

265284
private void assertEncodedData(String actualValue, String expectedValue, Charset encodingCharset, Charset decodingCharset) {

0 commit comments

Comments
 (0)