Skip to content

GraphQlTester support for WebSocket #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hantsy opened this issue Oct 9, 2021 · 7 comments
Closed

GraphQlTester support for WebSocket #163

hantsy opened this issue Oct 9, 2021 · 7 comments
Labels
in: test Issues related to the test module status: superseded Issue is superseded by another type: enhancement A general enhancement

Comments

@hantsy
Copy link
Contributor

hantsy commented Oct 9, 2021

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=user@example.com, 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"}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 9, 2021
@hantsy
Copy link
Contributor Author

hantsy commented Oct 10, 2021

The testing codes is working as expected against WebGraphQlTester with WebGraphQLHandler, I think here it does not use the real protocol.

@hantsy
Copy link
Contributor Author

hantsy commented Oct 10, 2021

It also does not work in an Integratoin tests running on a real HTTP server, check the integration tests.

@rstoyanchev
Copy link
Contributor

GraphQlTester does not support WebSocket, so at the moment, the only option to test subscriptions is to create the tester it with a WebGraphQlHandler, i.e. no running server.

@rstoyanchev rstoyanchev changed the title Subscription test failed in a WebFlux/WebSocket mock web env GraphQlTester support for WebSocket Oct 13, 2021
@bclozel
Copy link
Member

bclozel commented Oct 13, 2021

We're going to provide more testing infrastructure for testing with WebGraphQlHandler in #75.

@hantsy
Copy link
Contributor Author

hantsy commented Oct 23, 2021

Hope this done in the GraphQlClient #10 , close this issue now. I will try to use a pure WebSocketClient to interact websocket protocol to see if working as expected.

@hantsy hantsy closed this as completed Oct 23, 2021
@rstoyanchev
Copy link
Contributor

@hantsy not sure why you closed this, but we do need to add WebSocket transport support for @GraphQlTester.

@rstoyanchev rstoyanchev reopened this Oct 26, 2021
@rstoyanchev rstoyanchev added in: test Issues related to the test module type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 26, 2021
@rstoyanchev rstoyanchev added this to the 1.0.0-M4 milestone Oct 26, 2021
@bclozel bclozel modified the milestones: 1.0.0-M4, 1.0.0-M5 Dec 2, 2021
@rstoyanchev rstoyanchev modified the milestones: 1.0.0-M5, 1.0 Backlog Jan 6, 2022
@rstoyanchev
Copy link
Contributor

This is available now as of M6 and was done as part of #317.

@rstoyanchev rstoyanchev added the status: superseded Issue is superseded by another label Mar 24, 2022
@rstoyanchev rstoyanchev removed this from the 1.0 Backlog milestone Mar 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues related to the test module status: superseded Issue is superseded by another type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants