Skip to content

Commit c955603

Browse files
committed
Configure interceptors on WebSocketGraphQlTester
This commit adds two new methods on the `WebSocketGraphQlTester` builder to configure `GraphQlClientInterceptor` instances on the client. For now, this is only accessible on the WebSocket tester but could be promoted to a higher level builder in the future. Closes gh-823
1 parent fbb08c4 commit c955603

File tree

3 files changed

+116
-2
lines changed

3 files changed

+116
-2
lines changed

spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultWebSocketGraphQlTesterBuilder.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,10 +18,14 @@
1818

1919

2020
import java.net.URI;
21+
import java.util.ArrayList;
22+
import java.util.Arrays;
23+
import java.util.List;
2124
import java.util.function.Consumer;
2225

2326
import reactor.core.publisher.Mono;
2427

28+
import org.springframework.graphql.client.GraphQlClientInterceptor;
2529
import org.springframework.graphql.client.WebSocketGraphQlClient;
2630
import org.springframework.http.HttpHeaders;
2731
import org.springframework.http.codec.CodecConfigurer;
@@ -41,6 +45,8 @@ final class DefaultWebSocketGraphQlTesterBuilder
4145

4246
private final WebSocketGraphQlClient.Builder<?> graphQlClientBuilder;
4347

48+
private final List<GraphQlClientInterceptor> interceptors = new ArrayList<>();
49+
4450

4551
/**
4652
* Constructor to start via {@link WebSocketGraphQlTester#builder(String, WebSocketClient)}.
@@ -98,10 +104,22 @@ public DefaultWebSocketGraphQlTesterBuilder codecConfigurer(Consumer<CodecConfig
98104
return this;
99105
}
100106

107+
@Override
108+
public DefaultWebSocketGraphQlTesterBuilder interceptor(GraphQlClientInterceptor... interceptors) {
109+
this.interceptors.addAll(Arrays.asList(interceptors));
110+
return this;
111+
}
112+
113+
@Override
114+
public DefaultWebSocketGraphQlTesterBuilder interceptors(Consumer<List<GraphQlClientInterceptor>> interceptorsConsumer) {
115+
interceptorsConsumer.accept(this.interceptors);
116+
return this;
117+
}
118+
101119
@Override
102120
public WebSocketGraphQlTester build() {
103121
registerJsonPathMappingProvider();
104-
WebSocketGraphQlClient client = this.graphQlClientBuilder.build();
122+
WebSocketGraphQlClient client = this.graphQlClientBuilder.interceptors((list) -> list.addAll(this.interceptors)).build();
105123
GraphQlTester graphQlTester = super.buildGraphQlTester(asTransport(client));
106124
return new DefaultWebSocketGraphQlTester(graphQlTester, client, getBuilderInitializer());
107125
}

spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/WebSocketGraphQlTester.java

+20
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717
package org.springframework.graphql.test.tester;
1818

1919
import java.net.URI;
20+
import java.util.List;
21+
import java.util.function.Consumer;
2022

2123
import reactor.core.publisher.Mono;
2224

25+
import org.springframework.graphql.client.GraphQlClientInterceptor;
26+
import org.springframework.graphql.client.GraphQlTransport;
2327
import org.springframework.graphql.client.WebSocketGraphQlClient;
2428
import org.springframework.web.reactive.socket.client.WebSocketClient;
2529

@@ -81,6 +85,22 @@ static WebSocketGraphQlTester.Builder<?> builder(URI url, WebSocketClient webSoc
8185
*/
8286
interface Builder<B extends Builder<B>> extends WebGraphQlTester.Builder<B> {
8387

88+
/**
89+
* Configure interceptors to be invoked before delegating to the
90+
* {@link GraphQlTransport} to perform the request.
91+
* @param interceptors the interceptors to add
92+
* @return this builder
93+
*/
94+
B interceptor(GraphQlClientInterceptor... interceptors);
95+
96+
/**
97+
* Customize the list of interceptors. The provided list is "live", so
98+
* the consumer can inspect and insert interceptors accordingly.
99+
* @param interceptorsConsumer consumer to customize the interceptors with
100+
* @return this builder
101+
*/
102+
B interceptors(Consumer<List<GraphQlClientInterceptor>> interceptorsConsumer);
103+
84104
/**
85105
* Build the {@code WebSocketGraphQlTester}.
86106
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2020-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.graphql.test.tester;
18+
19+
import java.net.URI;
20+
import java.time.Duration;
21+
import java.util.concurrent.atomic.AtomicBoolean;
22+
23+
import org.junit.jupiter.api.Test;
24+
import reactor.core.publisher.Mono;
25+
26+
import org.springframework.graphql.client.ClientGraphQlRequest;
27+
import org.springframework.graphql.client.ClientGraphQlResponse;
28+
import org.springframework.graphql.client.GraphQlClientInterceptor;
29+
import org.springframework.graphql.client.TestWebSocketClient;
30+
import org.springframework.graphql.client.WebSocketGraphQlClient;
31+
import org.springframework.graphql.execution.MockExecutionGraphQlService;
32+
import org.springframework.graphql.server.WebGraphQlHandler;
33+
import org.springframework.graphql.server.webflux.GraphQlWebSocketHandler;
34+
import org.springframework.http.codec.ClientCodecConfigurer;
35+
import org.springframework.web.reactive.socket.WebSocketHandler;
36+
37+
import static org.assertj.core.api.Assertions.assertThat;
38+
39+
/**
40+
* Tests for {@link WebSocketGraphQlTester}.
41+
*/
42+
class WebSocketGraphQlTesterTests {
43+
44+
45+
@Test
46+
void shouldConfigureInterceptors() {
47+
TestInterceptor testInterceptor = new TestInterceptor();
48+
TestWebSocketClient webSocketClient = createWebSocketClient();
49+
WebSocketGraphQlClient client = WebSocketGraphQlClient.builder(URI.create(""), webSocketClient)
50+
.interceptor(testInterceptor)
51+
.build();
52+
53+
client.document("{ Query }").execute().block(Duration.ofMillis(500));
54+
assertThat(testInterceptor.executed).as("Interceptor is not executed").isTrue();
55+
}
56+
57+
private TestWebSocketClient createWebSocketClient() {
58+
MockExecutionGraphQlService graphQlService = new MockExecutionGraphQlService();
59+
graphQlService.setDefaultResponse("{}");
60+
WebGraphQlHandler graphQlHandler = WebGraphQlHandler.builder(graphQlService).build();
61+
ClientCodecConfigurer configurer = ClientCodecConfigurer.create();
62+
WebSocketHandler handler = new GraphQlWebSocketHandler(graphQlHandler, configurer, Duration.ofSeconds(5));
63+
return new TestWebSocketClient(handler);
64+
}
65+
66+
class TestInterceptor implements GraphQlClientInterceptor {
67+
68+
AtomicBoolean executed = new AtomicBoolean();
69+
70+
@Override
71+
public Mono<ClientGraphQlResponse> intercept(ClientGraphQlRequest request, Chain chain) {
72+
return chain.next(request).doOnNext((response) -> executed.set(true));
73+
}
74+
}
75+
76+
}

0 commit comments

Comments
 (0)