Search in sources :

Example 1 with TestWebSocketClient

use of org.springframework.graphql.web.TestWebSocketClient in project spring-graphql by spring-projects.

the class MockWebSocketGraphQlTransportTests method pingHandling.

@Test
void pingHandling() {
    TestWebSocketClient client = new TestWebSocketClient(new PingResponseHandler(this.result1));
    WebSocketGraphQlTransport transport = createTransport(client);
    StepVerifier.create(transport.execute(new GraphQlRequest("{Query1}"))).expectNext(this.result1).expectComplete().verify(TIMEOUT);
    assertActualClientMessages(client.getConnection(0), GraphQlMessage.connectionInit(null), GraphQlMessage.pong(null), GraphQlMessage.subscribe("1", new GraphQlRequest("{Query1}")));
}
Also used : GraphQlRequest(org.springframework.graphql.GraphQlRequest) TestWebSocketClient(org.springframework.graphql.web.TestWebSocketClient) Test(org.junit.jupiter.api.Test)

Example 2 with TestWebSocketClient

use of org.springframework.graphql.web.TestWebSocketClient in project spring-graphql by spring-projects.

the class MockWebSocketGraphQlTransportTests method start.

@Test
void start() {
    MockGraphQlWebSocketServer handler = new MockGraphQlWebSocketServer();
    handler.connectionInitHandler(payload -> Mono.just(Collections.singletonMap("key", payload.get("key") + "Ack")));
    TestWebSocketClient client = new TestWebSocketClient(handler);
    Map<String, String> initPayload = Collections.singletonMap("key", "valueInit");
    AtomicReference<Map<String, Object>> connectionAckRef = new AtomicReference<>();
    WebSocketGraphQlTransport transport = new WebSocketGraphQlTransport(URI.create("/"), HttpHeaders.EMPTY, client, ClientCodecConfigurer.create(), initPayload, connectionAckRef::set);
    transport.start().block(TIMEOUT);
    assertThat(client.getConnection(0).isOpen()).isTrue();
    assertThat(connectionAckRef.get()).isEqualTo(Collections.singletonMap("key", "valueInitAck"));
    assertActualClientMessages(client.getConnection(0), GraphQlMessage.connectionInit(initPayload));
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) TestWebSocketClient(org.springframework.graphql.web.TestWebSocketClient) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 3 with TestWebSocketClient

use of org.springframework.graphql.web.TestWebSocketClient in project spring-graphql by spring-projects.

the class MockWebSocketGraphQlTransportTests method errorBeforeConnectionAck.

@Test
void errorBeforeConnectionAck() {
    // Errors before GraphQL session initialized should be routed, no hanging on start
    MockGraphQlWebSocketServer handler = new MockGraphQlWebSocketServer();
    handler.connectionInitHandler(initPayload -> Mono.error(new IllegalStateException("boo")));
    TestWebSocketClient client = new TestWebSocketClient(handler);
    StepVerifier.create(createTransport(client).start()).expectErrorMessage("boo").verify(TIMEOUT);
}
Also used : TestWebSocketClient(org.springframework.graphql.web.TestWebSocketClient) Test(org.junit.jupiter.api.Test)

Example 4 with TestWebSocketClient

use of org.springframework.graphql.web.TestWebSocketClient in project spring-graphql by spring-projects.

the class MockWebSocketGraphQlTransportTests method errorDuringResponseHandling.

@Test
void errorDuringResponseHandling() {
    // Response handling errors that close the connection should terminate outstanding requests
    TestWebSocketClient client = new TestWebSocketClient(new UnexpectedResponseHandler());
    WebSocketGraphQlTransport transport = createTransport(client);
    String expectedMessage = "disconnected with CloseStatus[code=1002, reason=null]";
    StepVerifier.create(transport.execute(new GraphQlRequest("{Query1}"))).expectErrorSatisfies(ex -> assertThat(ex).hasMessageEndingWith(expectedMessage)).verify(TIMEOUT);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) StepVerifier(reactor.test.StepVerifier) WebSocketSession(org.springframework.web.reactive.socket.WebSocketSession) CloseStatus(org.springframework.web.reactive.socket.CloseStatus) WebSocketClient(org.springframework.web.reactive.socket.client.WebSocketClient) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestWebSocketClient(org.springframework.graphql.web.TestWebSocketClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutionResult(graphql.ExecutionResult) GraphQLError(graphql.GraphQLError) Duration(java.time.Duration) Map(java.util.Map) TestWebSocketConnection(org.springframework.graphql.web.TestWebSocketConnection) WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) URI(java.net.URI) MapExecutionResult(org.springframework.graphql.support.MapExecutionResult) HttpHeaders(org.springframework.http.HttpHeaders) GraphQlRequest(org.springframework.graphql.GraphQlRequest) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) GraphqlErrorBuilder(graphql.GraphqlErrorBuilder) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) ClientCodecConfigurer(org.springframework.http.codec.ClientCodecConfigurer) List(java.util.List) GraphQlMessageType(org.springframework.graphql.web.support.GraphQlMessageType) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) GraphQlRequest(org.springframework.graphql.GraphQlRequest) TestWebSocketClient(org.springframework.graphql.web.TestWebSocketClient) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)4 TestWebSocketClient (org.springframework.graphql.web.TestWebSocketClient)4 Map (java.util.Map)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 GraphQlRequest (org.springframework.graphql.GraphQlRequest)2 ExecutionResult (graphql.ExecutionResult)1 GraphQLError (graphql.GraphQLError)1 GraphqlErrorBuilder (graphql.GraphqlErrorBuilder)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Duration (java.time.Duration)1 Collections (java.util.Collections)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.when (org.mockito.Mockito.when)1 MapExecutionResult (org.springframework.graphql.support.MapExecutionResult)1 TestWebSocketConnection (org.springframework.graphql.web.TestWebSocketConnection)1