20
20
import java .util .function .Consumer ;
21
21
22
22
import graphql .schema .idl .TypeRuntimeWiring ;
23
- import org .hamcrest .Matchers ;
24
23
import org .junit .jupiter .api .Test ;
25
24
26
25
import org .springframework .boot .autoconfigure .AutoConfigurations ;
37
36
import org .springframework .http .MediaType ;
38
37
import org .springframework .test .web .reactive .server .WebTestClient ;
39
38
39
+ import static org .hamcrest .Matchers .containsString ;
40
+
41
+ // @formatter:off
42
+
40
43
class WebFluxApplicationContextTests {
41
44
42
45
private static final AutoConfigurations AUTO_CONFIGURATIONS = AutoConfigurations .of (
@@ -49,57 +52,97 @@ class WebFluxApplicationContextTests {
49
52
@ Test
50
53
void query () {
51
54
testWithWebClient ((client ) -> {
52
- String query = "{" + " bookById(id: \\ \" book-1\\ \" ){ " + " id" + " name" + " pageCount"
53
- + " author" + " }" + "}" ;
54
-
55
- client .post ().uri ("" ).bodyValue ("{ \" query\" : \" " + query + "\" }" ).exchange ().expectStatus ().isOk ()
56
- .expectBody ().jsonPath ("data.bookById.name" ).isEqualTo ("GraphQL for beginners" );
55
+ String query = "{" +
56
+ " bookById(id: \\ \" book-1\\ \" ){ " +
57
+ " id" +
58
+ " name" +
59
+ " pageCount" +
60
+ " author" +
61
+ " }" +
62
+ "}" ;
63
+
64
+ client .post ().uri ("" ).bodyValue ("{ \" query\" : \" " + query + "\" }" )
65
+ .exchange ()
66
+ .expectStatus ()
67
+ .isOk ()
68
+ .expectBody ()
69
+ .jsonPath ("data.bookById.name" )
70
+ .isEqualTo ("GraphQL for beginners" );
57
71
});
58
72
}
59
73
60
74
@ Test
61
75
void queryMissing () {
62
- testWithWebClient ((client ) -> client .post ().uri ("" ).bodyValue ("{}" ).exchange ().expectStatus ().isBadRequest ());
76
+ testWithWebClient ((client ) ->
77
+ client .post ().uri ("" ).bodyValue ("{}" )
78
+ .exchange ()
79
+ .expectStatus ()
80
+ .isBadRequest ());
63
81
}
64
82
65
83
@ Test
66
84
void queryIsInvalidJson () {
67
- testWithWebClient ((client ) -> client .post ().uri ("" ).bodyValue (":)" ).exchange ().expectStatus ().isBadRequest ());
85
+ testWithWebClient ((client ) ->
86
+ client .post ().uri ("" ).bodyValue (":)" )
87
+ .exchange ()
88
+ .expectStatus ()
89
+ .isBadRequest ());
68
90
}
69
91
70
92
@ Test
71
93
void interceptedQuery () {
72
94
testWithWebClient ((client ) -> {
73
- String query = "{" + " bookById(id: \\ \" book-1\\ \" ){ " + " id" + " name" + " pageCount"
74
- + " author" + " }" + "}" ;
75
-
76
- client .post ().uri ("" ).bodyValue ("{ \" query\" : \" " + query + "\" }" ).exchange ().expectStatus ().isOk ()
77
- .expectHeader ().valueEquals ("X-Custom-Header" , "42" );
95
+ String query = "{" +
96
+ " bookById(id: \\ \" book-1\\ \" ){ " +
97
+ " id" +
98
+ " name" +
99
+ " pageCount" +
100
+ " author" +
101
+ " }" +
102
+ "}" ;
103
+
104
+ client .post ().uri ("" ).bodyValue ("{ \" query\" : \" " + query + "\" }" )
105
+ .exchange ()
106
+ .expectStatus ()
107
+ .isOk ()
108
+ .expectHeader ()
109
+ .valueEquals ("X-Custom-Header" , "42" );
78
110
});
79
111
}
80
112
81
113
@ Test
82
114
void schemaEndpoint () {
83
- testWithWebClient ((client ) -> client .get ().uri ("/schema" ).accept (MediaType .ALL ).exchange ().expectStatus ().isOk ()
84
- .expectHeader ().contentType (MediaType .TEXT_PLAIN ).expectBody (String .class )
85
- .value (Matchers .containsString ("type Book" )));
115
+ testWithWebClient ((client ) ->
116
+ client .get ().uri ("/schema" ).accept (MediaType .ALL )
117
+ .exchange ()
118
+ .expectStatus ()
119
+ .isOk ()
120
+ .expectHeader ()
121
+ .contentType (MediaType .TEXT_PLAIN )
122
+ .expectBody (String .class )
123
+ .value (containsString ("type Book" )));
86
124
}
87
125
88
126
private void testWithWebClient (Consumer <WebTestClient > consumer ) {
89
127
testWithApplicationContext ((context ) -> {
90
- WebTestClient client = WebTestClient .bindToApplicationContext (context ).configureClient ()
128
+ WebTestClient client = WebTestClient .bindToApplicationContext (context )
129
+ .configureClient ()
91
130
.defaultHeaders ((headers ) -> {
92
131
headers .setContentType (MediaType .APPLICATION_JSON );
93
132
headers .setAccept (Collections .singletonList (MediaType .APPLICATION_JSON ));
94
- }).baseUrl (BASE_URL ).build ();
133
+ })
134
+ .baseUrl (BASE_URL )
135
+ .build ();
95
136
consumer .accept (client );
96
137
});
97
138
}
98
139
99
140
private void testWithApplicationContext (ContextConsumer <ApplicationContext > consumer ) {
100
- new ReactiveWebApplicationContextRunner ().withConfiguration (AUTO_CONFIGURATIONS )
141
+ new ReactiveWebApplicationContextRunner ()
142
+ .withConfiguration (AUTO_CONFIGURATIONS )
101
143
.withUserConfiguration (DataFetchersConfiguration .class , CustomWebInterceptor .class )
102
- .withPropertyValues ("spring.main.web-application-type=reactive" ,
144
+ .withPropertyValues (
145
+ "spring.main.web-application-type=reactive" ,
103
146
"spring.graphql.schema.printer.enabled=true" ,
104
147
"spring.graphql.schema.location=classpath:books/schema.graphqls" )
105
148
.run (consumer );
@@ -110,8 +153,9 @@ public static class DataFetchersConfiguration {
110
153
111
154
@ Bean
112
155
public RuntimeWiringCustomizer bookDataFetcher () {
113
- return (runtimeWiring ) -> runtimeWiring .type (TypeRuntimeWiring .newTypeWiring ("Query" )
114
- .dataFetcher ("bookById" , GraphQlDataFetchers .getBookByIdDataFetcher ()));
156
+ return (runtimeWiring ) ->
157
+ runtimeWiring .type (TypeRuntimeWiring .newTypeWiring ("Query" )
158
+ .dataFetcher ("bookById" , GraphQlDataFetchers .getBookByIdDataFetcher ()));
115
159
}
116
160
117
161
}
@@ -121,8 +165,8 @@ public static class CustomWebInterceptor {
121
165
122
166
@ Bean
123
167
public WebInterceptor customWebInterceptor () {
124
- return (input , next ) -> next .handle (input )
125
- . map (( output ) -> output .transform ((builder ) -> builder .responseHeader ("X-Custom-Header" , "42" )));
168
+ return (input , next ) -> next .handle (input ). map (( output ) ->
169
+ output .transform ((builder ) -> builder .responseHeader ("X-Custom-Header" , "42" )));
126
170
}
127
171
128
172
}
0 commit comments