|
| 1 | +/* |
| 2 | + * Copyright 2012-present 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.boot.web.server.test.client.reactive; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Nested; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import reactor.core.publisher.Mono; |
| 25 | + |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | +import org.springframework.boot.SpringBootConfiguration; |
| 28 | +import org.springframework.boot.test.context.SpringBootTest; |
| 29 | +import org.springframework.boot.tomcat.reactive.TomcatReactiveWebServerFactory; |
| 30 | +import org.springframework.context.annotation.Bean; |
| 31 | +import org.springframework.context.annotation.Import; |
| 32 | +import org.springframework.core.io.buffer.DefaultDataBufferFactory; |
| 33 | +import org.springframework.http.HttpStatus; |
| 34 | +import org.springframework.http.server.reactive.ContextPathCompositeHandler; |
| 35 | +import org.springframework.http.server.reactive.HttpHandler; |
| 36 | +import org.springframework.http.server.reactive.ServerHttpRequest; |
| 37 | +import org.springframework.http.server.reactive.ServerHttpResponse; |
| 38 | +import org.springframework.test.context.TestPropertySource; |
| 39 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 40 | + |
| 41 | +/** |
| 42 | + * Tests for {@link WebTestClientContextCustomizer} when the test context framework pauses |
| 43 | + * a context while it's in the cache. |
| 44 | + * |
| 45 | + * @author Andy Wilkinson |
| 46 | + */ |
| 47 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, |
| 48 | + properties = "spring.main.web-application-type=none") |
| 49 | +class WebTestClientContextCustomizerTcfCacheContextPausingTests { |
| 50 | + |
| 51 | + @Nested |
| 52 | + @Import(TestConfig.class) |
| 53 | + @TestPropertySource(properties = { "context=one", "spring.main.web-application-type=reactive" }) |
| 54 | + class ContextOne { |
| 55 | + |
| 56 | + @Autowired |
| 57 | + private WebTestClient webTestClient; |
| 58 | + |
| 59 | + @Test |
| 60 | + void test() { |
| 61 | + this.webTestClient.get().uri("/test").exchange().expectBody(String.class).isEqualTo("hello world"); |
| 62 | + } |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + @Nested |
| 67 | + @Import(TestConfig.class) |
| 68 | + @TestPropertySource(properties = { "context=two", "spring.main.web-application-type=reactive" }) |
| 69 | + class ContextTwo { |
| 70 | + |
| 71 | + @Autowired |
| 72 | + private WebTestClient webTestClient; |
| 73 | + |
| 74 | + @Test |
| 75 | + void test() { |
| 76 | + this.webTestClient.get().uri("/test").exchange().expectBody(String.class).isEqualTo("hello world"); |
| 77 | + } |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + @Nested |
| 82 | + @Import(TestConfig.class) |
| 83 | + @TestPropertySource(properties = { "context=one", "spring.main.web-application-type=reactive" }) |
| 84 | + class ReuseContextOne { |
| 85 | + |
| 86 | + @Autowired |
| 87 | + private WebTestClient webTestClient; |
| 88 | + |
| 89 | + @Test |
| 90 | + void test() { |
| 91 | + this.webTestClient.get().uri("/test").exchange().expectBody(String.class).isEqualTo("hello world"); |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + @SpringBootConfiguration(proxyBeanMethods = false) |
| 97 | + static class TestConfig { |
| 98 | + |
| 99 | + @Bean |
| 100 | + TomcatReactiveWebServerFactory webServerFactory() { |
| 101 | + return new TomcatReactiveWebServerFactory(0); |
| 102 | + } |
| 103 | + |
| 104 | + @Bean |
| 105 | + HttpHandler httpHandler() { |
| 106 | + TestHandler httpHandler = new TestHandler(); |
| 107 | + Map<String, HttpHandler> handlersMap = Collections.singletonMap("/test", httpHandler); |
| 108 | + return new ContextPathCompositeHandler(handlersMap); |
| 109 | + } |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + static class TestHandler implements HttpHandler { |
| 114 | + |
| 115 | + private static final DefaultDataBufferFactory factory = new DefaultDataBufferFactory(); |
| 116 | + |
| 117 | + @Override |
| 118 | + public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) { |
| 119 | + response.setStatusCode(HttpStatus.OK); |
| 120 | + return response.writeWith(Mono.just(factory.wrap("hello world".getBytes()))); |
| 121 | + } |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments