Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit f63531a

Browse files
fix: support error messages in subscription tests
1 parent bc3c878 commit f63531a

File tree

6 files changed

+40
-7
lines changed

6 files changed

+40
-7
lines changed

graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/GraphQLTestSubscription.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,15 +382,18 @@ public void onMessage(final String message) {
382382
try {
383383
log.debug("Received message from web socket: {}", message);
384384
final JsonNode jsonNode = objectMapper.readTree(message);
385-
final JsonNode type = jsonNode.get("type");
386-
if (type.asText().equals("connection_ack")) {
385+
final JsonNode typeNode = jsonNode.get("type");
386+
assertThat(typeNode.isNull()).as("GraphQL messages should have a type field.").isFalse();
387+
final String type = typeNode.asText();
388+
if (type.equals("connection_ack")) {
387389
acknowledged = true;
388390
log.debug("WebSocket connection acknowledged by the GraphQL Server.");
389-
} else if (type.asText().equals("data")) {
390-
final JsonNode data = jsonNode.get("payload");
391-
assertThat(data).as("Data message must have a payload.").isNotNull();
392-
final GraphQLResponse graphQLResponse
393-
= new GraphQLResponse(ResponseEntity.ok(objectMapper.writeValueAsString(data)), objectMapper);
391+
} else if (type.equals("data") || type.equals("error")) {
392+
final JsonNode payload = jsonNode.get("payload");
393+
assertThat(payload).as("Data/error messages must have a payload.").isNotNull();
394+
final String payloadString = objectMapper.writeValueAsString(payload);
395+
final GraphQLResponse graphQLResponse = new GraphQLResponse(ResponseEntity.ok(payloadString),
396+
objectMapper);
394397
synchronized (responses) {
395398
responses.add(graphQLResponse);
396399
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.graphql.spring.boot.test;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
@DisplayName("Test error message handling.")
9+
public class GraphQLTestSubscriptionErrorTest extends GraphQLTestSubscriptionTestBase {
10+
11+
@Test
12+
@DisplayName("Should handle error messages.")
13+
void shouldHandleErrorMessages() {
14+
// WHEN
15+
final GraphQLResponse graphQLResponse = graphQLTestSubscription.start(SUBSCRIPTION_THAT_THROWS_EXCEPTION)
16+
.awaitAndGetNextResponse(TIMEOUT);
17+
// THEN
18+
assertThat(graphQLResponse.get("$.errors[0].message")).isNotBlank();
19+
}
20+
}

graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/GraphQLTestSubscriptionTestBase.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class GraphQLTestSubscriptionTestBase {
1717
= "subscription-with-init-payload-resource.graphql";
1818
protected static final String SUBSCRIPTION_THAT_TIMES_OUT_RESOURCE
1919
= "subscription-that-times-out-resource.graphql";
20+
protected static final String SUBSCRIPTION_THAT_THROWS_EXCEPTION
21+
= "subscription-that-throws-exception-resource.graphql";
2022
protected static final String DATA_TIMER_FIELD = "$.data.timer";
2123
protected static final String DATA_SUBSCRIPTION_WITH_PARAMETER_FIELD = "$.data.subscriptionWithParameter";
2224
protected static final String DATA_SUBSCRIPTION_WITH_INIT_PAYLOAD_FIELD = "$.data.subscriptionWithInitPayload";

graphql-spring-boot-test/src/test/java/com/graphql/spring/boot/test/beans/TestSubscriptions.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ public Flowable<String> subscriptionWithInitPayload() {
2929
public Flowable<Long> subscriptionThatTimesOut() {
3030
return Flowable.interval(20000, 20000, TimeUnit.MILLISECONDS);
3131
}
32+
33+
public Flowable<Boolean> subscriptionThatThrowsException() {
34+
throw new RuntimeException("Test exception.");
35+
}
3236
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
subscription {
2+
subscriptionThatThrowsException
3+
}

graphql-spring-boot-test/src/test/resources/test-schema.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type Subscription {
55
subscriptionWithParameter(param: String!): String!
66
subscriptionWithInitPayload: String!
77
subscriptionThatTimesOut: Long!
8+
subscriptionThatThrowsException: Boolean!
89
}
910

1011
type Query {

0 commit comments

Comments
 (0)