From bf1d07d66f52ef4591722a027044a0bb3784586f Mon Sep 17 00:00:00 2001 From: jcarranzan Date: Wed, 5 Jun 2024 09:29:57 +0200 Subject: [PATCH] tweak gzip tests --- .../ts/http/advanced/GzipMaxInputIT.java | 121 ++++++++---------- .../src/test/resources/gzip.properties | 2 - 2 files changed, 54 insertions(+), 69 deletions(-) diff --git a/http/http-advanced/src/test/java/io/quarkus/ts/http/advanced/GzipMaxInputIT.java b/http/http-advanced/src/test/java/io/quarkus/ts/http/advanced/GzipMaxInputIT.java index 4330cbd0b8..d234eda158 100644 --- a/http/http-advanced/src/test/java/io/quarkus/ts/http/advanced/GzipMaxInputIT.java +++ b/http/http-advanced/src/test/java/io/quarkus/ts/http/advanced/GzipMaxInputIT.java @@ -6,7 +6,7 @@ import java.io.IOException; import java.util.zip.GZIPOutputStream; -import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.HttpHeaders; import org.apache.http.HttpStatus; import org.junit.jupiter.api.Tag; @@ -21,106 +21,93 @@ public class GzipMaxInputIT { final byte[] zero_bytes = new byte[0]; - final String invalid_value = ""; - final byte[] _512K = new byte[512]; - final byte[] _100M = new byte[100 * 1024 * 1024]; - final byte[] _200M = new byte[200 * 1024 * 1024]; - - @QuarkusApplication(classes = { GzipResource.class }, properties = "gzip.properties") - static RestService app = new RestService(); + final byte[] SMALL_PAYLOAD = new byte[512]; + final byte[] LIMIT_PAYLOAD = new byte[100 * 1024 * 1024]; + final byte[] OVER_LIMIT_PAYLOAD = new byte[200 * 1024 * 1024]; /** * - * According "All configurations options" guide the property 'quarkus.resteasy.gzip.max-input' refers to - * - * Maximum deflated file bytes size - * - * If the limit is exceeded, Resteasy will return Response with status 413("Request Entity Too Large") - * + * Tests are checking server response on different size of sent payload + * Limit is configured using quarkus.resteasy.gzip.max-input property + * (According "All configurations options" guide the property 'quarkus.resteasy.gzip.max-input' refers to + * Maximum deflated file bytes size) + * If the limit is exceeded, Resteasy will return a response with status 413("Request Entity Too Large") */ + @QuarkusApplication(classes = { GzipResource.class }, properties = "gzip.properties") + static RestService app = new RestService(); @Test - void invalidValue() throws IOException { - app.given() - .contentType(MediaType.TEXT_PLAIN) - .header("Content-Encoding", "gzip") - .body(invalid_value) - .when() - .post("/gzip") - .then() - .statusCode(400); + void sendInvalidContent() { + Response response = sendStringDataToGzipEndpoint(invalid_value); + assertEquals(HttpStatus.SC_BAD_REQUEST, response.statusCode(), + "Invalid data as this void string should result in 400 BAD_REQUEST response"); } @Test - void sendZeroValue() throws IOException { + void sendZeroBytesPayload() throws IOException { byte[] compressedData = generateCompressedData(zero_bytes); + Response response = sendDataToGzipEndpoint(compressedData); + assertEquals(HttpStatus.SC_OK, response.statusCode(), + "The response should be 200 OK because the compression returns 2 bytes"); + } - Response response = app.given() - .contentType(MediaType.APPLICATION_OCTET_STREAM) - .header("Content-Encoding", "gzip") - .body(compressedData) - .when() - .post("/gzip") - .then() - .extract().response(); + @Test + void sendPayloadBelowMaxInputLimit() throws IOException { + byte[] compressedData = generateCompressedData(SMALL_PAYLOAD); + Response response = sendDataToGzipEndpoint(compressedData); assertEquals(HttpStatus.SC_OK, response.statusCode(), - "Because the compression return 2 bytes and then is a valid value"); + "The response should be 200 OK because sending just 512 bytes"); } @Tag("https://github.com/quarkusio/quarkus/issues/39636") @Test - void testGzipInputAtDefaultLimit() throws IOException { - byte[] compressedData = generateCompressedData(_100M); - Response response = app.given() - .contentType(MediaType.APPLICATION_OCTET_STREAM) - .header("Content-Encoding", "gzip") - .body(compressedData) - .when() - .post("/gzip") - .then() - .extract().response(); + void sendMaximumAllowedPayload() throws IOException { + byte[] compressedData = generateCompressedData(LIMIT_PAYLOAD); + Response response = sendDataToGzipEndpoint(compressedData); assertEquals(HttpStatus.SC_OK, response.statusCode(), - "This test will pass, because the value defined quarkus.resteasy.gzip.max-input=1024K and RESTEasy 1M default"); + "The response should be 200 OK because sending just the limit payload configured using " + + "quarkus.resteasy.gzip.max-input=100M. This fails if the suffix format parsing is not " + + "working and RESTEasy falls back to its default which is 10M"); } @Test - void failAlways() throws IOException { - byte[] compressedData = generateCompressedData(_200M); - Response response = app.given() - .contentType(MediaType.APPLICATION_OCTET_STREAM) - .header("Content-Encoding", "gzip") - .body(compressedData) + void sendMoreThanMaximumAllowedPayload() throws IOException { + byte[] compressedData = generateCompressedData(OVER_LIMIT_PAYLOAD); + Response response = sendDataToGzipEndpoint(compressedData); + assertEquals(HttpStatus.SC_REQUEST_TOO_LONG, response.statusCode(), + "The response should be 413 REQUEST_TOO_LONG when sending larger payload than the limit"); + } + + private Response sendDataToGzipEndpoint(byte[] data) { + return app.given() + .header(HttpHeaders.CONTENT_ENCODING, "gzip") + .body(data) .when() .post("/gzip") .then() .extract().response(); - assertEquals(HttpStatus.SC_REQUEST_TOO_LONG, response.statusCode(), - "RESTEASY003357: GZIP input exceeds max size: 104857600 and the default value is 100M"); } - @Test - void testGzipInputBelowMaxInputLimit() throws IOException { - byte[] compressedData = generateCompressedData(_512K); - - Response response = app.given() - .contentType(MediaType.APPLICATION_OCTET_STREAM) + private Response sendStringDataToGzipEndpoint(String data) { + return app.given() .header("Content-Encoding", "gzip") - .body(compressedData) + .body(data) .when() .post("/gzip") - .then().log().all() + .then() .extract().response(); - assertEquals(HttpStatus.SC_OK, response.statusCode(), - "This test will pass, because the value is less than defined value quarkus.resteasy.gzip.max-input=1024K and RESTEasy 1M default"); } public byte[] generateCompressedData(byte[] data) throws IOException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - GZIPOutputStream gzipOut = new GZIPOutputStream(baos); - gzipOut.write(data); - gzipOut.close(); - return baos.toByteArray(); + byte[] result; + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + GZIPOutputStream gzipOut = new GZIPOutputStream(baos)) { + gzipOut.write(data); + gzipOut.close(); + result = baos.toByteArray(); + } + return result; } } diff --git a/http/http-advanced/src/test/resources/gzip.properties b/http/http-advanced/src/test/resources/gzip.properties index cbb1d5c5ad..f379e936e7 100644 --- a/http/http-advanced/src/test/resources/gzip.properties +++ b/http/http-advanced/src/test/resources/gzip.properties @@ -2,5 +2,3 @@ quarkus.oidc.enabled=false #Gzip quarkus.resteasy.gzip.enabled=true quarkus.resteasy.gzip.max-input=100M -quarkus.http.compress-media-types=application/octet-stream -quarkus.rest-client.my-client.url=http://${quarkus.http.host}:${quarkus.http.port}