|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2022 the original author or authors. |
| 2 | + * Copyright 2002-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
20 | 20 | import java.time.Duration;
|
21 | 21 | import java.util.Collections;
|
22 | 22 | import java.util.Map;
|
| 23 | +import java.util.concurrent.ConcurrentHashMap; |
23 | 24 | import java.util.stream.Stream;
|
24 | 25 |
|
25 | 26 | import graphql.ExecutionResultImpl;
|
|
47 | 48 | import org.springframework.test.web.reactive.server.HttpHandlerConnector;
|
48 | 49 | import org.springframework.util.Assert;
|
49 | 50 | import org.springframework.util.MimeType;
|
| 51 | +import org.springframework.web.reactive.function.client.ClientRequest; |
| 52 | +import org.springframework.web.reactive.function.client.ClientResponse; |
| 53 | +import org.springframework.web.reactive.function.client.ExchangeFunction; |
50 | 54 | import org.springframework.web.reactive.function.client.WebClient;
|
51 | 55 | import org.springframework.web.reactive.function.server.HandlerStrategies;
|
52 | 56 | import org.springframework.web.reactive.function.server.RouterFunction;
|
@@ -227,6 +231,20 @@ void codecConfigurerRegistersJsonPathMappingProvider(ClientBuilderSetup builderS
|
227 | 231 | assertThat(testDecoder.getLastValue()).isEqualTo(character);
|
228 | 232 | }
|
229 | 233 |
|
| 234 | + @Test |
| 235 | + void attributes() { |
| 236 | + |
| 237 | + HttpBuilderSetup builderSetup = new HttpBuilderSetup(); |
| 238 | + |
| 239 | + builderSetup.initBuilder().url("/graphql-one").headers(headers -> headers.add("h", "one")).build() |
| 240 | + .document(DOCUMENT) |
| 241 | + .attribute("id", 123) |
| 242 | + .execute() |
| 243 | + .block(TIMEOUT); |
| 244 | + |
| 245 | + assertThat(builderSetup.getClientAttributes()).containsEntry("id", 123); |
| 246 | + } |
| 247 | + |
230 | 248 |
|
231 | 249 | private interface ClientBuilderSetup {
|
232 | 250 |
|
@@ -275,13 +293,26 @@ protected WebGraphQlHandler webGraphQlHandler() {
|
275 | 293 |
|
276 | 294 | private static class HttpBuilderSetup extends AbstractBuilderSetup {
|
277 | 295 |
|
| 296 | + private final Map<String, Object> clientAttributes = new ConcurrentHashMap<>(); |
| 297 | + |
| 298 | + public Map<String, Object> getClientAttributes() { |
| 299 | + return this.clientAttributes; |
| 300 | + } |
| 301 | + |
278 | 302 | @Override
|
279 | 303 | public HttpGraphQlClient.Builder<?> initBuilder() {
|
280 | 304 | GraphQlHttpHandler handler = new GraphQlHttpHandler(webGraphQlHandler());
|
281 | 305 | RouterFunction<ServerResponse> routerFunction = route().POST("/**", handler::handleRequest).build();
|
282 | 306 | HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction, HandlerStrategies.withDefaults());
|
283 | 307 | HttpHandlerConnector connector = new HttpHandlerConnector(httpHandler);
|
284 |
| - return HttpGraphQlClient.builder(WebClient.builder().clientConnector(connector)); |
| 308 | + return HttpGraphQlClient.builder(WebClient.builder() |
| 309 | + .clientConnector(connector).filter(this::updateAttributes)); |
| 310 | + } |
| 311 | + |
| 312 | + private Mono<ClientResponse> updateAttributes(ClientRequest request, ExchangeFunction next) { |
| 313 | + this.clientAttributes.clear(); |
| 314 | + this.clientAttributes.putAll(request.attributes()); |
| 315 | + return next.exchange(request); |
285 | 316 | }
|
286 | 317 |
|
287 | 318 | }
|
|
0 commit comments