Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Test gzip support in RESTEasy Reactive #16924

Closed
wants to merge 9 commits into from
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package io.quarkus.resteasy.reactive;

import java.util.function.Supplier;

import javax.enterprise.event.Observes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

import org.hamcrest.Matchers;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;
import io.vertx.ext.web.Router;

public class GZipTest {

private static final String APP_PROPS = "" +
"quarkus.http.enable-compression=true\n";
Copy link
Contributor

@netodevel netodevel Apr 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think of a test with the other way?

quarkus.http.enable-compression=false

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is disabled, should we really perform any sort of test? I don't think that's needed when is not even enabled :)

Copy link
Contributor

@netodevel netodevel May 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but how to ensure that the property is working correctly?
and how do you make sure that no future code from other developers doesn't affect this and go undetected because not have any tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FroMage WDYT

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, we should test the default behaviour, that's true, but because we know what the default is ATM, we want to test the default behaviour without any configuration, to make sure it won't be compressed by default.


static String longString;
static {

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1000; ++i) {
sb.append("Hello RESTEasy Reactive;");
}
longString = sb.toString();
}

@RegisterExtension
static final QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(new Supplier<JavaArchive>() {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class)
.addAsResource(new StringAsset(APP_PROPS), "application.properties")
.addClasses(TestCompression.class);
}
});

@Test
public void testServerCompression() throws Exception {

RestAssured.given().get("/test/compression").then().statusCode(200)
.header("content-encoding", "gzip")
.header("content-length", Matchers.not(Matchers.equalTo(Integer.toString(longString.length()))))
.body(Matchers.equalTo(longString));

RestAssured.given().get("/test/nocompression").then().statusCode(200)
.header("content-encoding", "identity")
.header("content-length", Matchers.equalTo(Integer.toString(longString.length())))
.body(Matchers.equalTo(longString));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So does this test pass? I don't see anything in the endpoints that would justify a different behaviour.
Could you test with returning a binary reponse, such as an image? Just to see what vert.x does in this case, if it stll tries to compress it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
It is failing with this info.
For endpoint /test/nocompression

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created an image and returned to the endpoint. This time also, content-encoding is gzip.
image

Copy link
Contributor Author

@saumya1singh saumya1singh May 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in the beginning I am initializing APP_PROPS as true

private static final String APP_PROPS = "" +
            "quarkus.http.enable-compression=true\n";

perhaps that's why content encoding is always gzip even for Binary Data i.e Images

That means vert.x is trying to compress the binary data as well, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I set quarkus.http.enable-compression to false , the content-length=24000 bytes which is far more than 130 bytes (for text) or 165 bytes(for image)

private static final String APP_PROPS = "" +
            "quarkus.http.enable-compression=false\n";

image

Copy link
Contributor Author

@saumya1singh saumya1singh May 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So perhaps the conclusion is - It is trying to compress images as well.

Am I right @FroMage?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, probably, could you add that test to your PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sure, I missed this comment.


// @ApplicationScoped
// static class BeanRegisteringRouteUsingObserves {
//
// public void register(@Observes Router router) {
//
// router.route("/compression").handler(rc -> {
// rc.response().end(longString);
// });
// router.route("/nocompression").handler(rc -> {
// rc.response().headers().set("content-encoding", "identity");
// rc.response().end(longString);
// });
// }
//
// }

@Path("/test")
public static class TestCompression {

//TODO - complete logic in method
@Path("/compression")
@GET
public void registerCompression(@Observes Router router) {
router.route("/test/compression").handler(routingContext -> {
saumya1singh marked this conversation as resolved.
Show resolved Hide resolved
routingContext.response().end(longString);
});
}

@Path("/nocompression")
@GET
public void registerNoCompression(@Observes Router router) {

}
}

}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The image I am creating in createImage() looks like this -

image

Perhaps "saving the image" is optional.