|
| 1 | +/* |
| 2 | + * Copyright 2020-2025 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.server; |
| 18 | + |
| 19 | + |
| 20 | +import java.net.URI; |
| 21 | +import java.time.Duration; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import graphql.ExecutionInput; |
| 25 | +import graphql.ExecutionResult; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import reactor.core.publisher.Mono; |
| 28 | +import reactor.test.StepVerifier; |
| 29 | + |
| 30 | +import org.springframework.graphql.ExecutionGraphQlResponse; |
| 31 | +import org.springframework.graphql.support.DefaultExecutionGraphQlResponse; |
| 32 | +import org.springframework.graphql.support.DefaultGraphQlRequest; |
| 33 | +import org.springframework.http.HttpHeaders; |
| 34 | +import org.springframework.http.HttpStatus; |
| 35 | +import org.springframework.web.server.ResponseStatusException; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | + |
| 39 | +/** |
| 40 | + * Tests for {@link TimeoutWebGraphQlInterceptor}. |
| 41 | + */ |
| 42 | +class TimeoutWebGraphQlInterceptorTests { |
| 43 | + |
| 44 | + @Test |
| 45 | + void shouldRespondWhenTimeoutNotExceeded() { |
| 46 | + TimeoutWebGraphQlInterceptor interceptor = new TimeoutWebGraphQlInterceptor(Duration.ofSeconds(3)); |
| 47 | + TestChain interceptorChain = new TestChain(Duration.ofMillis(200)); |
| 48 | + Mono<WebGraphQlResponse> response = interceptor.intercept(createRequest(), interceptorChain); |
| 49 | + |
| 50 | + StepVerifier.create(response).expectNextCount(1).expectComplete().verify(); |
| 51 | + assertThat(interceptorChain.cancelled).isFalse(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void shouldTimeoutWithDefaultStatus() { |
| 56 | + TimeoutWebGraphQlInterceptor interceptor = new TimeoutWebGraphQlInterceptor(Duration.ofMillis(200)); |
| 57 | + TestChain interceptorChain = new TestChain(Duration.ofSeconds(1)); |
| 58 | + Mono<WebGraphQlResponse> response = interceptor.intercept(createRequest(), interceptorChain); |
| 59 | + |
| 60 | + StepVerifier.create(response).expectErrorSatisfies(error -> { |
| 61 | + assertThat(error).isInstanceOf(ResponseStatusException.class); |
| 62 | + ResponseStatusException responseStatusException = (ResponseStatusException) error; |
| 63 | + assertThat(responseStatusException.getStatusCode()).isEqualTo(HttpStatus.REQUEST_TIMEOUT); |
| 64 | + }).verify(); |
| 65 | + assertThat(interceptorChain.cancelled).isTrue(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void shouldTimeoutWithCustomStatus() { |
| 70 | + TimeoutWebGraphQlInterceptor interceptor = new TimeoutWebGraphQlInterceptor(Duration.ofMillis(200), HttpStatus.GATEWAY_TIMEOUT); |
| 71 | + TestChain interceptorChain = new TestChain(Duration.ofSeconds(1)); |
| 72 | + Mono<WebGraphQlResponse> response = interceptor.intercept(createRequest(), interceptorChain); |
| 73 | + |
| 74 | + StepVerifier.create(response).expectErrorSatisfies(error -> { |
| 75 | + assertThat(error).isInstanceOf(ResponseStatusException.class); |
| 76 | + ResponseStatusException responseStatusException = (ResponseStatusException) error; |
| 77 | + assertThat(responseStatusException.getStatusCode()).isEqualTo(HttpStatus.GATEWAY_TIMEOUT); |
| 78 | + }).verify(); |
| 79 | + assertThat(interceptorChain.cancelled).isTrue(); |
| 80 | + } |
| 81 | + |
| 82 | + WebGraphQlRequest createRequest() { |
| 83 | + return new WebGraphQlRequest(URI.create("https://localhost/graphql"), new HttpHeaders(), |
| 84 | + null, null, Map.of(), new DefaultGraphQlRequest("{ greeting }"), "id", null); |
| 85 | + } |
| 86 | + |
| 87 | + class TestChain implements WebGraphQlInterceptor.Chain { |
| 88 | + |
| 89 | + private Duration delay; |
| 90 | + |
| 91 | + boolean cancelled; |
| 92 | + |
| 93 | + public TestChain(Duration delay) { |
| 94 | + this.delay = delay; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public Mono<WebGraphQlResponse> next(WebGraphQlRequest request) { |
| 99 | + ExecutionInput executionInput = ExecutionInput.newExecutionInput().query("{ greeting }").build(); |
| 100 | + ExecutionResult executionResult = ExecutionResult.newExecutionResult().data("Hello World").build(); |
| 101 | + ExecutionGraphQlResponse response = new DefaultExecutionGraphQlResponse(executionInput, executionResult); |
| 102 | + return Mono.just(new WebGraphQlResponse(response)) |
| 103 | + .delayElement(this.delay) |
| 104 | + .doOnCancel(() -> this.cancelled = true); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments