|
23 | 23 | import com.jayway.jsonpath.DocumentContext;
|
24 | 24 | import com.jayway.jsonpath.JsonPath;
|
25 | 25 | import org.junit.jupiter.api.Test;
|
26 |
| -import reactor.core.publisher.Mono; |
| 26 | +import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; |
| 27 | +import reactor.test.StepVerifier; |
27 | 28 |
|
28 |
| -import org.springframework.graphql.GraphQlRequest; |
29 | 29 | import org.springframework.graphql.GraphQlSetup;
|
30 | 30 | import org.springframework.graphql.server.support.SerializableGraphQlRequest;
|
31 | 31 | import org.springframework.http.MediaType;
|
| 32 | +import org.springframework.http.codec.DecoderHttpMessageReader; |
32 | 33 | import org.springframework.http.codec.EncoderHttpMessageWriter;
|
| 34 | +import org.springframework.http.codec.HttpMessageReader; |
33 | 35 | import org.springframework.http.codec.HttpMessageWriter;
|
| 36 | +import org.springframework.http.codec.json.Jackson2JsonDecoder; |
34 | 37 | import org.springframework.http.codec.json.Jackson2JsonEncoder;
|
35 | 38 | import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
36 | 39 | import org.springframework.mock.http.server.reactive.MockServerHttpResponse;
|
37 |
| -import org.springframework.mock.web.reactive.function.server.MockServerRequest; |
38 | 40 | import org.springframework.mock.web.server.MockServerWebExchange;
|
| 41 | +import org.springframework.web.reactive.function.server.ServerRequest; |
39 | 42 | import org.springframework.web.reactive.function.server.ServerResponse;
|
40 | 43 | import org.springframework.web.reactive.result.view.ViewResolver;
|
41 |
| -import org.springframework.web.server.ServerWebExchange; |
42 | 44 |
|
43 | 45 | import static org.assertj.core.api.Assertions.assertThat;
|
44 | 46 |
|
|
48 | 50 | */
|
49 | 51 | public class GraphQlHttpHandlerTests {
|
50 | 52 |
|
51 |
| - private final GraphQlHttpHandler greetingHandler = GraphQlSetup.schemaContent("type Query { greeting: String }") |
52 |
| - .queryFetcher("greeting", (env) -> "Hello").toHttpHandlerWebFlux(); |
| 53 | + private static final List<HttpMessageReader<?>> MESSAGE_READERS = |
| 54 | + List.of(new DecoderHttpMessageReader<>(new Jackson2JsonDecoder())); |
| 55 | + |
| 56 | + private final GraphQlHttpHandler greetingHandler = |
| 57 | + GraphQlSetup.schemaContent("type Query { greeting: String }") |
| 58 | + .queryFetcher("greeting", (env) -> "Hello") |
| 59 | + .toHttpHandlerWebFlux(); |
53 | 60 |
|
54 | 61 |
|
55 | 62 | @Test
|
56 |
| - void shouldProduceApplicationJsonByDefault() { |
| 63 | + void shouldProduceApplicationJsonByDefault() throws Exception { |
| 64 | + String document = "{greeting}"; |
57 | 65 | MockServerHttpRequest httpRequest = MockServerHttpRequest.post("/")
|
58 |
| - .contentType(MediaType.APPLICATION_JSON).accept(MediaType.ALL).build(); |
| 66 | + .contentType(MediaType.APPLICATION_JSON) |
| 67 | + .accept(MediaType.ALL) |
| 68 | + .body(initRequestBody(document)); |
59 | 69 |
|
60 |
| - String document = "{greeting}"; |
61 |
| - MockServerHttpResponse httpResponse = handleRequest( |
62 |
| - httpRequest, this.greetingHandler, initRequest(document)); |
| 70 | + MockServerHttpResponse response = handleRequest(httpRequest, this.greetingHandler); |
63 | 71 |
|
64 |
| - assertThat(httpResponse.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON); |
| 72 | + assertThat(response.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON); |
| 73 | + StepVerifier.create(response.getBodyAsString()) |
| 74 | + .expectNext("{\"data\":{\"greeting\":\"Hello\"}}") |
| 75 | + .verifyComplete(); |
65 | 76 | }
|
66 | 77 |
|
67 | 78 | @Test
|
68 |
| - void shouldProduceApplicationGraphQl() { |
| 79 | + void shouldProduceApplicationGraphQl() throws Exception { |
69 | 80 | MockServerHttpRequest httpRequest = MockServerHttpRequest.post("/")
|
70 |
| - .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_GRAPHQL_RESPONSE).build(); |
| 81 | + .contentType(MediaType.APPLICATION_JSON) |
| 82 | + .accept(MediaType.APPLICATION_GRAPHQL_RESPONSE) |
| 83 | + .body(initRequestBody("{greeting}")); |
71 | 84 |
|
72 |
| - MockServerHttpResponse httpResponse = handleRequest( |
73 |
| - httpRequest, this.greetingHandler, initRequest("{greeting}")); |
| 85 | + MockServerHttpResponse httpResponse = handleRequest(httpRequest, this.greetingHandler); |
74 | 86 |
|
75 | 87 | assertThat(httpResponse.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_GRAPHQL_RESPONSE);
|
76 | 88 | }
|
77 | 89 |
|
78 | 90 | @Test
|
79 |
| - void shouldProduceApplicationJson() { |
| 91 | + void shouldProduceApplicationJson() throws Exception { |
80 | 92 | MockServerHttpRequest httpRequest = MockServerHttpRequest.post("/")
|
81 |
| - .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).build(); |
| 93 | + .contentType(MediaType.APPLICATION_JSON) |
| 94 | + .accept(MediaType.APPLICATION_JSON) |
| 95 | + .body(initRequestBody("{greeting}")); |
82 | 96 |
|
83 |
| - MockServerHttpResponse httpResponse = handleRequest( |
84 |
| - httpRequest, this.greetingHandler, initRequest("{greeting}")); |
| 97 | + MockServerHttpResponse httpResponse = handleRequest(httpRequest, this.greetingHandler); |
85 | 98 |
|
86 | 99 | assertThat(httpResponse.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
|
87 | 100 | }
|
88 | 101 |
|
89 | 102 | @Test
|
90 |
| - void locale() { |
| 103 | + void locale() throws Exception { |
91 | 104 | GraphQlHttpHandler handler = GraphQlSetup.schemaContent("type Query { greeting: String }")
|
92 | 105 | .queryFetcher("greeting", (env) -> "Hello in " + env.getLocale())
|
93 | 106 | .toHttpHandlerWebFlux();
|
94 | 107 |
|
95 | 108 | MockServerHttpRequest httpRequest = MockServerHttpRequest.post("/")
|
96 |
| - .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_GRAPHQL_RESPONSE) |
97 |
| - .acceptLanguageAsLocales(Locale.FRENCH).build(); |
| 109 | + .contentType(MediaType.APPLICATION_JSON) |
| 110 | + .accept(MediaType.APPLICATION_GRAPHQL_RESPONSE) |
| 111 | + .acceptLanguageAsLocales(Locale.FRENCH) |
| 112 | + .body(initRequestBody("{greeting}")); |
98 | 113 |
|
99 |
| - MockServerHttpResponse httpResponse = handleRequest( |
100 |
| - httpRequest, handler, initRequest("{greeting}")); |
| 114 | + MockServerHttpResponse httpResponse = handleRequest(httpRequest, handler); |
101 | 115 |
|
102 | 116 | assertThat(httpResponse.getBodyAsString().block())
|
103 | 117 | .isEqualTo("{\"data\":{\"greeting\":\"Hello in fr\"}}");
|
104 | 118 | }
|
105 | 119 |
|
106 | 120 | @Test
|
107 |
| - void shouldSetExecutionId() { |
| 121 | + void shouldSetExecutionId() throws Exception { |
108 | 122 | GraphQlHttpHandler handler = GraphQlSetup.schemaContent("type Query { showId: String }")
|
109 | 123 | .queryFetcher("showId", (env) -> env.getExecutionId().toString())
|
110 | 124 | .toHttpHandlerWebFlux();
|
111 | 125 |
|
112 | 126 | MockServerHttpRequest httpRequest = MockServerHttpRequest.post("/")
|
113 |
| - .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_GRAPHQL_RESPONSE).build(); |
| 127 | + .contentType(MediaType.APPLICATION_JSON) |
| 128 | + .accept(MediaType.APPLICATION_GRAPHQL_RESPONSE) |
| 129 | + .body(initRequestBody("{showId}")); |
114 | 130 |
|
115 |
| - MockServerHttpResponse httpResponse = handleRequest( |
116 |
| - httpRequest, handler, initRequest("{showId}")); |
| 131 | + MockServerHttpResponse httpResponse = handleRequest(httpRequest, handler); |
117 | 132 |
|
118 | 133 | DocumentContext document = JsonPath.parse(httpResponse.getBodyAsString().block());
|
119 | 134 | String id = document.read("data.showId", String.class);
|
120 | 135 | assertThat(id).isEqualTo(httpRequest.getId());
|
121 | 136 | }
|
122 | 137 |
|
123 |
| - private static SerializableGraphQlRequest initRequest(String document) { |
| 138 | + private static String initRequestBody(String document) throws Exception { |
124 | 139 | SerializableGraphQlRequest request = new SerializableGraphQlRequest();
|
125 | 140 | request.setQuery(document);
|
126 |
| - return request; |
| 141 | + return new ObjectMapper().writeValueAsString(request); |
127 | 142 | }
|
128 | 143 |
|
129 |
| - private MockServerHttpResponse handleRequest( |
130 |
| - MockServerHttpRequest httpRequest, GraphQlHttpHandler handler, GraphQlRequest body) { |
131 |
| - |
| 144 | + private MockServerHttpResponse handleRequest(MockServerHttpRequest httpRequest, GraphQlHttpHandler handler) { |
132 | 145 | MockServerWebExchange exchange = MockServerWebExchange.from(httpRequest);
|
133 |
| - |
134 |
| - MockServerRequest serverRequest = MockServerRequest.builder() |
135 |
| - .exchange(exchange) |
136 |
| - .uri(((ServerWebExchange) exchange).getRequest().getURI()) |
137 |
| - .method(((ServerWebExchange) exchange).getRequest().getMethod()) |
138 |
| - .headers(((ServerWebExchange) exchange).getRequest().getHeaders()) |
139 |
| - .body(Mono.just(body)); |
| 146 | + ServerRequest serverRequest = ServerRequest.create(exchange, MESSAGE_READERS); |
140 | 147 |
|
141 | 148 | handler.handleRequest(serverRequest)
|
142 | 149 | .flatMap(response -> response.writeTo(exchange, new DefaultContext()))
|
|
0 commit comments