@ -47,20 +47,21 @@ import static org.hamcrest.Matchers.containsString;
* /
* /
class GraphQlWebFluxAutoConfigurationTests {
class GraphQlWebFluxAutoConfigurationTests {
private static final String BASE_URL = "https://spring.example.org/ graphql ";
private static final String BASE_URL = "https://spring.example.org/ ";
private final ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner ( )
private final ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner ( )
. withConfiguration ( AutoConfigurations . of ( HttpHandlerAutoConfiguration . class , WebFluxAutoConfiguration . class ,
. withConfiguration ( AutoConfigurations . of ( HttpHandlerAutoConfiguration . class , WebFluxAutoConfiguration . class ,
CodecsAutoConfiguration . class , JacksonAutoConfiguration . class , GraphQlAutoConfiguration . class ,
CodecsAutoConfiguration . class , JacksonAutoConfiguration . class , GraphQlAutoConfiguration . class ,
GraphQlWebFluxAutoConfiguration . class ) )
GraphQlWebFluxAutoConfiguration . class ) )
. withUserConfiguration ( DataFetchersConfiguration . class , CustomWebInterceptor . class ) . withPropertyValues (
. withUserConfiguration ( DataFetchersConfiguration . class , CustomWebInterceptor . class )
"spring.main.web-application-type=reactive" , "spring.graphql.schema.printer.enabled=true" ) ;
. withPropertyValues ( "spring.main.web-application-type=reactive" , "spring.graphql.graphiql.enabled=true" ,
"spring.graphql.schema.printer.enabled=true" ) ;
@Test
@Test
void simpleQueryShouldWork ( ) {
void simpleQueryShouldWork ( ) {
testWithWebClient ( ( client ) - > {
testWithWebClient ( ( client ) - > {
String query = "{ bookById(id: \\\"book-1\\\"){ id name pageCount author } }" ;
String query = "{ bookById(id: \\\"book-1\\\"){ id name pageCount author } }" ;
client . post ( ) . uri ( " ") . bodyValue ( "{ \"query\": \"" + query + "\"}" ) . exchange ( ) . expectStatus ( ) . isOk ( )
client . post ( ) . uri ( " /graphql ") . bodyValue ( "{ \"query\": \"" + query + "\"}" ) . exchange ( ) . expectStatus ( ) . isOk ( )
. expectBody ( ) . jsonPath ( "data.bookById.name" ) . isEqualTo ( "GraphQL for beginners" ) ;
. expectBody ( ) . jsonPath ( "data.bookById.name" ) . isEqualTo ( "GraphQL for beginners" ) ;
} ) ;
} ) ;
}
}
@ -69,19 +70,21 @@ class GraphQlWebFluxAutoConfigurationTests {
void httpGetQueryShouldBeSupported ( ) {
void httpGetQueryShouldBeSupported ( ) {
testWithWebClient ( ( client ) - > {
testWithWebClient ( ( client ) - > {
String query = "{ bookById(id: \\\"book-1\\\"){ id name pageCount author } }" ;
String query = "{ bookById(id: \\\"book-1\\\"){ id name pageCount author } }" ;
client . get ( ) . uri ( " ?query={query}", "{ \"query\": \"" + query + "\"}" ) . exchange ( ) . expectStatus ( )
client . get ( ) . uri ( " /graphql ?query={query}", "{ \"query\": \"" + query + "\"}" ) . exchange ( ) . expectStatus ( )
. isEqualTo ( HttpStatus . METHOD_NOT_ALLOWED ) . expectHeader ( ) . valueEquals ( "Allow" , "POST" ) ;
. isEqualTo ( HttpStatus . METHOD_NOT_ALLOWED ) . expectHeader ( ) . valueEquals ( "Allow" , "POST" ) ;
} ) ;
} ) ;
}
}
@Test
@Test
void shouldRejectMissingQuery ( ) {
void shouldRejectMissingQuery ( ) {
testWithWebClient ( ( client ) - > client . post ( ) . uri ( "" ) . bodyValue ( "{}" ) . exchange ( ) . expectStatus ( ) . isBadRequest ( ) ) ;
testWithWebClient (
( client ) - > client . post ( ) . uri ( "/graphql" ) . bodyValue ( "{}" ) . exchange ( ) . expectStatus ( ) . isBadRequest ( ) ) ;
}
}
@Test
@Test
void shouldRejectQueryWithInvalidJson ( ) {
void shouldRejectQueryWithInvalidJson ( ) {
testWithWebClient ( ( client ) - > client . post ( ) . uri ( "" ) . bodyValue ( ":)" ) . exchange ( ) . expectStatus ( ) . isBadRequest ( ) ) ;
testWithWebClient (
( client ) - > client . post ( ) . uri ( "/graphql" ) . bodyValue ( ":)" ) . exchange ( ) . expectStatus ( ) . isBadRequest ( ) ) ;
}
}
@Test
@Test
@ -89,18 +92,28 @@ class GraphQlWebFluxAutoConfigurationTests {
testWithWebClient ( ( client ) - > {
testWithWebClient ( ( client ) - > {
String query = "{ bookById(id: \\\"book-1\\\"){ id name pageCount author } }" ;
String query = "{ bookById(id: \\\"book-1\\\"){ id name pageCount author } }" ;
client . post ( ) . uri ( " ") . bodyValue ( "{ \"query\": \"" + query + "\"}" ) . exchange ( ) . expectStatus ( ) . isOk ( )
client . post ( ) . uri ( " /graphql ") . bodyValue ( "{ \"query\": \"" + query + "\"}" ) . exchange ( ) . expectStatus ( ) . isOk ( )
. expectHeader ( ) . valueEquals ( "X-Custom-Header" , "42" ) ;
. expectHeader ( ) . valueEquals ( "X-Custom-Header" , "42" ) ;
} ) ;
} ) ;
}
}
@Test
@Test
void shouldExposeSchemaEndpoint ( ) {
void shouldExposeSchemaEndpoint ( ) {
testWithWebClient ( ( client ) - > client . get ( ) . uri ( "/ schema") . accept ( MediaType . ALL ) . exchange ( )
testWithWebClient ( ( client ) - > client . get ( ) . uri ( "/ graphql/ schema") . accept ( MediaType . ALL ) . exchange ( )
. expectStatus ( ) . isOk ( ) . expectHeader ( ) . contentType ( MediaType . TEXT_PLAIN ) . expectBody ( String . class )
. expectStatus ( ) . isOk ( ) . expectHeader ( ) . contentType ( MediaType . TEXT_PLAIN ) . expectBody ( String . class )
. value ( containsString ( "type Book" ) ) ) ;
. value ( containsString ( "type Book" ) ) ) ;
}
}
@Test
void shouldExposeGraphiqlEndpoint ( ) {
testWithWebClient ( ( client ) - > {
client . get ( ) . uri ( "/graphiql" ) . exchange ( ) . expectStatus ( ) . is3xxRedirection ( ) . expectHeader ( )
. location ( "https://spring.example.org/graphiql?path=/graphql" ) ;
client . get ( ) . uri ( "/graphiql?path=/graphql" ) . accept ( MediaType . ALL ) . exchange ( ) . expectStatus ( ) . isOk ( )
. expectHeader ( ) . contentType ( MediaType . TEXT_HTML ) ;
} ) ;
}
private void testWithWebClient ( Consumer < WebTestClient > consumer ) {
private void testWithWebClient ( Consumer < WebTestClient > consumer ) {
this . contextRunner . run ( ( context ) - > {
this . contextRunner . run ( ( context ) - > {
WebTestClient client = WebTestClient . bindToApplicationContext ( context ) . configureClient ( )
WebTestClient client = WebTestClient . bindToApplicationContext ( context ) . configureClient ( )