Extract RestDocsProperties

Extract properties used by RestDocs to their own class.
pull/11587/head
Phillip Webb 7 years ago
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.ConditionalOnWebApplication;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -47,13 +46,13 @@ import org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation
* @since 1.4.0
*/
@Configuration
@EnableConfigurationProperties
@ConditionalOnWebApplication
public class RestDocsAutoConfiguration {
@Configuration
@ConditionalOnClass(MockMvcRestDocumentation.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
@EnableConfigurationProperties(RestDocsProperties.class)
static class RestDocsMockMvcAutoConfiguration {
@Bean
@ -72,11 +71,11 @@ public class RestDocsAutoConfiguration {
}
@Bean
@ConfigurationProperties(prefix = "spring.test.restdocs")
public RestDocsMockMvcBuilderCustomizer restDocumentationConfigurer(
RestDocsProperties properties,
MockMvcRestDocumentationConfigurer configurer,
ObjectProvider<RestDocumentationResultHandler> resultHandler) {
return new RestDocsMockMvcBuilderCustomizer(configurer,
return new RestDocsMockMvcBuilderCustomizer(properties, configurer,
resultHandler.getIfAvailable());
}
@ -85,6 +84,7 @@ public class RestDocsAutoConfiguration {
@Configuration
@ConditionalOnClass({ RequestSpecification.class,
RestAssuredRestDocumentation.class })
@EnableConfigurationProperties(RestDocsProperties.class)
static class RestDocsRestAssuredAutoConfiguration {
@Bean
@ -103,10 +103,9 @@ public class RestDocsAutoConfiguration {
}
@Bean
@ConfigurationProperties(prefix = "spring.test.restdocs")
public RestDocsRestAssuredBuilderCustomizer restAssuredBuilderCustomizer(
RequestSpecification configurer) {
return new RestDocsRestAssuredBuilderCustomizer(configurer);
RestDocsProperties properties, RequestSpecification configurer) {
return new RestDocsRestAssuredBuilderCustomizer(properties, configurer);
}
}
@ -114,6 +113,7 @@ public class RestDocsAutoConfiguration {
@Configuration
@ConditionalOnClass(WebTestClientRestDocumentation.class)
@ConditionalOnWebApplication(type = Type.REACTIVE)
@EnableConfigurationProperties(RestDocsProperties.class)
static class RestDocsWebTestClientAutoConfiguration {
@Bean
@ -132,10 +132,10 @@ public class RestDocsAutoConfiguration {
}
@Bean
@ConfigurationProperties(prefix = "spring.test.restdocs")
public RestDocsWebTestClientBuilderCustomizer restDocumentationConfigurer(
RestDocsProperties properties,
WebTestClientRestDocumentationConfigurer configurer) {
return new RestDocsWebTestClientBuilderCustomizer(configurer);
return new RestDocsWebTestClientBuilderCustomizer(properties, configurer);
}
}

@ -17,11 +17,12 @@
package org.springframework.boot.test.autoconfigure.restdocs;
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.restdocs.mockmvc.MockMvcRestDocumentationConfigurer;
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
import org.springframework.restdocs.mockmvc.UriConfigurer;
import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder;
import org.springframework.util.StringUtils;
/**
* A {@link MockMvcBuilderCustomizer} that configures Spring REST Docs.
@ -31,57 +32,28 @@ import org.springframework.util.StringUtils;
class RestDocsMockMvcBuilderCustomizer
implements InitializingBean, MockMvcBuilderCustomizer {
private final RestDocsProperties properties;
private final MockMvcRestDocumentationConfigurer delegate;
private final RestDocumentationResultHandler resultHandler;
private String uriScheme;
private String uriHost;
private Integer uriPort;
RestDocsMockMvcBuilderCustomizer(MockMvcRestDocumentationConfigurer delegate,
RestDocsMockMvcBuilderCustomizer(RestDocsProperties properties,
MockMvcRestDocumentationConfigurer delegate,
RestDocumentationResultHandler resultHandler) {
this.properties = properties;
this.delegate = delegate;
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
public void afterPropertiesSet() throws Exception {
if (StringUtils.hasText(this.uriScheme)) {
this.delegate.uris().withScheme(this.uriScheme);
}
if (StringUtils.hasText(this.uriHost)) {
this.delegate.uris().withHost(this.uriHost);
}
if (this.uriPort != null) {
this.delegate.uris().withPort(this.uriPort);
}
PropertyMapper map = PropertyMapper.get();
RestDocsProperties properties = this.properties;
UriConfigurer uri = this.delegate.uris();
map.from(properties::getUriScheme).whenHasText().to(uri::withScheme);
map.from(properties::getUriHost).whenHasText().to(uri::withHost);
map.from(properties::getUriPort).whenNonNull().to(uri::withPort);
}
@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 org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.util.StringUtils;
/**
@ -28,50 +29,25 @@ import org.springframework.util.StringUtils;
*/
class RestDocsRestAssuredBuilderCustomizer implements InitializingBean {
private final RequestSpecification delegate;
private String uriScheme;
private String uriHost;
private final RestDocsProperties properties;
private Integer uriPort;
private final RequestSpecification delegate;
RestDocsRestAssuredBuilderCustomizer(RequestSpecification delegate) {
RestDocsRestAssuredBuilderCustomizer(RestDocsProperties properties,
RequestSpecification delegate) {
this.properties = properties;
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
public void afterPropertiesSet() throws Exception {
if (StringUtils.hasText(this.uriScheme) && StringUtils.hasText(this.uriHost)) {
this.delegate.baseUri(this.uriScheme + "://" + this.uriHost);
}
if (this.uriPort != null) {
this.delegate.port(this.uriPort);
}
PropertyMapper map = PropertyMapper.get();
String host = this.properties.getUriHost();
map.from(this.properties::getUriScheme)
.when((scheme) -> StringUtils.hasText(scheme)
&& 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 {
private final WebTestClientRestDocumentationConfigurer delegate;
private String uriScheme;
private final RestDocsProperties properties;
private String uriHost;
private Integer uriPort;
private final WebTestClientRestDocumentationConfigurer delegate;
RestDocsWebTestClientBuilderCustomizer(
RestDocsWebTestClientBuilderCustomizer(RestDocsProperties properties,
WebTestClientRestDocumentationConfigurer delegate) {
this.properties = properties;
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
public void customize(WebTestClient.Builder builder) {
customizeBaseUrl(builder);
@ -73,21 +46,23 @@ class RestDocsWebTestClientBuilderCustomizer implements WebTestClientBuilderCust
}
private void customizeBaseUrl(WebTestClient.Builder builder) {
String scheme = StringUtils.hasText(this.uriScheme) ? this.uriScheme : "http";
String host = StringUtils.hasText(this.uriHost) ? this.uriHost : "localhost";
String baseUrl = scheme + "://" + host;
if (!isStandardPort()) {
baseUrl += ":" + this.uriPort;
String scheme = this.properties.getUriScheme();
String host = this.properties.getUriHost();
String baseUrl = (StringUtils.hasText(scheme) ? scheme : "http") + "://"
+ (StringUtils.hasText(host) ? host : "localhost");
Integer port = this.properties.getUriPort();
if (!isStandardPort(scheme, port)) {
baseUrl += ":" + port;
}
builder.baseUrl(baseUrl);
}
private boolean isStandardPort() {
if (this.uriPort == null) {
private boolean isStandardPort(String scheme, Integer port) {
if (port == null) {
return true;
}
return this.uriScheme.equals("http") && this.uriPort == 80
|| this.uriScheme.equals("https") && this.uriPort.equals(443);
return (scheme.equals("http") && port == 80)
|| (scheme.equals("https") && port == 443);
}
}

Loading…
Cancel
Save