Extract RestDocsProperties
Extract properties used by RestDocs to their own class.
This commit is contained in:
parent
d439b73758
commit
b9bb31cfd4
|
@ -25,7 +25,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
@ -47,13 +46,13 @@ import org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation
|
||||||
* @since 1.4.0
|
* @since 1.4.0
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableConfigurationProperties
|
|
||||||
@ConditionalOnWebApplication
|
@ConditionalOnWebApplication
|
||||||
public class RestDocsAutoConfiguration {
|
public class RestDocsAutoConfiguration {
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass(MockMvcRestDocumentation.class)
|
@ConditionalOnClass(MockMvcRestDocumentation.class)
|
||||||
@ConditionalOnWebApplication(type = Type.SERVLET)
|
@ConditionalOnWebApplication(type = Type.SERVLET)
|
||||||
|
@EnableConfigurationProperties(RestDocsProperties.class)
|
||||||
static class RestDocsMockMvcAutoConfiguration {
|
static class RestDocsMockMvcAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -72,11 +71,11 @@ public class RestDocsAutoConfiguration {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConfigurationProperties(prefix = "spring.test.restdocs")
|
|
||||||
public RestDocsMockMvcBuilderCustomizer restDocumentationConfigurer(
|
public RestDocsMockMvcBuilderCustomizer restDocumentationConfigurer(
|
||||||
|
RestDocsProperties properties,
|
||||||
MockMvcRestDocumentationConfigurer configurer,
|
MockMvcRestDocumentationConfigurer configurer,
|
||||||
ObjectProvider<RestDocumentationResultHandler> resultHandler) {
|
ObjectProvider<RestDocumentationResultHandler> resultHandler) {
|
||||||
return new RestDocsMockMvcBuilderCustomizer(configurer,
|
return new RestDocsMockMvcBuilderCustomizer(properties, configurer,
|
||||||
resultHandler.getIfAvailable());
|
resultHandler.getIfAvailable());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,6 +84,7 @@ public class RestDocsAutoConfiguration {
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass({ RequestSpecification.class,
|
@ConditionalOnClass({ RequestSpecification.class,
|
||||||
RestAssuredRestDocumentation.class })
|
RestAssuredRestDocumentation.class })
|
||||||
|
@EnableConfigurationProperties(RestDocsProperties.class)
|
||||||
static class RestDocsRestAssuredAutoConfiguration {
|
static class RestDocsRestAssuredAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -103,10 +103,9 @@ public class RestDocsAutoConfiguration {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConfigurationProperties(prefix = "spring.test.restdocs")
|
|
||||||
public RestDocsRestAssuredBuilderCustomizer restAssuredBuilderCustomizer(
|
public RestDocsRestAssuredBuilderCustomizer restAssuredBuilderCustomizer(
|
||||||
RequestSpecification configurer) {
|
RestDocsProperties properties, RequestSpecification configurer) {
|
||||||
return new RestDocsRestAssuredBuilderCustomizer(configurer);
|
return new RestDocsRestAssuredBuilderCustomizer(properties, configurer);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -114,6 +113,7 @@ public class RestDocsAutoConfiguration {
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass(WebTestClientRestDocumentation.class)
|
@ConditionalOnClass(WebTestClientRestDocumentation.class)
|
||||||
@ConditionalOnWebApplication(type = Type.REACTIVE)
|
@ConditionalOnWebApplication(type = Type.REACTIVE)
|
||||||
|
@EnableConfigurationProperties(RestDocsProperties.class)
|
||||||
static class RestDocsWebTestClientAutoConfiguration {
|
static class RestDocsWebTestClientAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -132,10 +132,10 @@ public class RestDocsAutoConfiguration {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConfigurationProperties(prefix = "spring.test.restdocs")
|
|
||||||
public RestDocsWebTestClientBuilderCustomizer restDocumentationConfigurer(
|
public RestDocsWebTestClientBuilderCustomizer restDocumentationConfigurer(
|
||||||
|
RestDocsProperties properties,
|
||||||
WebTestClientRestDocumentationConfigurer configurer) {
|
WebTestClientRestDocumentationConfigurer configurer) {
|
||||||
return new RestDocsWebTestClientBuilderCustomizer(configurer);
|
return new RestDocsWebTestClientBuilderCustomizer(properties, configurer);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,11 +17,12 @@
|
||||||
package org.springframework.boot.test.autoconfigure.restdocs;
|
package org.springframework.boot.test.autoconfigure.restdocs;
|
||||||
|
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.boot.context.properties.PropertyMapper;
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.MockMvcBuilderCustomizer;
|
import org.springframework.boot.test.autoconfigure.web.servlet.MockMvcBuilderCustomizer;
|
||||||
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentationConfigurer;
|
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentationConfigurer;
|
||||||
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
|
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
|
||||||
|
import org.springframework.restdocs.mockmvc.UriConfigurer;
|
||||||
import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder;
|
import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder;
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link MockMvcBuilderCustomizer} that configures Spring REST Docs.
|
* A {@link MockMvcBuilderCustomizer} that configures Spring REST Docs.
|
||||||
|
@ -31,57 +32,28 @@ import org.springframework.util.StringUtils;
|
||||||
class RestDocsMockMvcBuilderCustomizer
|
class RestDocsMockMvcBuilderCustomizer
|
||||||
implements InitializingBean, MockMvcBuilderCustomizer {
|
implements InitializingBean, MockMvcBuilderCustomizer {
|
||||||
|
|
||||||
|
private final RestDocsProperties properties;
|
||||||
|
|
||||||
private final MockMvcRestDocumentationConfigurer delegate;
|
private final MockMvcRestDocumentationConfigurer delegate;
|
||||||
|
|
||||||
private final RestDocumentationResultHandler resultHandler;
|
private final RestDocumentationResultHandler resultHandler;
|
||||||
|
|
||||||
private String uriScheme;
|
RestDocsMockMvcBuilderCustomizer(RestDocsProperties properties,
|
||||||
|
MockMvcRestDocumentationConfigurer delegate,
|
||||||
private String uriHost;
|
|
||||||
|
|
||||||
private Integer uriPort;
|
|
||||||
|
|
||||||
RestDocsMockMvcBuilderCustomizer(MockMvcRestDocumentationConfigurer delegate,
|
|
||||||
RestDocumentationResultHandler resultHandler) {
|
RestDocumentationResultHandler resultHandler) {
|
||||||
|
this.properties = properties;
|
||||||
this.delegate = delegate;
|
this.delegate = delegate;
|
||||||
this.resultHandler = resultHandler;
|
this.resultHandler = resultHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUriScheme() {
|
|
||||||
return this.uriScheme;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUriScheme(String uriScheme) {
|
|
||||||
this.uriScheme = uriScheme;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUriHost() {
|
|
||||||
return this.uriHost;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUriHost(String uriHost) {
|
|
||||||
this.uriHost = uriHost;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getUriPort() {
|
|
||||||
return this.uriPort;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUriPort(Integer uriPort) {
|
|
||||||
this.uriPort = uriPort;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() throws Exception {
|
public void afterPropertiesSet() throws Exception {
|
||||||
if (StringUtils.hasText(this.uriScheme)) {
|
PropertyMapper map = PropertyMapper.get();
|
||||||
this.delegate.uris().withScheme(this.uriScheme);
|
RestDocsProperties properties = this.properties;
|
||||||
}
|
UriConfigurer uri = this.delegate.uris();
|
||||||
if (StringUtils.hasText(this.uriHost)) {
|
map.from(properties::getUriScheme).whenHasText().to(uri::withScheme);
|
||||||
this.delegate.uris().withHost(this.uriHost);
|
map.from(properties::getUriHost).whenHasText().to(uri::withHost);
|
||||||
}
|
map.from(properties::getUriPort).whenNonNull().to(uri::withPort);
|
||||||
if (this.uriPort != null) {
|
|
||||||
this.delegate.uris().withPort(this.uriPort);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.test.autoconfigure.restdocs;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration properties for Spring REST Docs.
|
||||||
|
*
|
||||||
|
* @author Andy Wilkinson
|
||||||
|
* @author Eddú Meléndez
|
||||||
|
* @author Phillip Webb
|
||||||
|
* @since 2.0.0
|
||||||
|
*/
|
||||||
|
@ConfigurationProperties("spring.test.restdocs")
|
||||||
|
public class RestDocsProperties {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The URI scheme for to use (for example http).
|
||||||
|
*/
|
||||||
|
private String uriScheme;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The URI host to use.
|
||||||
|
*/
|
||||||
|
private String uriHost;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The URI port to use.
|
||||||
|
*/
|
||||||
|
private Integer uriPort;
|
||||||
|
|
||||||
|
public String getUriScheme() {
|
||||||
|
return this.uriScheme;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUriScheme(String uriScheme) {
|
||||||
|
this.uriScheme = uriScheme;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUriHost() {
|
||||||
|
return this.uriHost;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUriHost(String uriHost) {
|
||||||
|
this.uriHost = uriHost;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getUriPort() {
|
||||||
|
return this.uriPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUriPort(Integer uriPort) {
|
||||||
|
this.uriPort = uriPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -19,6 +19,7 @@ package org.springframework.boot.test.autoconfigure.restdocs;
|
||||||
import io.restassured.specification.RequestSpecification;
|
import io.restassured.specification.RequestSpecification;
|
||||||
|
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.boot.context.properties.PropertyMapper;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -28,50 +29,25 @@ import org.springframework.util.StringUtils;
|
||||||
*/
|
*/
|
||||||
class RestDocsRestAssuredBuilderCustomizer implements InitializingBean {
|
class RestDocsRestAssuredBuilderCustomizer implements InitializingBean {
|
||||||
|
|
||||||
|
private final RestDocsProperties properties;
|
||||||
|
|
||||||
private final RequestSpecification delegate;
|
private final RequestSpecification delegate;
|
||||||
|
|
||||||
private String uriScheme;
|
RestDocsRestAssuredBuilderCustomizer(RestDocsProperties properties,
|
||||||
|
RequestSpecification delegate) {
|
||||||
private String uriHost;
|
this.properties = properties;
|
||||||
|
|
||||||
private Integer uriPort;
|
|
||||||
|
|
||||||
RestDocsRestAssuredBuilderCustomizer(RequestSpecification delegate) {
|
|
||||||
this.delegate = delegate;
|
this.delegate = delegate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUriScheme() {
|
|
||||||
return this.uriScheme;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUriScheme(String uriScheme) {
|
|
||||||
this.uriScheme = uriScheme;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUriHost() {
|
|
||||||
return this.uriHost;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUriHost(String uriHost) {
|
|
||||||
this.uriHost = uriHost;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getUriPort() {
|
|
||||||
return this.uriPort;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUriPort(Integer uriPort) {
|
|
||||||
this.uriPort = uriPort;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() throws Exception {
|
public void afterPropertiesSet() throws Exception {
|
||||||
if (StringUtils.hasText(this.uriScheme) && StringUtils.hasText(this.uriHost)) {
|
PropertyMapper map = PropertyMapper.get();
|
||||||
this.delegate.baseUri(this.uriScheme + "://" + this.uriHost);
|
String host = this.properties.getUriHost();
|
||||||
}
|
map.from(this.properties::getUriScheme)
|
||||||
if (this.uriPort != null) {
|
.when((scheme) -> StringUtils.hasText(scheme)
|
||||||
this.delegate.port(this.uriPort);
|
&& StringUtils.hasText(host))
|
||||||
}
|
.to((scheme) -> this.delegate.baseUri(scheme + "://" + host));
|
||||||
|
map.from(this.properties::getUriPort).whenNonNull().to(this.delegate::port);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,43 +29,16 @@ import org.springframework.util.StringUtils;
|
||||||
*/
|
*/
|
||||||
class RestDocsWebTestClientBuilderCustomizer implements WebTestClientBuilderCustomizer {
|
class RestDocsWebTestClientBuilderCustomizer implements WebTestClientBuilderCustomizer {
|
||||||
|
|
||||||
|
private final RestDocsProperties properties;
|
||||||
|
|
||||||
private final WebTestClientRestDocumentationConfigurer delegate;
|
private final WebTestClientRestDocumentationConfigurer delegate;
|
||||||
|
|
||||||
private String uriScheme;
|
RestDocsWebTestClientBuilderCustomizer(RestDocsProperties properties,
|
||||||
|
|
||||||
private String uriHost;
|
|
||||||
|
|
||||||
private Integer uriPort;
|
|
||||||
|
|
||||||
RestDocsWebTestClientBuilderCustomizer(
|
|
||||||
WebTestClientRestDocumentationConfigurer delegate) {
|
WebTestClientRestDocumentationConfigurer delegate) {
|
||||||
|
this.properties = properties;
|
||||||
this.delegate = delegate;
|
this.delegate = delegate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUriScheme() {
|
|
||||||
return this.uriScheme;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUriScheme(String uriScheme) {
|
|
||||||
this.uriScheme = uriScheme;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUriHost() {
|
|
||||||
return this.uriHost;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUriHost(String uriHost) {
|
|
||||||
this.uriHost = uriHost;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getUriPort() {
|
|
||||||
return this.uriPort;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUriPort(Integer uriPort) {
|
|
||||||
this.uriPort = uriPort;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void customize(WebTestClient.Builder builder) {
|
public void customize(WebTestClient.Builder builder) {
|
||||||
customizeBaseUrl(builder);
|
customizeBaseUrl(builder);
|
||||||
|
@ -73,21 +46,23 @@ class RestDocsWebTestClientBuilderCustomizer implements WebTestClientBuilderCust
|
||||||
}
|
}
|
||||||
|
|
||||||
private void customizeBaseUrl(WebTestClient.Builder builder) {
|
private void customizeBaseUrl(WebTestClient.Builder builder) {
|
||||||
String scheme = StringUtils.hasText(this.uriScheme) ? this.uriScheme : "http";
|
String scheme = this.properties.getUriScheme();
|
||||||
String host = StringUtils.hasText(this.uriHost) ? this.uriHost : "localhost";
|
String host = this.properties.getUriHost();
|
||||||
String baseUrl = scheme + "://" + host;
|
String baseUrl = (StringUtils.hasText(scheme) ? scheme : "http") + "://"
|
||||||
if (!isStandardPort()) {
|
+ (StringUtils.hasText(host) ? host : "localhost");
|
||||||
baseUrl += ":" + this.uriPort;
|
Integer port = this.properties.getUriPort();
|
||||||
|
if (!isStandardPort(scheme, port)) {
|
||||||
|
baseUrl += ":" + port;
|
||||||
}
|
}
|
||||||
builder.baseUrl(baseUrl);
|
builder.baseUrl(baseUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isStandardPort() {
|
private boolean isStandardPort(String scheme, Integer port) {
|
||||||
if (this.uriPort == null) {
|
if (port == null) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return this.uriScheme.equals("http") && this.uriPort == 80
|
return (scheme.equals("http") && port == 80)
|
||||||
|| this.uriScheme.equals("https") && this.uriPort.equals(443);
|
|| (scheme.equals("https") && port == 443);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue