Skip to content

Commit 1017f7f

Browse files
committed
Tests
1 parent 3be21d9 commit 1017f7f

File tree

2 files changed

+68
-23
lines changed

2 files changed

+68
-23
lines changed

src/main/java/io/fusionauth/http/server/HTTPServerConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public LoggerFactory getLoggerFactory() {
183183
* take more time to read more bytes from the client before being able to re-use the connection.
184184
*
185185
* @return The maximum number of bytes to drain from the InputStream when the request handler did not read all available bytes. Defaults
186-
* to 256k.
186+
* to 256 Kilobytes.
187187
*/
188188
public int getMaxBytesToDrain() {
189189
return maxBytesToDrain;

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

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
import java.time.Duration;
2929
import java.util.List;
3030
import java.util.Map;
31+
import java.util.function.Consumer;
3132

3233
import io.fusionauth.http.HTTPValues.Headers;
3334
import io.fusionauth.http.io.MultipartConfiguration;
3435
import io.fusionauth.http.io.MultipartFileUploadPolicy;
3536
import io.fusionauth.http.server.CountingInstrumenter;
3637
import io.fusionauth.http.server.HTTPHandler;
3738
import io.fusionauth.http.server.HTTPServer;
39+
import io.fusionauth.http.server.HTTPServerConfiguration;
3840
import org.testng.annotations.Test;
3941
import static org.testng.Assert.assertEquals;
4042
import static org.testng.Assert.assertNotNull;
@@ -113,11 +115,13 @@ public void post_server_configuration_fileTooBig(String scheme) throws Exception
113115
// File too big even though the overall request size is ok.
114116
withScheme(scheme)
115117
.withFileSize(10 * 1024 * 1024) // 10 Mb
116-
.withConfiguration(new MultipartConfiguration().withFileUploadPolicy(MultipartFileUploadPolicy.Allow)
117-
// Max file size is 2Mb
118-
.withMaxFileSize(2 * 1024 * 1024)
119-
// Max request size is 15 Mb
120-
.withMaxRequestSize(15 * 1024 * 1024))
118+
.withConfiguration(config -> config.withMultipartConfiguration(
119+
new MultipartConfiguration().withFileUploadPolicy(MultipartFileUploadPolicy.Allow)
120+
// Max file size is 2Mb
121+
.withMaxFileSize(2 * 1024 * 1024)
122+
// Max request size is 15 Mb
123+
.withMaxRequestSize(15 * 1024 * 1024))
124+
)
121125
.expectResponse("""
122126
HTTP/1.1 413 \r
123127
connection: close\r
@@ -132,7 +136,9 @@ public void post_server_configuration_file_upload_allow(String scheme) throws Ex
132136
// File uploads allowed
133137
withScheme(scheme)
134138
.withFileCount(5)
135-
.withConfiguration(new MultipartConfiguration().withFileUploadPolicy(MultipartFileUploadPolicy.Allow))
139+
.withConfiguration(config -> config.withMultipartConfiguration(
140+
new MultipartConfiguration().withFileUploadPolicy(MultipartFileUploadPolicy.Allow))
141+
)
136142
.expectResponse("""
137143
HTTP/1.1 200 \r
138144
connection: keep-alive\r
@@ -148,7 +154,9 @@ public void post_server_configuration_file_upload_ignore(String scheme) throws E
148154
// File uploads ignored
149155
withScheme(scheme)
150156
.withFileCount(5)
151-
.withConfiguration(new MultipartConfiguration().withFileUploadPolicy(MultipartFileUploadPolicy.Ignore))
157+
.withConfiguration(config -> config.withMultipartConfiguration(
158+
new MultipartConfiguration().withFileUploadPolicy(MultipartFileUploadPolicy.Ignore))
159+
)
152160
// Ignored means that we will not see any files in the request handler
153161
.expectedFileCount(0)
154162
.expectResponse("""
@@ -165,8 +173,10 @@ public void post_server_configuration_file_upload_ignore(String scheme) throws E
165173
public void post_server_configuration_file_upload_reject(String scheme) throws Exception {
166174
// File uploads rejected
167175
withScheme(scheme)
168-
.withConfiguration(new MultipartConfiguration().withFileUploadPolicy(MultipartFileUploadPolicy.Reject))
169-
// 5 * 1 Mb = 5 Mb
176+
.withConfiguration(config -> config.withMultipartConfiguration(
177+
new MultipartConfiguration().withFileUploadPolicy(MultipartFileUploadPolicy.Reject))
178+
)
179+
// 5 * 1 MB = 5 Megabytes
170180
.withFileCount(5)
171181
.withFileSize(1024 * 1024)
172182
.expectResponse("""
@@ -187,11 +197,13 @@ public void post_server_configuration_requestTooBig(String scheme) throws Except
187197
withScheme(scheme)
188198
.withFileSize(1024 * 1024) // 1 Mb
189199
.withFileCount(15) // 15 files
190-
.withConfiguration(new MultipartConfiguration().withFileUploadPolicy(MultipartFileUploadPolicy.Allow)
191-
// Max file size is 2 Mb bytes
192-
.withMaxFileSize(2 * 1024 * 1024)
193-
// Max request size is 3 Mb
194-
.withMaxRequestSize(3 * 1024 * 1024))
200+
.withConfiguration(config -> config.withMultipartConfiguration(
201+
new MultipartConfiguration().withFileUploadPolicy(MultipartFileUploadPolicy.Allow)
202+
// Max file size is 2 Megabytes
203+
.withMaxFileSize(2 * 1024 * 1024)
204+
// Max request size is 3 Mb
205+
.withMaxRequestSize(3 * 1024 * 1024))
206+
)
195207
.expectResponse("""
196208
HTTP/1.1 413 \r
197209
connection: close\r
@@ -203,7 +215,35 @@ public void post_server_configuration_requestTooBig(String scheme) throws Except
203215
.expectExceptionOnWrite(SocketException.class);
204216
}
205217

206-
private Builder withConfiguration(MultipartConfiguration configuration) throws Exception {
218+
@Test(dataProvider = "schemes")
219+
public void post_server_configuration_requestTooBig_maxBodySize(String scheme) throws Exception {
220+
// Request too big, file size is ok, overall request size too big.
221+
// - Not using the MultipartConfiguration, instead use the default max body size.
222+
withScheme(scheme)
223+
.withFileSize(1024 * 1024) // 1 Megabyte
224+
.withFileCount(15) // 15 files
225+
.withConfiguration(config -> config.withMultipartConfiguration(
226+
// Set the multipart configuration to something very large.
227+
new MultipartConfiguration().withFileUploadPolicy(MultipartFileUploadPolicy.Allow)
228+
// Max file size is 2 GB bytes
229+
.withMaxFileSize(2L * 1024 * 1024 * 1024)
230+
// Max request size is 5 GB
231+
.withMaxRequestSize(5L * 1024 * 1024 * 1024))
232+
// Max request size is 3 Megabytes
233+
.withMaxRequestBodySize(Map.of("*", 3 * 1024 * 1024))
234+
)
235+
.expectResponse("""
236+
HTTP/1.1 413 \r
237+
connection: close\r
238+
content-length: 0\r
239+
\r
240+
""")
241+
// If the request is large enough, because we throw an exception before we have emptied the InputStream
242+
// we will take an exception while trying to write all the bytes to the server.
243+
.expectExceptionOnWrite(SocketException.class);
244+
}
245+
246+
private Builder withConfiguration(Consumer<HTTPServerConfiguration> configuration) throws Exception {
207247
return new Builder("http").withConfiguration(configuration);
208248
}
209249

@@ -213,7 +253,7 @@ private Builder withScheme(String scheme) {
213253

214254
@SuppressWarnings({"StringConcatenationInLoop", "unused", "UnusedReturnValue"})
215255
private class Builder {
216-
private MultipartConfiguration configuration;
256+
private Consumer<HTTPServerConfiguration> configuration;
217257

218258
private int expectedFileCount = 1;
219259

@@ -240,6 +280,7 @@ public Builder expectNoExceptionOnWrite() {
240280
return this;
241281
}
242282

283+
@SuppressWarnings("resource")
243284
public Builder expectResponse(String response) throws Exception {
244285
HTTPHandler handler = (req, res) -> {
245286
assertEquals(req.getContentType(), "multipart/form-data");
@@ -270,14 +311,18 @@ public Builder expectResponse(String response) throws Exception {
270311
}
271312
};
272313

273-
// Server level configuration : File uploads are disabled
274-
try (HTTPServer ignore = makeServer(scheme, handler, null)
314+
HTTPServer server = makeServer(scheme, handler, null)
275315
.withInitialReadTimeout(Duration.ofSeconds(30))
276316
.withKeepAliveTimeoutDuration(Duration.ofSeconds(30))
277317
.withMinimumWriteThroughput(1024)
278-
.withMinimumReadThroughput(1024)
279-
.withMultipartConfiguration(configuration)
280-
.start();
318+
.withMinimumReadThroughput(1024);
319+
320+
if (configuration != null) {
321+
configuration.accept(server.configuration());
322+
}
323+
324+
// Server level configuration : File uploads are disabled
325+
try (HTTPServer ignore = server.start();
281326
Socket socket = makeClientSocket(scheme)) {
282327

283328
socket.setSoTimeout((int) Duration.ofSeconds(30).toMillis());
@@ -336,7 +381,7 @@ public Builder expectedFileCount(int expectedFileCount) {
336381
return this;
337382
}
338383

339-
public Builder withConfiguration(MultipartConfiguration configuration) {
384+
public Builder withConfiguration(Consumer<HTTPServerConfiguration> configuration) {
340385
this.configuration = configuration;
341386
return this;
342387
}

0 commit comments

Comments
 (0)