Migrate missed tests to new Binder
Migrate a few tests that were missed to use the new `Binder`. See gh-9000
This commit is contained in:
parent
528d776d2b
commit
3f71b8453f
|
@ -16,16 +16,12 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure.security;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.boot.bind.RelaxedDataBinder;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
|
||||
import org.springframework.boot.context.properties.source.MockConfigurationPropertySource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
@ -38,88 +34,71 @@ public class SecurityPropertiesTests {
|
|||
|
||||
private SecurityProperties security = new SecurityProperties();
|
||||
|
||||
private RelaxedDataBinder binder = new RelaxedDataBinder(this.security, "security");
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
this.binder.setIgnoreUnknownFields(false);
|
||||
this.binder.setConversionService(new DefaultConversionService());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindingIgnoredSingleValued() {
|
||||
this.binder.bind(new MutablePropertyValues(
|
||||
Collections.singletonMap("security.ignored", "/css/**")));
|
||||
assertThat(this.binder.getBindingResult().hasErrors()).isFalse();
|
||||
bind("security.ignored", "/css/**");
|
||||
assertThat(this.security.getIgnored()).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindingIgnoredEmpty() {
|
||||
this.binder.bind(new MutablePropertyValues(
|
||||
Collections.singletonMap("security.ignored", "")));
|
||||
assertThat(this.binder.getBindingResult().hasErrors()).isFalse();
|
||||
bind("security.ignored", "");
|
||||
assertThat(this.security.getIgnored()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindingIgnoredDisable() {
|
||||
this.binder.bind(new MutablePropertyValues(
|
||||
Collections.singletonMap("security.ignored", "none")));
|
||||
assertThat(this.binder.getBindingResult().hasErrors()).isFalse();
|
||||
bind("security.ignored", "none");
|
||||
assertThat(this.security.getIgnored()).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindingIgnoredMultiValued() {
|
||||
this.binder.bind(new MutablePropertyValues(
|
||||
Collections.singletonMap("security.ignored", "/css/**,/images/**")));
|
||||
assertThat(this.binder.getBindingResult().hasErrors()).isFalse();
|
||||
bind("security.ignored", "/css/**,/images/**");
|
||||
assertThat(this.security.getIgnored()).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindingIgnoredMultiValuedList() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("security.ignored[0]", "/css/**");
|
||||
map.put("security.ignored[1]", "/foo/**");
|
||||
this.binder.bind(new MutablePropertyValues(map));
|
||||
assertThat(this.binder.getBindingResult().hasErrors()).isFalse();
|
||||
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
|
||||
source.put("security.ignored[0]", "/css/**");
|
||||
source.put("security.ignored[1]", "/foo/**");
|
||||
bind(source);
|
||||
assertThat(this.security.getIgnored()).hasSize(2);
|
||||
assertThat(this.security.getIgnored().contains("/foo/**")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultPasswordAutogeneratedIfUnresolvedPlaceholder() {
|
||||
this.binder.bind(new MutablePropertyValues(
|
||||
Collections.singletonMap("security.user.password", "${ADMIN_PASSWORD}")));
|
||||
assertThat(this.binder.getBindingResult().hasErrors()).isFalse();
|
||||
bind("security.user.password", "${ADMIN_PASSWORD}");
|
||||
assertThat(this.security.getUser().isDefaultPassword()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultPasswordAutogeneratedIfEmpty() {
|
||||
this.binder.bind(new MutablePropertyValues(
|
||||
Collections.singletonMap("security.user.password", "")));
|
||||
assertThat(this.binder.getBindingResult().hasErrors()).isFalse();
|
||||
bind("security.user.password", "");
|
||||
assertThat(this.security.getUser().isDefaultPassword()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRoles() {
|
||||
this.binder.bind(new MutablePropertyValues(
|
||||
Collections.singletonMap("security.user.role", "USER,ADMIN")));
|
||||
assertThat(this.binder.getBindingResult().hasErrors()).isFalse();
|
||||
bind("security.user.role", "USER,ADMIN");
|
||||
assertThat(this.security.getUser().getRole().toString())
|
||||
.isEqualTo("[USER, ADMIN]");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRole() {
|
||||
this.binder.bind(new MutablePropertyValues(
|
||||
Collections.singletonMap("security.user.role", "ADMIN")));
|
||||
assertThat(this.binder.getBindingResult().hasErrors()).isFalse();
|
||||
bind("security.user.role", "ADMIN");
|
||||
assertThat(this.security.getUser().getRole().toString()).isEqualTo("[ADMIN]");
|
||||
}
|
||||
|
||||
private void bind(String name, String value) {
|
||||
bind(new MockConfigurationPropertySource(name, value));
|
||||
}
|
||||
|
||||
private void bind(ConfigurationPropertySource source) {
|
||||
new Binder(source).bind("security", Bindable.ofInstance(this.security));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -24,8 +24,9 @@ import java.util.Map;
|
|||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.boot.bind.RelaxedDataBinder;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.context.properties.source.MockConfigurationPropertySource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
@ -46,18 +47,14 @@ public class ServerPropertiesTests {
|
|||
|
||||
@Test
|
||||
public void testAddressBinding() throws Exception {
|
||||
RelaxedDataBinder binder = new RelaxedDataBinder(this.properties, "server");
|
||||
binder.bind(new MutablePropertyValues(
|
||||
Collections.singletonMap("server.address", "127.0.0.1")));
|
||||
assertThat(binder.getBindingResult().hasErrors()).isFalse();
|
||||
bind("server.address", "127.0.0.1");
|
||||
assertThat(this.properties.getAddress())
|
||||
.isEqualTo(InetAddress.getByName("127.0.0.1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPortBinding() throws Exception {
|
||||
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues(
|
||||
Collections.singletonMap("server.port", "9000")));
|
||||
bind("server.port", "9000");
|
||||
assertThat(this.properties.getPort().intValue()).isEqualTo(9000);
|
||||
}
|
||||
|
||||
|
@ -68,36 +65,26 @@ public class ServerPropertiesTests {
|
|||
|
||||
@Test
|
||||
public void testServerHeader() throws Exception {
|
||||
RelaxedDataBinder binder = new RelaxedDataBinder(this.properties, "server");
|
||||
binder.bind(new MutablePropertyValues(
|
||||
Collections.singletonMap("server.server-header", "Custom Server")));
|
||||
bind("server.server-header", "Custom Server");
|
||||
assertThat(this.properties.getServerHeader()).isEqualTo("Custom Server");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConnectionTimeout() throws Exception {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.connection-timeout", "60000");
|
||||
bindProperties(map);
|
||||
bind("server.connection-timeout", "60000");
|
||||
assertThat(this.properties.getConnectionTimeout()).isEqualTo(60000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServletPathAsMapping() throws Exception {
|
||||
RelaxedDataBinder binder = new RelaxedDataBinder(this.properties, "server");
|
||||
binder.bind(new MutablePropertyValues(
|
||||
Collections.singletonMap("server.servlet.path", "/foo/*")));
|
||||
assertThat(binder.getBindingResult().hasErrors()).isFalse();
|
||||
bind("server.servlet.path", "/foo/*");
|
||||
assertThat(this.properties.getServlet().getServletMapping()).isEqualTo("/foo/*");
|
||||
assertThat(this.properties.getServlet().getServletPrefix()).isEqualTo("/foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServletPathAsPrefix() throws Exception {
|
||||
RelaxedDataBinder binder = new RelaxedDataBinder(this.properties, "server");
|
||||
binder.bind(new MutablePropertyValues(
|
||||
Collections.singletonMap("server.servlet.path", "/foo")));
|
||||
assertThat(binder.getBindingResult().hasErrors()).isFalse();
|
||||
bind("server.servlet.path", "/foo");
|
||||
assertThat(this.properties.getServlet().getServletMapping()).isEqualTo("/foo/*");
|
||||
assertThat(this.properties.getServlet().getServletPrefix()).isEqualTo("/foo");
|
||||
}
|
||||
|
@ -111,11 +98,11 @@ public class ServerPropertiesTests {
|
|||
map.put("server.tomcat.accesslog.rename-on-rotate", "true");
|
||||
map.put("server.tomcat.accesslog.request-attributes-enabled", "true");
|
||||
map.put("server.tomcat.accesslog.suffix", "-bar.log");
|
||||
map.put("server.tomcat.protocol_header", "X-Forwarded-Protocol");
|
||||
map.put("server.tomcat.remote_ip_header", "Remote-Ip");
|
||||
map.put("server.tomcat.internal_proxies", "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
|
||||
map.put("server.tomcat.background_processor_delay", "10");
|
||||
bindProperties(map);
|
||||
map.put("server.tomcat.protocol-header", "X-Forwarded-Protocol");
|
||||
map.put("server.tomcat.remote-ip-header", "Remote-Ip");
|
||||
map.put("server.tomcat.internal-proxies", "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
|
||||
map.put("server.tomcat.background-processor-delay", "10");
|
||||
bind(map);
|
||||
ServerProperties.Tomcat tomcat = this.properties.getTomcat();
|
||||
assertThat(tomcat.getAccesslog().getPattern()).isEqualTo("%h %t '%r' %s %b");
|
||||
assertThat(tomcat.getAccesslog().getPrefix()).isEqualTo("foo");
|
||||
|
@ -132,55 +119,45 @@ public class ServerPropertiesTests {
|
|||
|
||||
@Test
|
||||
public void redirectContextRootIsNotConfiguredByDefault() throws Exception {
|
||||
bindProperties(new HashMap<String, String>());
|
||||
bind(new HashMap<String, String>());
|
||||
ServerProperties.Tomcat tomcat = this.properties.getTomcat();
|
||||
assertThat(tomcat.getRedirectContextRoot()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTrailingSlashOfContextPathIsRemoved() {
|
||||
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues(
|
||||
Collections.singletonMap("server.servlet.contextPath", "/foo/")));
|
||||
bind("server.servlet.context-path", "/foo/");
|
||||
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSlashOfContextPathIsDefaultValue() {
|
||||
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues(
|
||||
Collections.singletonMap("server.servlet.contextPath", "/")));
|
||||
bind("server.servlet.context-path", "/");
|
||||
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeUriEncoding() throws Exception {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.uriEncoding", "US-ASCII");
|
||||
bindProperties(map);
|
||||
bind("server.tomcat.uri-encoding", "US-ASCII");
|
||||
assertThat(this.properties.getTomcat().getUriEncoding())
|
||||
.isEqualTo(Charset.forName("US-ASCII"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeHeaderSize() throws Exception {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.maxHttpHeaderSize", "9999");
|
||||
bindProperties(map);
|
||||
bind("server.max-http-header-size", "9999");
|
||||
assertThat(this.properties.getMaxHttpHeaderSize()).isEqualTo(9999);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeJettyAcceptors() throws Exception {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.jetty.acceptors", "10");
|
||||
bindProperties(map);
|
||||
bind("server.jetty.acceptors", "10");
|
||||
assertThat(this.properties.getJetty().getAcceptors()).isEqualTo(10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeJettySelectors() throws Exception {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.jetty.selectors", "10");
|
||||
bindProperties(map);
|
||||
bind("server.jetty.selectors", "10");
|
||||
assertThat(this.properties.getJetty().getSelectors()).isEqualTo(10);
|
||||
}
|
||||
|
||||
|
@ -192,7 +169,7 @@ public class ServerPropertiesTests {
|
|||
map.put("server.jetty.accesslog.file-date-format", "yyyymmdd");
|
||||
map.put("server.jetty.accesslog.retention-period", "4");
|
||||
map.put("server.jetty.accesslog.append", "true");
|
||||
bindProperties(map);
|
||||
bind(map);
|
||||
ServerProperties.Jetty jetty = this.properties.getJetty();
|
||||
assertThat(jetty.getAccesslog().isEnabled()).isEqualTo(true);
|
||||
assertThat(jetty.getAccesslog().getFilename()).isEqualTo("foo.txt");
|
||||
|
@ -201,9 +178,14 @@ public class ServerPropertiesTests {
|
|||
assertThat(jetty.getAccesslog().isAppend()).isEqualTo(true);
|
||||
}
|
||||
|
||||
private void bindProperties(Map<String, String> map) {
|
||||
new RelaxedDataBinder(this.properties, "server")
|
||||
.bind(new MutablePropertyValues(map));
|
||||
private void bind(String name, String value) {
|
||||
bind(Collections.singletonMap(name, value));
|
||||
}
|
||||
|
||||
private void bind(Map<String, String> map) {
|
||||
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
|
||||
map.forEach(source::put);
|
||||
new Binder(source).bind("server", Bindable.ofInstance(this.properties));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -45,9 +45,10 @@ import org.mockito.ArgumentCaptor;
|
|||
import org.mockito.Captor;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.bind.RelaxedDataBinder;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.context.properties.source.MockConfigurationPropertySource;
|
||||
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
|
||||
import org.springframework.boot.web.embedded.jetty.JettyWebServer;
|
||||
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
|
||||
|
@ -255,8 +256,8 @@ public class DefaultServletWebServerFactoryCustomizerTests {
|
|||
@Test
|
||||
public void disableTomcatRemoteIpValve() throws Exception {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.remote_ip_header", "");
|
||||
map.put("server.tomcat.protocol_header", "");
|
||||
map.put("server.tomcat.remote-ip-header", "");
|
||||
map.put("server.tomcat.protocol-header", "");
|
||||
bindProperties(map);
|
||||
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
|
||||
this.customizer.customize(factory);
|
||||
|
@ -286,8 +287,8 @@ public class DefaultServletWebServerFactoryCustomizerTests {
|
|||
public void defaultTomcatRemoteIpValve() throws Exception {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
// Since 1.1.7 you need to specify at least the protocol
|
||||
map.put("server.tomcat.protocol_header", "X-Forwarded-Proto");
|
||||
map.put("server.tomcat.remote_ip_header", "X-Forwarded-For");
|
||||
map.put("server.tomcat.protocol-header", "X-Forwarded-Proto");
|
||||
map.put("server.tomcat.remote-ip-header", "X-Forwarded-For");
|
||||
bindProperties(map);
|
||||
testRemoteIpValveConfigured();
|
||||
}
|
||||
|
@ -328,9 +329,9 @@ public class DefaultServletWebServerFactoryCustomizerTests {
|
|||
@Test
|
||||
public void customTomcatRemoteIpValve() throws Exception {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("server.tomcat.remote_ip_header", "x-my-remote-ip-header");
|
||||
map.put("server.tomcat.protocol_header", "x-my-protocol-header");
|
||||
map.put("server.tomcat.internal_proxies", "192.168.0.1");
|
||||
map.put("server.tomcat.remote-ip-header", "x-my-remote-ip-header");
|
||||
map.put("server.tomcat.protocol-header", "x-my-protocol-header");
|
||||
map.put("server.tomcat.internal-proxies", "192.168.0.1");
|
||||
map.put("server.tomcat.port-header", "x-my-forward-port");
|
||||
map.put("server.tomcat.protocol-header-https-value", "On");
|
||||
bindProperties(map);
|
||||
|
@ -623,8 +624,9 @@ public class DefaultServletWebServerFactoryCustomizerTests {
|
|||
}
|
||||
|
||||
private void bindProperties(Map<String, String> map) {
|
||||
new RelaxedDataBinder(this.properties, "server")
|
||||
.bind(new MutablePropertyValues(map));
|
||||
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
|
||||
map.forEach(source::put);
|
||||
new Binder(source).bind("server", Bindable.ofInstance(this.properties));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue