Skip to content

Commit

Permalink
tweak gzip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarranzan committed Jun 5, 2024
1 parent c896b50 commit bf1d07d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

}
2 changes: 0 additions & 2 deletions http/http-advanced/src/test/resources/gzip.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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}

0 comments on commit bf1d07d

Please sign in to comment.