26
26
import com .jayway .jsonpath .JsonPath ;
27
27
import org .junit .jupiter .api .Test ;
28
28
import reactor .core .publisher .Flux ;
29
- import reactor .core . publisher . Mono ;
29
+ import reactor .test . StepVerifier ;
30
30
31
31
import org .springframework .core .codec .DataBufferEncoder ;
32
+ import org .springframework .core .io .buffer .DefaultDataBuffer ;
32
33
import org .springframework .core .io .buffer .DefaultDataBufferFactory ;
33
- import org .springframework .graphql .GraphQlRequest ;
34
34
import org .springframework .graphql .GraphQlSetup ;
35
35
import org .springframework .graphql .server .WebGraphQlHandler ;
36
36
import org .springframework .graphql .server .support .SerializableGraphQlRequest ;
37
37
import org .springframework .http .MediaType ;
38
38
import org .springframework .http .codec .CodecConfigurer ;
39
+ import org .springframework .http .codec .DecoderHttpMessageReader ;
39
40
import org .springframework .http .codec .EncoderHttpMessageWriter ;
41
+ import org .springframework .http .codec .HttpMessageReader ;
40
42
import org .springframework .http .codec .HttpMessageWriter ;
41
43
import org .springframework .http .codec .ServerCodecConfigurer ;
42
44
import org .springframework .http .codec .json .Jackson2JsonDecoder ;
43
45
import org .springframework .http .codec .json .Jackson2JsonEncoder ;
44
46
import org .springframework .mock .http .server .reactive .MockServerHttpRequest ;
45
47
import org .springframework .mock .http .server .reactive .MockServerHttpResponse ;
46
- import org .springframework .mock .web .reactive .function .server .MockServerRequest ;
47
48
import org .springframework .mock .web .server .MockServerWebExchange ;
49
+ import org .springframework .web .reactive .function .server .ServerRequest ;
48
50
import org .springframework .web .reactive .function .server .ServerResponse ;
49
51
import org .springframework .web .reactive .result .view .ViewResolver ;
50
- import org .springframework .web .server .ServerWebExchange ;
51
52
52
53
import static org .assertj .core .api .Assertions .assertThat ;
53
54
57
58
*/
58
59
public class GraphQlHttpHandlerTests {
59
60
60
- private final GraphQlHttpHandler greetingHandler = GraphQlSetup .schemaContent ("type Query { greeting: String }" )
61
- .queryFetcher ("greeting" , (env ) -> "Hello" ).toHttpHandlerWebFlux ();
61
+ private static final List <HttpMessageReader <?>> MESSAGE_READERS =
62
+ List .of (new DecoderHttpMessageReader <>(new Jackson2JsonDecoder ()));
63
+
64
+ private final GraphQlHttpHandler greetingHandler =
65
+ GraphQlSetup .schemaContent ("type Query { greeting: String }" )
66
+ .queryFetcher ("greeting" , (env ) -> "Hello" )
67
+ .toHttpHandlerWebFlux ();
62
68
63
69
64
70
@ Test
65
- void shouldProduceApplicationJsonByDefault () {
71
+ void shouldProduceApplicationJsonByDefault () throws Exception {
72
+ String document = "{greeting}" ;
66
73
MockServerHttpRequest httpRequest = MockServerHttpRequest .post ("/" )
67
- .contentType (MediaType .APPLICATION_JSON ).accept (MediaType .ALL ).build ();
74
+ .contentType (MediaType .APPLICATION_JSON )
75
+ .accept (MediaType .ALL )
76
+ .body (initRequestBody (document ));
68
77
78
+ MockServerHttpResponse response = handleRequest (httpRequest , this .greetingHandler );
79
+
80
+ assertThat (response .getHeaders ().getContentType ()).isEqualTo (MediaType .APPLICATION_JSON );
81
+ StepVerifier .create (response .getBodyAsString ())
82
+ .expectNext ("{\" data\" :{\" greeting\" :\" Hello\" }}" )
83
+ .verifyComplete ();
84
+ }
85
+
86
+ @ Test
87
+ void shouldSupportApplicationGraphQl () throws Exception {
69
88
String document = "{greeting}" ;
70
- MockServerHttpResponse httpResponse = handleRequest (
71
- httpRequest , this .greetingHandler , initRequest (document ));
89
+ MockServerHttpRequest httpRequest = MockServerHttpRequest .post ("/" )
90
+ .contentType (MediaType .parseMediaType ("application/graphql" ))
91
+ .accept (MediaType .ALL )
92
+ .body (initRequestBody (document ));
72
93
73
- assertThat (httpResponse .getHeaders ().getContentType ()).isEqualTo (MediaType .APPLICATION_JSON );
94
+ MockServerHttpResponse response = handleRequest (httpRequest , this .greetingHandler );
95
+
96
+ assertThat (response .getHeaders ().getContentType ()).isEqualTo (MediaType .APPLICATION_JSON );
97
+ StepVerifier .create (response .getBodyAsString ())
98
+ .expectNext ("{\" data\" :{\" greeting\" :\" Hello\" }}" )
99
+ .verifyComplete ();
74
100
}
75
101
76
102
@ Test
77
- void shouldProduceApplicationGraphQl () {
103
+ void shouldProduceApplicationGraphQl () throws Exception {
78
104
MockServerHttpRequest httpRequest = MockServerHttpRequest .post ("/" )
79
- .contentType (MediaType .APPLICATION_JSON ).accept (MediaType .APPLICATION_GRAPHQL_RESPONSE ).build ();
105
+ .contentType (MediaType .APPLICATION_JSON )
106
+ .accept (MediaType .APPLICATION_GRAPHQL_RESPONSE )
107
+ .body (initRequestBody ("{greeting}" ));
80
108
81
- MockServerHttpResponse httpResponse = handleRequest (
82
- httpRequest , this .greetingHandler , initRequest ("{greeting}" ));
109
+ MockServerHttpResponse httpResponse = handleRequest (httpRequest , this .greetingHandler );
83
110
84
111
assertThat (httpResponse .getHeaders ().getContentType ()).isEqualTo (MediaType .APPLICATION_GRAPHQL_RESPONSE );
85
112
}
86
113
87
114
@ Test
88
- void shouldProduceApplicationJson () {
115
+ void shouldProduceApplicationJson () throws Exception {
89
116
MockServerHttpRequest httpRequest = MockServerHttpRequest .post ("/" )
90
- .contentType (MediaType .APPLICATION_JSON ).accept (MediaType .APPLICATION_JSON ).build ();
117
+ .contentType (MediaType .APPLICATION_JSON )
118
+ .accept (MediaType .APPLICATION_JSON )
119
+ .body (initRequestBody ("{greeting}" ));
91
120
92
- MockServerHttpResponse httpResponse = handleRequest (
93
- httpRequest , this .greetingHandler , initRequest ("{greeting}" ));
121
+ MockServerHttpResponse httpResponse = handleRequest (httpRequest , this .greetingHandler );
94
122
95
123
assertThat (httpResponse .getHeaders ().getContentType ()).isEqualTo (MediaType .APPLICATION_JSON );
96
124
}
97
125
98
126
@ Test
99
- void locale () {
127
+ void locale () throws Exception {
100
128
GraphQlHttpHandler handler = GraphQlSetup .schemaContent ("type Query { greeting: String }" )
101
129
.queryFetcher ("greeting" , (env ) -> "Hello in " + env .getLocale ())
102
130
.toHttpHandlerWebFlux ();
103
131
104
132
MockServerHttpRequest httpRequest = MockServerHttpRequest .post ("/" )
105
- .contentType (MediaType .APPLICATION_JSON ).accept (MediaType .APPLICATION_GRAPHQL_RESPONSE )
106
- .acceptLanguageAsLocales (Locale .FRENCH ).build ();
133
+ .contentType (MediaType .APPLICATION_JSON )
134
+ .accept (MediaType .APPLICATION_GRAPHQL_RESPONSE )
135
+ .acceptLanguageAsLocales (Locale .FRENCH )
136
+ .body (initRequestBody ("{greeting}" ));
107
137
108
- MockServerHttpResponse httpResponse = handleRequest (
109
- httpRequest , handler , initRequest ("{greeting}" ));
138
+ MockServerHttpResponse httpResponse = handleRequest (httpRequest , handler );
110
139
111
140
assertThat (httpResponse .getBodyAsString ().block ())
112
141
.isEqualTo ("{\" data\" :{\" greeting\" :\" Hello in fr\" }}" );
113
142
}
114
143
115
144
@ Test
116
- void shouldSetExecutionId () {
145
+ void shouldSetExecutionId () throws Exception {
117
146
GraphQlHttpHandler handler = GraphQlSetup .schemaContent ("type Query { showId: String }" )
118
147
.queryFetcher ("showId" , (env ) -> env .getExecutionId ().toString ())
119
148
.toHttpHandlerWebFlux ();
120
149
121
150
MockServerHttpRequest httpRequest = MockServerHttpRequest .post ("/" )
122
- .contentType (MediaType .APPLICATION_JSON ).accept (MediaType .APPLICATION_GRAPHQL_RESPONSE ).build ();
151
+ .contentType (MediaType .APPLICATION_JSON )
152
+ .accept (MediaType .APPLICATION_GRAPHQL_RESPONSE )
153
+ .body (initRequestBody ("{showId}" ));
123
154
124
- MockServerHttpResponse httpResponse = handleRequest (
125
- httpRequest , handler , initRequest ("{showId}" ));
155
+ MockServerHttpResponse httpResponse = handleRequest (httpRequest , handler );
126
156
127
157
DocumentContext document = JsonPath .parse (httpResponse .getBodyAsString ().block ());
128
158
String id = document .read ("data.showId" , String .class );
@@ -134,24 +164,25 @@ void shouldUseCustomCodec() {
134
164
WebGraphQlHandler webGraphQlHandler = GraphQlSetup .schemaContent ("type Query { showId: String }" )
135
165
.queryFetcher ("showId" , (env ) -> env .getExecutionId ().toString ())
136
166
.toWebGraphQlHandler ();
167
+
137
168
ObjectMapper mapper = new ObjectMapper ();
138
169
CodecConfigurer configurer = ServerCodecConfigurer .create ();
139
170
configurer .defaultCodecs ().jackson2JsonDecoder (new Jackson2JsonDecoder (mapper ));
140
171
configurer .defaultCodecs ().jackson2JsonEncoder (new Jackson2JsonEncoder (mapper ));
141
- GraphQlHttpHandler httpHandler = new GraphQlHttpHandler (webGraphQlHandler , configurer );
172
+
173
+ byte [] bytes = "{\" query\" : \" {showId}\" }" .getBytes (StandardCharsets .UTF_8 );
174
+ Flux <DefaultDataBuffer > body = Flux .just (DefaultDataBufferFactory .sharedInstance .wrap (bytes ));
142
175
143
176
MockServerHttpRequest httpRequest = MockServerHttpRequest .post ("/" )
144
- .contentType (MediaType .APPLICATION_JSON ).accept (MediaType .APPLICATION_GRAPHQL_RESPONSE ).build ();
177
+ .contentType (MediaType .APPLICATION_JSON )
178
+ .accept (MediaType .APPLICATION_GRAPHQL_RESPONSE )
179
+ .body (body );
145
180
146
181
MockServerWebExchange exchange = MockServerWebExchange .from (httpRequest );
147
- MockServerRequest serverRequest = MockServerRequest .builder ()
148
- .exchange (exchange )
149
- .uri (((ServerWebExchange ) exchange ).getRequest ().getURI ())
150
- .method (((ServerWebExchange ) exchange ).getRequest ().getMethod ())
151
- .headers (((ServerWebExchange ) exchange ).getRequest ().getHeaders ())
152
- .body (Flux .just (DefaultDataBufferFactory .sharedInstance .wrap ("{\" query\" :\" {showId}\" }" .getBytes (StandardCharsets .UTF_8 ))));
153
-
154
- httpHandler .handleRequest (serverRequest )
182
+ ServerRequest request = ServerRequest .create (exchange , configurer .getReaders ());
183
+
184
+ new GraphQlHttpHandler (webGraphQlHandler , configurer )
185
+ .handleRequest (request )
155
186
.flatMap (response -> response .writeTo (exchange , new EmptyContext ()))
156
187
.block ();
157
188
@@ -160,23 +191,15 @@ void shouldUseCustomCodec() {
160
191
assertThat (id ).isEqualTo (httpRequest .getId ());
161
192
}
162
193
163
- private static SerializableGraphQlRequest initRequest (String document ) {
194
+ private static String initRequestBody (String document ) throws Exception {
164
195
SerializableGraphQlRequest request = new SerializableGraphQlRequest ();
165
196
request .setQuery (document );
166
- return request ;
197
+ return new ObjectMapper (). writeValueAsString ( request ) ;
167
198
}
168
199
169
- private MockServerHttpResponse handleRequest (
170
- MockServerHttpRequest httpRequest , GraphQlHttpHandler handler , GraphQlRequest body ) {
171
-
200
+ private MockServerHttpResponse handleRequest (MockServerHttpRequest httpRequest , GraphQlHttpHandler handler ) {
172
201
MockServerWebExchange exchange = MockServerWebExchange .from (httpRequest );
173
-
174
- MockServerRequest serverRequest = MockServerRequest .builder ()
175
- .exchange (exchange )
176
- .uri (((ServerWebExchange ) exchange ).getRequest ().getURI ())
177
- .method (((ServerWebExchange ) exchange ).getRequest ().getMethod ())
178
- .headers (((ServerWebExchange ) exchange ).getRequest ().getHeaders ())
179
- .body (Mono .just (body ));
202
+ ServerRequest serverRequest = ServerRequest .create (exchange , MESSAGE_READERS );
180
203
181
204
handler .handleRequest (serverRequest )
182
205
.flatMap (response -> response .writeTo (exchange , new DefaultContext ()))
0 commit comments