diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizer.java index 54029cdd07..d962b0cdc7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizer.java @@ -58,10 +58,12 @@ public class NettyWebServerFactoryCustomizer @Override public void customize(NettyReactiveWebServerFactory factory) { factory.setUseForwardHeaders(getOrDeduceUseForwardHeaders(this.serverProperties, this.environment)); - PropertyMapper propertyMapper = PropertyMapper.get(); - propertyMapper.from(this.serverProperties::getMaxHttpHeaderSize).whenNonNull().asInt(DataSize::toBytes) + PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull(); + propertyMapper.from(this.serverProperties::getMaxHttpHeaderSize).asInt(DataSize::toBytes) .to((maxHttpRequestHeaderSize) -> customizeMaxHttpHeaderSize(factory, maxHttpRequestHeaderSize)); - propertyMapper.from(this.serverProperties::getConnectionTimeout).whenNonNull().asInt(Duration::toMillis) + propertyMapper.from(this.serverProperties::getConnectionTimeout).asInt(Duration::toMillis) + .whenNot((connectionTimout) -> connectionTimout.equals(0)) + .as((connectionTimeout) -> connectionTimeout.equals(-1) ? 0 : connectionTimeout) .to((duration) -> factory.addServerCustomizers(getConnectionTimeOutCustomizer(duration))); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizerTests.java index d5429548dd..05ca5c6e72 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/NettyWebServerFactoryCustomizerTests.java @@ -16,21 +16,27 @@ package org.springframework.boot.autoconfigure.web.embedded; +import java.time.Duration; + import org.junit.Before; import org.junit.Test; import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.context.properties.source.ConfigurationPropertySources; import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory; +import org.springframework.boot.web.embedded.netty.NettyServerCustomizer; import org.springframework.mock.env.MockEnvironment; +import static org.mockito.Mockito.any; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; /** * Tests for {@link NettyWebServerFactoryCustomizer}. * * @author Brian Clozel + * @author Artsiom Yudovin */ public class NettyWebServerFactoryCustomizerTests { @@ -48,6 +54,12 @@ public class NettyWebServerFactoryCustomizerTests { this.customizer = new NettyWebServerFactoryCustomizer(this.environment, this.serverProperties); } + private void clear() { + this.serverProperties.setUseForwardHeaders(null); + this.serverProperties.setMaxHttpHeaderSize(null); + this.serverProperties.setConnectionTimeout(null); + } + @Test public void deduceUseForwardHeaders() { this.environment.setProperty("DYNO", "-"); @@ -71,4 +83,24 @@ public class NettyWebServerFactoryCustomizerTests { verify(factory).setUseForwardHeaders(true); } + @Test + public void setConnectionTimeoutAsZero() { + clear(); + this.serverProperties.setConnectionTimeout(Duration.ZERO); + + NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class); + this.customizer.customize(factory); + verify(factory, times(0)).addServerCustomizers(any(NettyServerCustomizer.class)); + } + + @Test + public void setConnectionTimeoutAsMinusOne() { + clear(); + this.serverProperties.setConnectionTimeout(Duration.ofNanos(-1)); + + NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class); + this.customizer.customize(factory); + verify(factory, times(1)).addServerCustomizers(any(NettyServerCustomizer.class)); + } + }