Search in sources :

Example 6 with GraphQlRequest

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

the class MockWebSocketGraphQlTransportTests method requestError.

@Test
void requestError() {
    GraphQlRequest request = this.mockServer.expectOperation("{Query1}").andRespondWithError(GraphqlErrorBuilder.newError().message("boo").build());
    StepVerifier.create(this.transport.execute(request)).consumeNextWith(result -> {
        assertThat(result.isDataPresent()).isFalse();
        assertThat(result.getErrors()).extracting(GraphQLError::getMessage).containsExactly("boo");
    }).expectComplete().verify(TIMEOUT);
    assertActualClientMessages(GraphQlMessage.connectionInit(null), GraphQlMessage.subscribe("1", request));
}
Also used : GraphQlRequest(org.springframework.graphql.GraphQlRequest) Test(org.junit.jupiter.api.Test)

Example 7 with GraphQlRequest

use of org.springframework.graphql.GraphQlRequest 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 8 with GraphQlRequest

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

the class MockWebSocketGraphQlTransportTests method stop.

@Test
void stop() {
    // Start
    this.transport.start().block(TIMEOUT);
    assertThat(this.webSocketClient.getConnectionCount()).isEqualTo(1);
    assertThat(this.webSocketClient.getConnection(0).isOpen()).isTrue();
    // Stop
    this.transport.stop().block(TIMEOUT);
    assertThat(this.webSocketClient.getConnection(0).isOpen()).isFalse();
    assertThat(this.webSocketClient.getConnection(0).closeStatus().block(TIMEOUT)).isEqualTo(CloseStatus.NORMAL);
    // New requests are rejected
    GraphQlRequest request = this.mockServer.expectOperation("{Query1}").andRespond(this.result1);
    StepVerifier.create(this.transport.execute(request)).expectErrorMessage("WebSocketGraphQlTransport has been stopped").verify(TIMEOUT);
    // Start
    this.transport.start().block(TIMEOUT);
    assertThat(this.webSocketClient.getConnectionCount()).isEqualTo(2);
    assertThat(this.webSocketClient.getConnection(1).isOpen()).isTrue();
    // Requests allowed again
    request = this.mockServer.expectOperation("{Query1}").andRespond(this.result1);
    StepVerifier.create(this.transport.execute(request)).expectNext(this.result1).expectComplete().verify(TIMEOUT);
}
Also used : GraphQlRequest(org.springframework.graphql.GraphQlRequest) Test(org.junit.jupiter.api.Test)

Example 9 with GraphQlRequest

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

the class MockWebSocketGraphQlTransportTests method request.

@Test
void request() {
    GraphQlRequest request = this.mockServer.expectOperation("{Query1}").andRespond(this.result1);
    StepVerifier.create(this.transport.execute(request)).expectNext(this.result1).expectComplete().verify(TIMEOUT);
    assertActualClientMessages(GraphQlMessage.connectionInit(null), GraphQlMessage.subscribe("1", request));
}
Also used : GraphQlRequest(org.springframework.graphql.GraphQlRequest) Test(org.junit.jupiter.api.Test)

Example 10 with GraphQlRequest

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

the class MockWebSocketGraphQlTransportTests method sessionIsCachedUntilClosed.

@Test
void sessionIsCachedUntilClosed() {
    GraphQlRequest request1 = this.mockServer.expectOperation("{Query1}").andRespond(this.result1);
    StepVerifier.create(this.transport.execute(request1)).expectNext(this.result1).expectComplete().verify(TIMEOUT);
    assertThat(this.webSocketClient.getConnectionCount()).isEqualTo(1);
    TestWebSocketConnection originalConnection = this.webSocketClient.getConnection(0);
    GraphQlRequest request2 = this.mockServer.expectOperation("{Query2}").andRespond(this.result2);
    StepVerifier.create(this.transport.execute(request2)).expectNext(this.result2).expectComplete().verify(TIMEOUT);
    assertThat(this.webSocketClient.getConnectionCount()).isEqualTo(1);
    assertThat(this.webSocketClient.getConnection(0)).isSameAs(originalConnection);
    // Close the connection
    originalConnection.closeServerSession(CloseStatus.NORMAL).block(TIMEOUT);
    request1 = this.mockServer.expectOperation("{Query1}").andRespond(this.result1);
    StepVerifier.create(this.transport.execute(request1)).expectNext(this.result1).expectComplete().verify(TIMEOUT);
    assertThat(this.webSocketClient.getConnectionCount()).isEqualTo(2);
    assertThat(this.webSocketClient.getConnection(1)).isNotSameAs(originalConnection);
}
Also used : TestWebSocketConnection(org.springframework.graphql.web.TestWebSocketConnection) GraphQlRequest(org.springframework.graphql.GraphQlRequest) Test(org.junit.jupiter.api.Test)

Aggregations

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