From b61386f1b0c9d86167014a3f4799bafca3e942df Mon Sep 17 00:00:00 2001 From: jcarranzan Date: Wed, 22 May 2024 14:09:02 +0200 Subject: [PATCH] Add test coverage of 'resteasy.gzip.max-input' property add test with bigger bytes --- .../ts/http/restclient/GzipClientService.java | 14 +++ .../ts/http/restclient/GzipResource.java | 22 +++++ .../ts/http/restclient/GzipMaxInputIT.java | 97 +++++++++++++++++++ .../src/test/resources/gzip.properties | 5 + 4 files changed, 138 insertions(+) create mode 100644 http/rest-client/src/main/java/io/quarkus/ts/http/restclient/GzipClientService.java create mode 100644 http/rest-client/src/main/java/io/quarkus/ts/http/restclient/GzipResource.java create mode 100644 http/rest-client/src/test/java/io/quarkus/ts/http/restclient/GzipMaxInputIT.java create mode 100644 http/rest-client/src/test/resources/gzip.properties diff --git a/http/rest-client/src/main/java/io/quarkus/ts/http/restclient/GzipClientService.java b/http/rest-client/src/main/java/io/quarkus/ts/http/restclient/GzipClientService.java new file mode 100644 index 0000000000..6aeb9100fc --- /dev/null +++ b/http/rest-client/src/main/java/io/quarkus/ts/http/restclient/GzipClientService.java @@ -0,0 +1,14 @@ +package io.quarkus.ts.http.restclient; + +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; + +import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; +import org.jboss.resteasy.annotations.GZIP; + +@RegisterRestClient(configKey = "my-client") +public interface GzipClientService { + @POST + @Path("/gzip") + String gzip(@GZIP byte[] message); +} diff --git a/http/rest-client/src/main/java/io/quarkus/ts/http/restclient/GzipResource.java b/http/rest-client/src/main/java/io/quarkus/ts/http/restclient/GzipResource.java new file mode 100644 index 0000000000..62309347c1 --- /dev/null +++ b/http/rest-client/src/main/java/io/quarkus/ts/http/restclient/GzipResource.java @@ -0,0 +1,22 @@ +package io.quarkus.ts.http.restclient; + +import jakarta.inject.Inject; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; + +import org.eclipse.microprofile.rest.client.inject.RestClient; +import org.jboss.resteasy.annotations.GZIP; + +@Path("/gzip") +public class GzipResource { + + @Inject + @RestClient + GzipClientService gzipClient; + + @POST + public String gzip(@GZIP byte[] message) { + return gzipClient.gzip(message); + } + +} diff --git a/http/rest-client/src/test/java/io/quarkus/ts/http/restclient/GzipMaxInputIT.java b/http/rest-client/src/test/java/io/quarkus/ts/http/restclient/GzipMaxInputIT.java new file mode 100644 index 0000000000..84b4aaa0a9 --- /dev/null +++ b/http/rest-client/src/test/java/io/quarkus/ts/http/restclient/GzipMaxInputIT.java @@ -0,0 +1,97 @@ +package io.quarkus.ts.http.restclient; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.zip.GZIPOutputStream; + +import jakarta.ws.rs.core.MediaType; + +import org.apache.http.HttpStatus; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +import io.quarkus.test.bootstrap.RestService; +import io.quarkus.test.scenarios.QuarkusScenario; +import io.quarkus.test.services.QuarkusApplication; +import io.restassured.response.Response; + +@QuarkusScenario +public class GzipMaxInputIT { + + final byte[] gzip_bellow_max_input_1K = new byte[1000]; + final byte[] gzip_max_input_10M = new byte[10000000]; + + final byte[] gzip_max_input_100M = new byte[100000000]; + + @QuarkusApplication(classes = { GzipResource.class, GzipClientService.class }, properties = "gzip.properties") + static RestService app = new RestService(); + + /** + * + * 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") + * + */ + + @Test + void testGzipOverTheMaxLimit() throws IOException { + + byte[] compressedData = generateCompressedData(gzip_max_input_100M); + + Response response = app.given() + .contentType(MediaType.APPLICATION_OCTET_STREAM) + .header("Content-Encoding", "gzip") + .body(compressedData) + .when() + .post("/gzip") + .then() + .extract().response(); + assertEquals(HttpStatus.SC_REQUEST_TOO_LONG, response.statusCode()); + } + + @Test + void testGzipBigBytesOverLimit() throws IOException { + + byte[] compressedData = generateCompressedData(gzip_max_input_10M); + + Response response = app.given() + .contentType(MediaType.APPLICATION_OCTET_STREAM) + .header("Content-Encoding", "gzip") + .body(compressedData) + .when() + .post("/gzip") + .then() + .extract().response(); + assertEquals(HttpStatus.SC_REQUEST_TOO_LONG, response.statusCode()); + } + + @Disabled ("Because https://github.com/quarkusio/quarkus/issues/40806") + @Test + void testGzipBelowMaxLimit() throws IOException { + byte[] compressedData = generateCompressedData(gzip_bellow_max_input_1K); + + Response response = app.given() + .contentType(MediaType.APPLICATION_OCTET_STREAM) + .header("Content-Encoding", "gzip") + .body(compressedData) + .when() + .post("/gzip") + .then().log().all() + .extract().response(); + assertEquals(HttpStatus.SC_OK, response.statusCode()); + } + + private byte[] generateCompressedData(byte[] data) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + GZIPOutputStream gzipOut = new GZIPOutputStream(baos); + gzipOut.write(data); + gzipOut.close(); + return baos.toByteArray(); + } + +} diff --git a/http/rest-client/src/test/resources/gzip.properties b/http/rest-client/src/test/resources/gzip.properties new file mode 100644 index 0000000000..0c2518055d --- /dev/null +++ b/http/rest-client/src/test/resources/gzip.properties @@ -0,0 +1,5 @@ +#Gzip +quarkus.resteasy.gzip.enabled=true +quarkus.resteasy.gzip.max-input=1k +quarkus.http.compress-media-types=application/octet-stream +quarkus.rest-client.my-client.url=http://${quarkus.http.host}:${quarkus.http.port}