|
| 1 | +package io.smallrye.graphql.tests.client.dynamic.error; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.URL; |
| 5 | +import java.util.concurrent.ExecutionException; |
| 6 | + |
| 7 | +import jakarta.servlet.ServletException; |
| 8 | +import jakarta.servlet.annotation.WebServlet; |
| 9 | +import jakarta.servlet.http.HttpServlet; |
| 10 | +import jakarta.servlet.http.HttpServletRequest; |
| 11 | +import jakarta.servlet.http.HttpServletResponse; |
| 12 | + |
| 13 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 14 | +import org.jboss.arquillian.container.test.api.RunAsClient; |
| 15 | +import org.jboss.arquillian.junit.Arquillian; |
| 16 | +import org.jboss.arquillian.test.api.ArquillianResource; |
| 17 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 18 | +import org.jboss.shrinkwrap.api.asset.EmptyAsset; |
| 19 | +import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 20 | +import org.junit.Assert; |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.runner.RunWith; |
| 24 | + |
| 25 | +import io.smallrye.graphql.client.InvalidResponseException; |
| 26 | +import io.smallrye.graphql.client.vertx.dynamic.VertxDynamicGraphQLClient; |
| 27 | +import io.smallrye.graphql.client.vertx.dynamic.VertxDynamicGraphQLClientBuilder; |
| 28 | + |
| 29 | +/** |
| 30 | + * Verify that the client allows access to HTTP response headers when an invalid response is received. |
| 31 | + * In this test, a servlet is used to return a 418 I'm a teapot response with a custom header. |
| 32 | + * The client then makes a request and verifies that the InvalidResponseException contains the custom header. |
| 33 | + */ |
| 34 | +@RunWith(Arquillian.class) |
| 35 | +@RunAsClient |
| 36 | +public class InvalidResponseTest { |
| 37 | + |
| 38 | + @Deployment |
| 39 | + public static WebArchive deployment() { |
| 40 | + return ShrinkWrap.create(WebArchive.class, "invalid-response-test.war") |
| 41 | + .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") |
| 42 | + .addClasses(ErrorServlet.class); |
| 43 | + } |
| 44 | + |
| 45 | + @WebServlet(urlPatterns = { "/graphql" }) |
| 46 | + public static class ErrorServlet extends HttpServlet { |
| 47 | + @Override |
| 48 | + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 49 | + resp.setStatus(418); // I'm a teapot |
| 50 | + resp.setHeader("Teapot-Color", "Yellow"); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @ArquillianResource |
| 55 | + URL testingURL; |
| 56 | + |
| 57 | + private static VertxDynamicGraphQLClient client; |
| 58 | + |
| 59 | + @Before |
| 60 | + public void prepare() { |
| 61 | + client = (VertxDynamicGraphQLClient) new VertxDynamicGraphQLClientBuilder() |
| 62 | + .url(testingURL.toString() + "graphql") |
| 63 | + .build(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void receiveImATeapotResponse() throws ExecutionException, InterruptedException { |
| 68 | + try { |
| 69 | + client.executeSync("{}"); |
| 70 | + Assert.fail(); |
| 71 | + } catch (InvalidResponseException ex) { |
| 72 | + Assert.assertEquals("Yellow", ex.getTransportMeta().get("Teapot-Color").get(0)); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments