|
| 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