Search in sources :

Example 1 with WebSocketInterceptor

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

the class GraphQlWebSocketHandlerTests method connectionInitRejected.

@Test
void connectionInitRejected() {
    TestWebSocketSession session = handle(Flux.just(toWebSocketMessage("{\"type\":\"connection_init\"}")), new WebSocketInterceptor() {

        @Override
        public Mono<Object> handleConnectionInitialization(String sessionId, Map<String, Object> payload) {
            return Mono.error(new IllegalStateException());
        }
    });
    StepVerifier.create(session.getOutput()).verifyComplete();
    StepVerifier.create(session.closeStatus()).expectNext(new CloseStatus(4401, "Unauthorized")).expectComplete().verify(TIMEOUT);
}
Also used : WebSocketInterceptor(org.springframework.graphql.web.WebSocketInterceptor) Mono(reactor.core.publisher.Mono) CloseStatus(org.springframework.web.reactive.socket.CloseStatus) Test(org.junit.jupiter.api.Test)

Example 2 with WebSocketInterceptor

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

the class GraphQlWebSocketHandlerTests method connectionClosedHandling.

@Test
void connectionClosedHandling() throws Exception {
    CloseStatus closeStatus = CloseStatus.PROTOCOL_ERROR;
    AtomicBoolean called = new AtomicBoolean();
    WebSocketInterceptor interceptor = new WebSocketInterceptor() {

        @Override
        public void handleConnectionClosed(String sessionId, int status, Map<String, Object> payload) {
            called.set(true);
            assertThat(sessionId).isEqualTo("1");
            assertThat(status).isEqualTo(closeStatus.getCode());
            assertThat(payload).hasSize(1).containsEntry("key", "A");
        }
    };
    GraphQlWebSocketHandler handler = initWebSocketHandler(interceptor);
    handle(handler, new TextMessage("{\"type\":\"connection_init\",\"payload\":{\"key\":\"A\"}}"));
    StepVerifier.create(session.getOutput()).expectNextCount(1).then(// Complete output Flux
    this.session::close).expectComplete().verify(TIMEOUT);
    handler.afterConnectionClosed(this.session, closeStatus);
    assertThat(called).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketInterceptor(org.springframework.graphql.web.WebSocketInterceptor) CloseStatus(org.springframework.web.socket.CloseStatus) Map(java.util.Map) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Example 3 with WebSocketInterceptor

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

the class GraphQlWebSocketHandlerTests method connectionInitRejected.

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

        @Override
        public Mono<Object> handleConnectionInitialization(String sessionId, Map<String, Object> payload) {
            return Mono.error(new IllegalStateException());
        }
    };
    handle(initWebSocketHandler(interceptor), new TextMessage("{\"type\":\"connection_init\"}"));
    StepVerifier.create(session.closeStatus()).expectNext(new CloseStatus(4401, "Unauthorized")).expectComplete().verify(TIMEOUT);
}
Also used : WebSocketInterceptor(org.springframework.graphql.web.WebSocketInterceptor) CloseStatus(org.springframework.web.socket.CloseStatus) Map(java.util.Map) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Example 4 with WebSocketInterceptor

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

the class GraphQlWebSocketHandlerTests method connectionInitHandling.

@Test
void connectionInitHandling() {
    TestWebSocketSession session = handle(Flux.just(toWebSocketMessage("{\"type\":\"connection_init\",\"payload\":{\"key\":\"A\"}}")), 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"));
        }
    });
    StepVerifier.create(session.getOutput()).consumeNextWith((message) -> {
        GraphQlMessage actual = decode(message);
        assertThat(actual.resolvedType()).isEqualTo(GraphQlMessageType.CONNECTION_ACK);
        assertThat(actual.<Map<String, Object>>getPayload()).containsEntry("key", "A acknowledged");
    }).expectComplete().verify(TIMEOUT);
}
Also used : WebSocketInterceptor(org.springframework.graphql.web.WebSocketInterceptor) Mono(reactor.core.publisher.Mono) GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 5 with WebSocketInterceptor

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

the class GraphQlWebSocketHandlerTests method connectionClosedHandling.

@Test
void connectionClosedHandling() {
    CloseStatus closeStatus = CloseStatus.PROTOCOL_ERROR;
    AtomicBoolean called = new AtomicBoolean();
    TestWebSocketSession session = handle(Flux.just(toWebSocketMessage("{\"type\":\"connection_init\",\"payload\":{\"key\":\"A\"}}")), new WebSocketInterceptor() {

        @Override
        public void handleConnectionClosed(String sessionId, int status, Map<String, Object> payload) {
            called.set(true);
            assertThat(sessionId).isEqualTo("1");
            assertThat(status).isEqualTo(closeStatus.getCode());
            assertThat(payload).hasSize(1).containsEntry("key", "A");
        }
    });
    StepVerifier.create(session.getOutput()).expectNextCount(1).verifyComplete();
    StepVerifier.create(session.close(closeStatus)).verifyComplete();
    assertThat(called).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketInterceptor(org.springframework.graphql.web.WebSocketInterceptor) CloseStatus(org.springframework.web.reactive.socket.CloseStatus) 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