Search in sources :

Example 6 with WebSocketInterceptor

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

the class GraphQlWebSocketHandlerTests method connectionInitHandling.

@Test
void connectionInitHandling() throws Exception {
    WebSocketInterceptor interceptor = new WebSocketInterceptor() {

        @Override
        public Mono<Object> handleConnectionInitialization(String sessionId, Map<String, Object> payload) {
            Object value = payload.get("key");
            return Mono.just(Collections.singletonMap("key", value + " acknowledged"));
        }
    };
    handle(initWebSocketHandler(interceptor), new TextMessage("{\"type\":\"connection_init\",\"payload\":{\"key\":\"A\"}}"));
    StepVerifier.create(session.getOutput()).consumeNextWith((webSocketMessage) -> {
        GraphQlMessage message = decode(webSocketMessage);
        assertThat(message.resolvedType()).isEqualTo(GraphQlMessageType.CONNECTION_ACK);
        assertThat(message.<Map<String, Object>>getPayload()).containsEntry("key", "A acknowledged");
    }).then(// Complete output Flux
    this.session::close).expectComplete().verify(TIMEOUT);
}
Also used : GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) StepVerifier(reactor.test.StepVerifier) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GraphQlSetup(org.springframework.graphql.GraphQlSetup) CloseStatus(org.springframework.web.socket.CloseStatus) ArrayList(java.util.ArrayList) TextMessage(org.springframework.web.socket.TextMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) Assertions.as(org.assertj.core.api.Assertions.as) Duration(java.time.Duration) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) WebInterceptor(org.springframework.graphql.web.WebInterceptor) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) WebSocketMessage(org.springframework.web.socket.WebSocketMessage) HttpHeaders(org.springframework.http.HttpHeaders) ConsumeOneAndNeverCompleteInterceptor(org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) WebSocketHandlerTestSupport(org.springframework.graphql.web.WebSocketHandlerTestSupport) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) GraphQlMessageType(org.springframework.graphql.web.support.GraphQlMessageType) HttpInputMessage(org.springframework.http.HttpInputMessage) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Collections(java.util.Collections) WebSocketInterceptor(org.springframework.graphql.web.WebSocketInterceptor) InputStream(java.io.InputStream) GenericHttpMessageConverter(org.springframework.http.converter.GenericHttpMessageConverter) WebSocketInterceptor(org.springframework.graphql.web.WebSocketInterceptor) GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) Map(java.util.Map) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)6 WebSocketInterceptor (org.springframework.graphql.web.WebSocketInterceptor)6 Map (java.util.Map)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 CloseStatus (org.springframework.web.socket.CloseStatus)3 TextMessage (org.springframework.web.socket.TextMessage)3 Mono (reactor.core.publisher.Mono)3 GraphQlMessage (org.springframework.graphql.web.support.GraphQlMessage)2 CloseStatus (org.springframework.web.reactive.socket.CloseStatus)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 BiConsumer (java.util.function.BiConsumer)1 Consumer (java.util.function.Consumer)1 Assertions.as (org.assertj.core.api.Assertions.as)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1