Skip to content

Commit 8b5e2a3

Browse files
committed
Update WebFlux WebSocket sample tests
- combine existing tests into one class - add integration tests
1 parent b8c4b25 commit 8b5e2a3

File tree

3 files changed

+76
-11
lines changed

3 files changed

+76
-11
lines changed

samples/webflux-websocket/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- [Data Controller](src/main/java/io/spring/sample/graphql/SampleController.java) with reactive methods for queries and subscriptions.
44
- [WebFilter](src/main/java/io/spring/sample/graphql/ContextWebFilter.java) to insert Reactor `Context` and check it can be accessed in [DataRepository](src/main/java/io/spring/sample/graphql/DataRepository.java).
5-
- Subscription [tests](src/test/java/io/spring/sample/graphql/SubscriptionTests.java) using Reactor's `StepVerifier` to verify the stream.
6-
5+
- [Server tests](src/test/java/io/spring/sample/graphql/WebFluxWebSocketSampleTests.java) without a client.
6+
- [Integration tests](src/test/java/io/spring/sample/graphql/WebFluxWebSocketSampleIntegrationTests.java) via `WebSocketGraphQlTester`.
7+
78
GraphiQL does not support subscriptions. There is a basic index.html page that logs subscriptions the console.
Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,59 @@
1515
*/
1616
package io.spring.sample.graphql;
1717

18+
import java.net.URI;
19+
20+
import org.junit.jupiter.api.BeforeEach;
1821
import org.junit.jupiter.api.Test;
1922
import reactor.core.publisher.Flux;
2023
import reactor.test.StepVerifier;
2124

22-
import org.springframework.beans.factory.annotation.Autowired;
23-
import org.springframework.boot.test.autoconfigure.graphql.GraphQlTest;
24-
import org.springframework.context.annotation.Import;
25+
import org.springframework.beans.factory.annotation.Value;
26+
import org.springframework.boot.test.context.SpringBootTest;
27+
import org.springframework.boot.web.server.LocalServerPort;
2528
import org.springframework.graphql.test.tester.GraphQlTester;
29+
import org.springframework.graphql.test.tester.WebSocketGraphQlTester;
30+
import org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient;
2631

2732
/**
28-
* GraphQL over WebSocket subscription tests.
33+
* GraphQL over WebSocket integration tests.
2934
*/
30-
@GraphQlTest(SampleController.class)
31-
@Import(DataRepository.class)
32-
public class WebFluxWebSocketSampleSubscriptionTests {
35+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
36+
public class WebFluxWebSocketSampleIntegrationTests {
37+
38+
@LocalServerPort
39+
private int port;
40+
41+
@Value("http://localhost:${local.server.port}${spring.graphql.websocket.path}")
42+
private String baseUrl;
3343

34-
@Autowired
3544
private GraphQlTester graphQlTester;
3645

3746

47+
@BeforeEach
48+
void setUp() {
49+
URI url = URI.create(baseUrl);
50+
this.graphQlTester = WebSocketGraphQlTester.builder(url, new ReactorNettyWebSocketClient()).build();
51+
}
52+
53+
@Test
54+
void greetingMono() {
55+
this.graphQlTester.document("{greetingMono}")
56+
.execute()
57+
.path("greetingMono")
58+
.entity(String.class)
59+
.isEqualTo("Hello!");
60+
}
61+
62+
@Test
63+
void greetingsFlux() {
64+
this.graphQlTester.document("{greetingsFlux}")
65+
.execute()
66+
.path("greetingsFlux")
67+
.entityList(String.class)
68+
.containsExactly("Hi!", "Bonjour!", "Hola!", "Ciao!", "Zdravo!");
69+
}
70+
3871
@Test
3972
void subscriptionWithEntityPath() {
4073
Flux<String> result = this.graphQlTester.document("subscription { greetings }")

samples/webflux-websocket/src/test/java/io/spring/sample/graphql/WebFluxWebSocketSampleTests.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616
package io.spring.sample.graphql;
1717

1818
import org.junit.jupiter.api.Test;
19+
import reactor.core.publisher.Flux;
20+
import reactor.test.StepVerifier;
1921

2022
import org.springframework.beans.factory.annotation.Autowired;
2123
import org.springframework.boot.test.autoconfigure.graphql.GraphQlTest;
2224
import org.springframework.context.annotation.Import;
2325
import org.springframework.graphql.test.tester.GraphQlTester;
2426

2527
/**
26-
* GraphQL over WebSocket single response tests.
28+
* GraphQL over WebSocket, server-side tests, i.e. without a client.
2729
*/
2830
@GraphQlTest(SampleController.class)
2931
@Import(DataRepository.class)
@@ -51,4 +53,33 @@ void greetingsFlux() {
5153
.containsExactly("Hi!", "Bonjour!", "Hola!", "Ciao!", "Zdravo!");
5254
}
5355

56+
@Test
57+
void subscriptionWithEntityPath() {
58+
Flux<String> result = this.graphQlTester.document("subscription { greetings }")
59+
.executeSubscription()
60+
.toFlux("greetings", String.class);
61+
62+
StepVerifier.create(result)
63+
.expectNext("Hi!")
64+
.expectNext("Bonjour!")
65+
.expectNext("Hola!")
66+
.expectNext("Ciao!")
67+
.expectNext("Zdravo!")
68+
.verifyComplete();
69+
}
70+
71+
@Test
72+
void subscriptionWithResponse() {
73+
Flux<GraphQlTester.Response> result = this.graphQlTester.document("subscription { greetings }")
74+
.executeSubscription()
75+
.toFlux();
76+
77+
StepVerifier.create(result)
78+
.consumeNextWith(response -> response.path("greetings").hasValue())
79+
.consumeNextWith(response -> response.path("greetings").matchesJson("\"Bonjour!\""))
80+
.consumeNextWith(response -> response.path("greetings").matchesJson("\"Hola!\""))
81+
.expectNextCount(2)
82+
.verifyComplete();
83+
}
84+
5485
}

0 commit comments

Comments
 (0)