|
| 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; |
| 18 | + |
| 19 | +import org.jspecify.annotations.Nullable; |
| 20 | + |
| 21 | +import org.springframework.aot.AotDetector; |
| 22 | +import org.springframework.beans.BeansException; |
| 23 | +import org.springframework.beans.factory.BeanFactory; |
| 24 | +import org.springframework.beans.factory.BeanFactoryAware; |
| 25 | +import org.springframework.beans.factory.BeanFactoryUtils; |
| 26 | +import org.springframework.beans.factory.FactoryBean; |
| 27 | +import org.springframework.beans.factory.ListableBeanFactory; |
| 28 | +import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| 29 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 30 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 31 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 32 | +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; |
| 33 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
| 34 | +import org.springframework.boot.restclient.RootUriBuilderFactory; |
| 35 | +import org.springframework.boot.test.context.SpringBootTest; |
| 36 | +import org.springframework.boot.web.server.reactive.AbstractReactiveWebServerFactory; |
| 37 | +import org.springframework.context.ApplicationContext; |
| 38 | +import org.springframework.context.ApplicationContextAware; |
| 39 | +import org.springframework.context.ConfigurableApplicationContext; |
| 40 | +import org.springframework.context.annotation.ConfigurationClassPostProcessor; |
| 41 | +import org.springframework.core.Ordered; |
| 42 | +import org.springframework.test.context.ContextCustomizer; |
| 43 | +import org.springframework.test.context.MergedContextConfiguration; |
| 44 | +import org.springframework.test.context.TestContextAnnotationUtils; |
| 45 | +import org.springframework.test.web.servlet.client.RestTestClient; |
| 46 | +import org.springframework.util.Assert; |
| 47 | + |
| 48 | +/** |
| 49 | + * {@link ContextCustomizer} for {@link RestTestClient}. |
| 50 | + * |
| 51 | + * @author Stephane Nicoll |
| 52 | + */ |
| 53 | +class RestTestClientContextCustomizer implements ContextCustomizer { |
| 54 | + |
| 55 | + @Override |
| 56 | + public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) { |
| 57 | + if (AotDetector.useGeneratedArtifacts()) { |
| 58 | + return; |
| 59 | + } |
| 60 | + SpringBootTest springBootTest = TestContextAnnotationUtils.findMergedAnnotation(mergedConfig.getTestClass(), |
| 61 | + SpringBootTest.class); |
| 62 | + Assert.state(springBootTest != null, "'springBootTest' must not be null"); |
| 63 | + if (springBootTest.webEnvironment().isEmbedded()) { |
| 64 | + registerRestTestClient(context); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private void registerRestTestClient(ConfigurableApplicationContext context) { |
| 69 | + ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); |
| 70 | + if (beanFactory instanceof BeanDefinitionRegistry registry) { |
| 71 | + registerRestTestClient(registry); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private void registerRestTestClient(BeanDefinitionRegistry registry) { |
| 76 | + RootBeanDefinition definition = new RootBeanDefinition(RestTestClientRegistrar.class); |
| 77 | + definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); |
| 78 | + registry.registerBeanDefinition(RestTestClientRegistrar.class.getName(), definition); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public boolean equals(@Nullable Object obj) { |
| 83 | + return (obj != null) && (obj.getClass() == getClass()); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public int hashCode() { |
| 88 | + return getClass().hashCode(); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * {@link BeanDefinitionRegistryPostProcessor} that runs after the |
| 93 | + * {@link ConfigurationClassPostProcessor} and add a {@link RestTestClientFactory} |
| 94 | + * bean definition when a {@link RestTestClient} hasn't already been registered. |
| 95 | + */ |
| 96 | + static class RestTestClientRegistrar implements BeanDefinitionRegistryPostProcessor, Ordered, BeanFactoryAware { |
| 97 | + |
| 98 | + @SuppressWarnings("NullAway.Init") |
| 99 | + private BeanFactory beanFactory; |
| 100 | + |
| 101 | + @Override |
| 102 | + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { |
| 103 | + this.beanFactory = beanFactory; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public int getOrder() { |
| 108 | + return Ordered.LOWEST_PRECEDENCE; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { |
| 113 | + if (AotDetector.useGeneratedArtifacts()) { |
| 114 | + return; |
| 115 | + } |
| 116 | + if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors((ListableBeanFactory) this.beanFactory, |
| 117 | + RestTestClient.class, false, false).length == 0) { |
| 118 | + registry.registerBeanDefinition(RestTestClient.class.getName(), |
| 119 | + new RootBeanDefinition(RestTestClientFactory.class)); |
| 120 | + } |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { |
| 126 | + } |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * {@link FactoryBean} used to create and configure a {@link RestTestClient}. |
| 132 | + */ |
| 133 | + public static class RestTestClientFactory implements FactoryBean<RestTestClient>, ApplicationContextAware { |
| 134 | + |
| 135 | + @SuppressWarnings("NullAway.Init") |
| 136 | + private ApplicationContext applicationContext; |
| 137 | + |
| 138 | + private @Nullable RestTestClient object; |
| 139 | + |
| 140 | + @Override |
| 141 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 142 | + this.applicationContext = applicationContext; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public boolean isSingleton() { |
| 147 | + return true; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public Class<?> getObjectType() { |
| 152 | + return RestTestClient.class; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public RestTestClient getObject() { |
| 157 | + if (this.object == null) { |
| 158 | + this.object = createRestTestClient(); |
| 159 | + } |
| 160 | + return this.object; |
| 161 | + } |
| 162 | + |
| 163 | + private RestTestClient createRestTestClient() { |
| 164 | + boolean sslEnabled = isSslEnabled(this.applicationContext); |
| 165 | + LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler( |
| 166 | + this.applicationContext.getEnvironment(), sslEnabled ? "https" : "http"); |
| 167 | + RestTestClient.Builder<?> builder = RestTestClient.bindToServer(); |
| 168 | + customizeRestTestClientBuilder(builder, this.applicationContext); |
| 169 | + return builder.uriBuilderFactory(new RootUriBuilderFactory(handler.getRootUri(), handler)).build(); |
| 170 | + } |
| 171 | + |
| 172 | + private boolean isSslEnabled(ApplicationContext context) { |
| 173 | + try { |
| 174 | + AbstractReactiveWebServerFactory webServerFactory = context |
| 175 | + .getBean(AbstractReactiveWebServerFactory.class); |
| 176 | + return webServerFactory.getSsl() != null && webServerFactory.getSsl().isEnabled(); |
| 177 | + } |
| 178 | + catch (NoSuchBeanDefinitionException ex) { |
| 179 | + return false; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private void customizeRestTestClientBuilder(RestTestClient.Builder<?> clientBuilder, |
| 184 | + ApplicationContext context) { |
| 185 | + for (RestTestClientBuilderCustomizer customizer : context |
| 186 | + .getBeansOfType(RestTestClientBuilderCustomizer.class) |
| 187 | + .values()) { |
| 188 | + customizer.customize(clientBuilder); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + } |
| 193 | + |
| 194 | +} |
0 commit comments