|
17 | 17 | package org.springframework.boot.autoconfigure.web.embedded;
|
18 | 18 |
|
19 | 19 | import java.io.File;
|
| 20 | +import java.util.Arrays; |
20 | 21 |
|
| 22 | +import io.undertow.Undertow; |
| 23 | +import io.undertow.Undertow.Builder; |
| 24 | +import io.undertow.UndertowOptions; |
21 | 25 | import org.junit.Before;
|
22 | 26 | import org.junit.Test;
|
| 27 | +import org.xnio.OptionMap; |
23 | 28 |
|
24 | 29 | import org.springframework.boot.autoconfigure.web.ServerProperties;
|
25 | 30 | import org.springframework.boot.context.properties.bind.Bindable;
|
26 | 31 | import org.springframework.boot.context.properties.bind.Binder;
|
27 | 32 | import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
|
28 | 33 | import org.springframework.boot.web.embedded.undertow.ConfigurableUndertowWebServerFactory;
|
| 34 | +import org.springframework.boot.web.embedded.undertow.UndertowBuilderCustomizer; |
29 | 35 | import org.springframework.mock.env.MockEnvironment;
|
30 | 36 | import org.springframework.test.context.support.TestPropertySourceUtils;
|
| 37 | +import org.springframework.test.util.ReflectionTestUtils; |
31 | 38 |
|
| 39 | +import static org.assertj.core.api.Assertions.assertThat; |
| 40 | +import static org.mockito.ArgumentMatchers.any; |
| 41 | +import static org.mockito.BDDMockito.willAnswer; |
32 | 42 | import static org.mockito.Mockito.mock;
|
33 | 43 | import static org.mockito.Mockito.verify;
|
34 | 44 |
|
@@ -100,6 +110,54 @@ public void setUseForwardHeaders() {
|
100 | 110 | verify(factory).setUseForwardHeaders(true);
|
101 | 111 | }
|
102 | 112 |
|
| 113 | + @Test |
| 114 | + public void customizeMaxHttpHeaderSize() { |
| 115 | + bind("server.max-http-header-size=2048"); |
| 116 | + Builder builder = Undertow.builder(); |
| 117 | + ConfigurableUndertowWebServerFactory factory = mockFactory(builder); |
| 118 | + this.customizer.customize(factory); |
| 119 | + OptionMap map = ((OptionMap.Builder) ReflectionTestUtils.getField(builder, |
| 120 | + "serverOptions")).getMap(); |
| 121 | + assertThat(map.get(UndertowOptions.MAX_HEADER_SIZE).intValue()).isEqualTo(2048); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void customMaxHttpHeaderSizeIgnoredIfNegative() { |
| 126 | + bind("server.max-http-header-size=-1"); |
| 127 | + Builder builder = Undertow.builder(); |
| 128 | + ConfigurableUndertowWebServerFactory factory = mockFactory(builder); |
| 129 | + this.customizer.customize(factory); |
| 130 | + OptionMap map = ((OptionMap.Builder) ReflectionTestUtils.getField(builder, |
| 131 | + "serverOptions")).getMap(); |
| 132 | + assertThat(map.contains(UndertowOptions.MAX_HEADER_SIZE)).isFalse(); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void customMaxHttpHeaderSizeIgnoredIfZero() { |
| 137 | + bind("server.max-http-header-size=0"); |
| 138 | + Builder builder = Undertow.builder(); |
| 139 | + ConfigurableUndertowWebServerFactory factory = mockFactory(builder); |
| 140 | + this.customizer.customize(factory); |
| 141 | + OptionMap map = ((OptionMap.Builder) ReflectionTestUtils.getField(builder, |
| 142 | + "serverOptions")).getMap(); |
| 143 | + assertThat(map.contains(UndertowOptions.MAX_HEADER_SIZE)).isFalse(); |
| 144 | + } |
| 145 | + |
| 146 | + private ConfigurableUndertowWebServerFactory mockFactory(Builder builder) { |
| 147 | + ConfigurableUndertowWebServerFactory factory = mock( |
| 148 | + ConfigurableUndertowWebServerFactory.class); |
| 149 | + willAnswer((invocation) -> { |
| 150 | + Object argument = invocation.getArgument(0); |
| 151 | + Arrays.stream((argument instanceof UndertowBuilderCustomizer) |
| 152 | + ? new UndertowBuilderCustomizer[] { |
| 153 | + (UndertowBuilderCustomizer) argument } |
| 154 | + : (UndertowBuilderCustomizer[]) argument) |
| 155 | + .forEach((customizer) -> customizer.customize(builder)); |
| 156 | + return null; |
| 157 | + }).given(factory).addBuilderCustomizers(any()); |
| 158 | + return factory; |
| 159 | + } |
| 160 | + |
103 | 161 | private void bind(String... inlinedProperties) {
|
104 | 162 | TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
|
105 | 163 | inlinedProperties);
|
|
0 commit comments