-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35767 from mskacelik/compression-SRGQL
Added gzip compression feature for SmallRye-GraphQL, added tests
- Loading branch information
Showing
5 changed files
with
122 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...phql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/CompressionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package io.quarkus.smallrye.graphql.deployment; | ||
|
||
import java.util.Arrays; | ||
|
||
import org.eclipse.microprofile.graphql.GraphQLApi; | ||
import org.eclipse.microprofile.graphql.Query; | ||
import org.hamcrest.CoreMatchers; | ||
import org.hamcrest.Matchers; | ||
import org.jboss.shrinkwrap.api.asset.EmptyAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class CompressionTest extends AbstractGraphQLTest { | ||
|
||
private static final String PI = "3.141592653589793238462643383279502884197169399375105820"; | ||
private static final String TAU = "6.28"; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")) | ||
.overrideConfigKey("quarkus.http.enable-compression", "true"); | ||
|
||
@Test | ||
public void singleValidQueryCompressedResponseTest() { | ||
String body = getPayload("{ validPiQuery }"); | ||
assertCompressed(body, PI); | ||
} | ||
|
||
@Test | ||
public void multipleValidQueriesCompressedResponseTest() { | ||
String body = getPayload("{ validPiQuery validTauQuery }"); | ||
assertCompressed(body, PI, TAU); | ||
} | ||
|
||
@Test | ||
public void singleInvalidQueryCompressedResponseTest() { | ||
String body = getPayload("{ invalidQuery }"); | ||
assertCompressed(body, "errors"); | ||
} | ||
|
||
private void assertCompressed(String body, String... expectedOutput) { | ||
org.hamcrest.Matcher messageMatcher = Arrays.stream(expectedOutput) | ||
.map(CoreMatchers::containsString) | ||
.reduce(Matchers.allOf(), (a, b) -> Matchers.allOf(a, b)); | ||
|
||
RestAssured.given() | ||
.body(body) | ||
.contentType(MEDIATYPE_JSON) | ||
.post("/graphql") | ||
.prettyPeek() | ||
.then() | ||
.assertThat() | ||
.statusCode(200) | ||
.header("Content-Encoding", "gzip") | ||
.body(messageMatcher); | ||
} | ||
|
||
@GraphQLApi | ||
public static class Schema { | ||
@Query | ||
public String validPiQuery() { | ||
return PI; | ||
} | ||
|
||
@Query | ||
public String validTauQuery() { | ||
return TAU; | ||
} | ||
|
||
@Query | ||
public String invalidQuery() { | ||
throw new RuntimeException(); | ||
} | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
.../src/main/java/io/quarkus/smallrye/graphql/runtime/SmallRyeGraphQLCompressionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.quarkus.smallrye.graphql.runtime; | ||
|
||
import io.vertx.core.Handler; | ||
import io.vertx.core.http.HttpHeaders; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
public class SmallRyeGraphQLCompressionHandler implements Handler<RoutingContext> { | ||
|
||
private final Handler<RoutingContext> routeHandler; | ||
|
||
public SmallRyeGraphQLCompressionHandler(Handler<RoutingContext> routeHandler) { | ||
this.routeHandler = routeHandler; | ||
} | ||
|
||
@Override | ||
public void handle(RoutingContext context) { | ||
context.addHeadersEndHandler(new Handler<Void>() { | ||
@Override | ||
public void handle(Void event) { | ||
context.response().headers().remove(HttpHeaders.CONTENT_ENCODING); | ||
} | ||
}); | ||
routeHandler.handle(context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters