Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,18 @@ public int getOrder() {
@Override
public void customize(NettyReactiveWebServerFactory factory) {
factory.setUseForwardHeaders(getOrDeduceUseForwardHeaders());
PropertyMapper propertyMapper = PropertyMapper.get();
propertyMapper.from(this.serverProperties::getMaxHttpHeaderSize).whenNonNull()
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).to((duration) -> factory

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)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -49,6 +55,12 @@ public void setup() {
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", "-");
Expand All @@ -72,4 +84,24 @@ public void setUseForwardHeaders() {
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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* {@link Source#toInstance(Function) new instance}.
*
* @author Phillip Webb
* @author Artsiom Yudovin
* @since 2.0.0
*/
public final class PropertyMapper {
Expand Down Expand Up @@ -289,7 +290,7 @@ public <R extends T> Source<R> whenInstanceOf(Class<R> target) {
*/
public Source<T> whenNot(Predicate<T> predicate) {
Assert.notNull(predicate, "Predicate must not be null");
return new Source<>(this.supplier, predicate.negate());
return when(predicate.negate());
}

/**
Expand All @@ -300,7 +301,13 @@ public Source<T> whenNot(Predicate<T> predicate) {
*/
public Source<T> when(Predicate<T> predicate) {
Assert.notNull(predicate, "Predicate must not be null");
return new Source<>(this.supplier, predicate);

if (Objects.nonNull(this.predicate)) {
return new Source<>(this.supplier, this.predicate.and(predicate));
}
else {
return new Source<>(this.supplier, predicate);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* Tests for {@link PropertyMapper}.
*
* @author Phillip Webb
* @author Artsiom Yudovin
*/
public class PropertyMapperTests {

Expand Down Expand Up @@ -201,6 +202,18 @@ public void alwaysApplyingWhenNonNullShouldAlwaysApplyNonNullToSource() {
this.map.alwaysApplyingWhenNonNull().from(() -> null).toCall(Assert::fail);
}

@Test
public void whenWhenValueNotMatchesShouldCheckTwoWhen() {
this.map.from("123").when("456"::equals).when("123"::equals).toCall(Assert::fail);
}

@Test
public void whenWhenValueMatchesShouldCheckTwoWhen() {
String result = this.map.from("123").when((s) -> s.contains("2"))
.when("123"::equals).toInstance(String::new);
assertThat(result).isEqualTo("123");
}

private static class Count<T> implements Supplier<T> {

private final Supplier<T> source;
Expand Down