Closed
Description
I enabled the WebSocket transport in a WebFlux Spring Graphql example, the complete source codes is here.
spring.graphql.websocket.path=/ws/graphql
I have written a test to verify the subscription like this.
@SpringBootTest()
@Slf4j
@AutoConfigureGraphQlTester
@AutoConfigureWebTestClient
class SubscriptionTests {
@Autowired
WebGraphQlTester graphQlTester;
@MockBean
PostService postService;
@MockBean
AuthorService authorService;
@Autowired
PostsDataFetcher dataFetcher;
@Test
void createCommentAndSubscription() {
when(postService.addComment(any(CommentInput.class))).thenReturn(Mono.just(UUID.randomUUID()));
when(postService.getCommentById(anyString())).thenReturn(Mono.just(
Comment.builder().id(UUID.randomUUID().toString())
.content("test comment")
.postId(UUID.randomUUID().toString())
.build()
));
String query = "subscription onCommentAdded { commentAdded { id postId content } }";
var verifier = graphQlTester.query(query)
.executeSubscription()
.toFlux("commentAdded.content", String.class)
.as(StepVerifier::create)
.expectNext("test comment")
.thenCancel()
.verifyLater();
// add comment
dataFetcher.addComment(
CommentInput.builder()
.postId(UUID.randomUUID().toString())
.content("test content")
.build()
)
.as(StepVerifier::create)
.consumeNextWith(comment -> assertThat(comment.getContent()).isEqualTo("test comment"))
.verifyComplete();
// verify the subscription now.
verifier.verify();
// verify the invocation of the mocks.
verify(postService, times(1)).addComment(any(CommentInput.class));
verify(postService, times(1)).getCommentById(anyString());
verifyNoMoreInteractions(postService);
}
}
I got an 404 error , /graphql url not found. The test tried to connect to HTTP not Web Socket endpoint.
In the standalone console output, found the following info:
2021-10-09 20:57:13.025 INFO 6364 --- [ Test worker] o.s.g.b.GraphQlWebFluxAutoConfiguration : GraphQL endpoint HTTP POST /graphql
2021-10-09 20:57:13.061 DEBUG 6364 --- [ Test worker] o.s.w.r.f.s.s.RouterFunctionMapping : 1 RouterFunction(s) in 'routerFunctionMapping'
2021-10-09 20:57:13.101 DEBUG 6364 --- [ Test worker] o.s.w.r.handler.SimpleUrlHandlerMapping : Patterns [/webjars/**, /**] in 'resourceHandlerMapping'
2021-10-09 20:57:13.162 INFO 6364 --- [ Test worker] o.s.g.b.GraphQlWebFluxAutoConfiguration : GraphQL endpoint WebSocket /ws/graphql
2021-10-09 20:57:13.163 DEBUG 6364 --- [ Test worker] o.s.w.r.handler.SimpleUrlHandlerMapping : Patterns [/ws/graphql] in 'graphQlWebSocketEndpoint'
2021-10-09 20:57:13.219 DEBUG 6364 --- [ Test worker] o.s.w.r.r.m.a.ControllerMethodResolver : ControllerAdvice beans: none
2021-10-09 20:57:13.313 DEBUG 6364 --- [ Test worker] o.s.w.s.adapter.HttpWebHandlerAdapter : enableLoggingRequestDetails='false': form data and headers will be masked to prevent unsafe logging of potentially sensitive data
2021-10-09 20:57:13.727 DEBUG 6364 --- [ Test worker] o.s.w.s.adapter.HttpWebHandlerAdapter : enableLoggingRequestDetails='false': form data and headers will be masked to prevent unsafe logging of potentially sensitive data
2021-10-09 20:57:13.751 DEBUG 6364 --- [ Test worker] o.s.w.s.adapter.HttpWebHandlerAdapter : enableLoggingRequestDetails='false': form data and headers will be masked to prevent unsafe logging of potentially sensitive data
2021-10-09 20:57:13.841 INFO 6364 --- [ Test worker] com.example.demo.SubscriptionTests : Started SubscriptionTests in 6.257 seconds (JVM running for 8.562)
2021-10-09 20:57:13.845 INFO 6364 --- [ Test worker] com.example.demo.DataInitializer : start data initialization...
2021-10-09 20:57:14.622 INFO 6364 --- [ Test worker] com.example.demo.DataInitializer : deleted rows: authors: 1, comments: 18, posts: 5
2021-10-09 20:57:14.713 INFO 6364 --- [ Test worker] com.example.demo.DataInitializer : done data initialization...
2021-10-09 20:57:14.710 INFO 6364 --- [actor-tcp-nio-1] com.example.demo.DataInitializer : author: AuthorEntity[id=72e03543-c53a-47b0-8e65-c0e82626c468, name=user, email=[email protected], createdAt=2021-10-09T20:57:14.629179]
2021-10-09 20:57:14.944 DEBUG 6364 --- [ Test worker] o.s.w.r.f.client.ExchangeFunctions : [2954c429] HTTP POST /graphql
2021-10-09 20:57:14.979 DEBUG 6364 --- [ parallel-2] o.s.http.codec.json.Jackson2JsonEncoder : [2954c429] Encoding [{query=subscription onCommentAdded { commentAdded { id postId content } }}]
2021-10-09 20:57:15.012 DEBUG 6364 --- [ parallel-2] o.s.w.s.adapter.HttpWebHandlerAdapter : [40a06180] HTTP POST "/graphql"
2021-10-09 20:57:15.032 DEBUG 6364 --- [ parallel-2] o.s.w.r.handler.SimpleUrlHandlerMapping : [40a06180] Mapped to ResourceWebHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"]
2021-10-09 20:57:15.037 DEBUG 6364 --- [ parallel-2] o.s.w.r.resource.ResourceWebHandler : [40a06180] Resource not found
2021-10-09 20:57:15.052 DEBUG 6364 --- [ parallel-2] org.springframework.web.HttpLogging : [40a06180] Resolved [ResponseStatusException: 404 NOT_FOUND] for HTTP POST /graphql
2021-10-09 20:57:15.067 DEBUG 6364 --- [ parallel-2] o.s.http.codec.json.Jackson2JsonEncoder : [40a06180] Encoding [{timestamp=Sat Oct 09 20:57:15 CST 2021, path=/graphql, status=404, error=Not Found, message=null, r (truncated)...]
2021-10-09 20:57:15.102 DEBUG 6364 --- [ parallel-2] o.s.w.s.adapter.HttpWebHandlerAdapter : [40a06180] Completed 404 NOT_FOUND
2021-10-09 20:57:15.108 DEBUG 6364 --- [ parallel-2] o.s.w.r.f.client.ExchangeFunctions : [2954c429] [44f83767] Response 404 NOT_FOUND
2021-10-09 20:57:15.123 ERROR 6364 --- [ Test worker] o.s.t.w.reactive.server.ExchangeResult : Request details for assertion failure:
> POST /graphql
> WebTestClient-Request-Id: [1]
> Content-Type: [application/json]
> Accept: [text/event-stream]
> Content-Length: [78]
{"query":"subscription onCommentAdded { commentAdded { id postId content } }"}
< 404 NOT_FOUND Not Found
< Content-Type: [application/json]
< Content-Length: [134]
{"timestamp":"2021-10-09T12:57:15.048+00:00","path":"/graphql","status":404,"error":"Not Found","message":null,"requestId":"40a06180"}