|
| 1 | +package io.quarkus.smallrye.graphql.client.deployment; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import jakarta.inject.Inject; |
| 6 | +import jakarta.ws.rs.Consumes; |
| 7 | +import jakarta.ws.rs.POST; |
| 8 | +import jakarta.ws.rs.Path; |
| 9 | +import jakarta.ws.rs.Produces; |
| 10 | + |
| 11 | +import org.jboss.shrinkwrap.api.asset.EmptyAsset; |
| 12 | +import org.jboss.shrinkwrap.api.asset.StringAsset; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 15 | + |
| 16 | +import io.quarkus.test.QuarkusUnitTest; |
| 17 | +import io.smallrye.graphql.client.GraphQLClient; |
| 18 | +import io.smallrye.graphql.client.Response; |
| 19 | +import io.smallrye.graphql.client.dynamic.api.DynamicGraphQLClient; |
| 20 | + |
| 21 | +/** |
| 22 | + * This test verifies that if <code>quarkus.smallrye-graphql-client.CLIENT_NAME.allow-unexpected-response-fields=true</code> |
| 23 | + * is set, then the client , instead of throwing an exception, logs a warning if it receives an unexpected top-level |
| 24 | + * field in a response. |
| 25 | + */ |
| 26 | +public class GraphQLClientUnexpectedFieldsTest { |
| 27 | + |
| 28 | + static String url = "http://" + System.getProperty("quarkus.http.host", "localhost") + ":" |
| 29 | + + System.getProperty("quarkus.http.test-port", "8081") + "/invalid-graphql-endpoint"; |
| 30 | + |
| 31 | + @RegisterExtension |
| 32 | + static QuarkusUnitTest test = new QuarkusUnitTest() |
| 33 | + .withApplicationRoot((jar) -> jar |
| 34 | + .addAsResource( |
| 35 | + new StringAsset( |
| 36 | + "quarkus.smallrye-graphql-client.client1.url=" + url + "\n" + |
| 37 | + "quarkus.smallrye-graphql-client.client1.allow-unexpected-response-fields=true\n"), |
| 38 | + "application.properties") |
| 39 | + .addClass(MockGraphQLEndpoint.class) |
| 40 | + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")); |
| 41 | + |
| 42 | + @Path("/invalid-graphql-endpoint") |
| 43 | + @Produces("application/json+graphql") |
| 44 | + @Consumes("application/json") |
| 45 | + public static class MockGraphQLEndpoint { |
| 46 | + @POST |
| 47 | + public String returnInvalidResponse() { |
| 48 | + return "{\n" + |
| 49 | + " \"data\": {\n" + |
| 50 | + " \"number\": 32\n" + |
| 51 | + " },\n" + |
| 52 | + " \"bugs\": {\n" + |
| 53 | + " }\n" + |
| 54 | + "}"; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Inject |
| 59 | + @GraphQLClient("client1") |
| 60 | + DynamicGraphQLClient client; |
| 61 | + |
| 62 | + @Test |
| 63 | + public void ignoringUnexpectedResponseField() throws Exception { |
| 64 | + Response response = client.executeSync("query {something}}"); |
| 65 | + assertEquals(32, response.getObject(Long.class, "number")); |
| 66 | + } |
| 67 | +} |
0 commit comments