forked from spring-projects/spring-boot
/
TomcatWebServerFactoryCustomizerTests.java
357 lines (311 loc) · 13.3 KB
/
TomcatWebServerFactoryCustomizerTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* Copyright 2012-2019 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
*
* https://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.autoconfigure.web.embedded;
import java.util.function.Consumer;
import org.apache.catalina.Context;
import org.apache.catalina.Valve;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.valves.AccessLogValve;
import org.apache.catalina.valves.ErrorReportValve;
import org.apache.catalina.valves.RemoteIpValve;
import org.apache.coyote.AbstractProtocol;
import org.apache.coyote.http11.AbstractHttp11Protocol;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.util.unit.DataSize;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link TomcatWebServerFactoryCustomizer}
*
* @author Brian Clozel
* @author Phillip Webb
* @author Rob Tompkins
* @author Artsiom Yudovin
* @author Stephane Nicoll
* @author Rafiullah Hamedy
*/
public class TomcatWebServerFactoryCustomizerTests {
private MockEnvironment environment;
private ServerProperties serverProperties;
private TomcatWebServerFactoryCustomizer customizer;
@Before
public void setup() {
this.environment = new MockEnvironment();
this.serverProperties = new ServerProperties();
ConfigurationPropertySources.attach(this.environment);
this.customizer = new TomcatWebServerFactoryCustomizer(this.environment, this.serverProperties);
}
@Test
public void defaultsAreConsistent() {
customizeAndRunServer((server) -> assertThat(
((AbstractHttp11Protocol<?>) server.getTomcat().getConnector().getProtocolHandler())
.getMaxSwallowSize())
.isEqualTo(this.serverProperties.getTomcat().getMaxSwallowSize().toBytes()));
}
@Test
public void customAcceptCount() {
bind("server.tomcat.accept-count=10");
customizeAndRunServer((server) -> assertThat(
((AbstractProtocol<?>) server.getTomcat().getConnector().getProtocolHandler()).getAcceptCount())
.isEqualTo(10));
}
@Test
public void customBackgroundProcessorDelay() {
bind("server.tomcat.background-processor-delay=5");
TomcatWebServer server = customizeAndGetServer();
assertThat(server.getTomcat().getEngine().getBackgroundProcessorDelay()).isEqualTo(5);
}
@Test
public void customDisableMaxHttpPostSize() {
bind("server.tomcat.max-http-post-size=-1");
customizeAndRunServer((server) -> assertThat(server.getTomcat().getConnector().getMaxPostSize()).isEqualTo(-1));
}
@Test
public void customDisableMaxHttpFormPostSize() {
bind("server.tomcat.max-http-form-post-size=-1");
customizeAndRunServer((server) -> assertThat(server.getTomcat().getConnector().getMaxPostSize()).isEqualTo(-1));
}
@Test
public void customMaxConnections() {
bind("server.tomcat.max-connections=5");
customizeAndRunServer((server) -> assertThat(
((AbstractProtocol<?>) server.getTomcat().getConnector().getProtocolHandler()).getMaxConnections())
.isEqualTo(5));
}
@Test
public void customMaxHttpPostSize() {
bind("server.tomcat.max-http-post-size=10000");
customizeAndRunServer(
(server) -> assertThat(server.getTomcat().getConnector().getMaxPostSize()).isEqualTo(10000));
}
@Test
public void customMaxHttpFormPostSize() {
bind("server.tomcat.max-http-form-post-size=10000");
customizeAndRunServer(
(server) -> assertThat(server.getTomcat().getConnector().getMaxPostSize()).isEqualTo(10000));
}
@Test
public void customMaxHttpHeaderSize() {
bind("server.max-http-header-size=1KB");
customizeAndRunServer((server) -> assertThat(
((AbstractHttp11Protocol<?>) server.getTomcat().getConnector().getProtocolHandler())
.getMaxHttpHeaderSize()).isEqualTo(DataSize.ofKilobytes(1).toBytes()));
}
@Test
public void customMaxHttpHeaderSizeIgnoredIfNegative() {
bind("server.max-http-header-size=-1");
customizeAndRunServer((server) -> assertThat(
((AbstractHttp11Protocol<?>) server.getTomcat().getConnector().getProtocolHandler())
.getMaxHttpHeaderSize()).isEqualTo(DataSize.ofKilobytes(8).toBytes()));
}
@Test
public void customMaxHttpHeaderSizeIgnoredIfZero() {
bind("server.max-http-header-size=0");
customizeAndRunServer((server) -> assertThat(
((AbstractHttp11Protocol<?>) server.getTomcat().getConnector().getProtocolHandler())
.getMaxHttpHeaderSize()).isEqualTo(DataSize.ofKilobytes(8).toBytes()));
}
@Test
@Deprecated
public void customMaxHttpHeaderSizeWithDeprecatedProperty() {
bind("server.max-http-header-size=4KB", "server.tomcat.max-http-header-size=1024");
customizeAndRunServer((server) -> assertThat(
((AbstractHttp11Protocol<?>) server.getTomcat().getConnector().getProtocolHandler())
.getMaxHttpHeaderSize()).isEqualTo(DataSize.ofKilobytes(1).toBytes()));
}
@Test
public void customMaxSwallowSize() {
bind("server.tomcat.max-swallow-size=10MB");
customizeAndRunServer((server) -> assertThat(
((AbstractHttp11Protocol<?>) server.getTomcat().getConnector().getProtocolHandler())
.getMaxSwallowSize()).isEqualTo(DataSize.ofMegabytes(10).toBytes()));
}
@Test
public void customConnectionTimeout() {
bind("server.tomcat.connection-timeout=30s");
customizeAndRunServer((server) -> assertThat(
((AbstractProtocol<?>) server.getTomcat().getConnector().getProtocolHandler()).getConnectionTimeout())
.isEqualTo(30000));
}
@Test
public void customRemoteIpValve() {
bind("server.tomcat.remote-ip-header=x-my-remote-ip-header",
"server.tomcat.protocol-header=x-my-protocol-header", "server.tomcat.internal-proxies=192.168.0.1",
"server.tomcat.port-header=x-my-forward-port", "server.tomcat.protocol-header-https-value=On");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(factory.getEngineValves()).hasSize(1);
Valve valve = factory.getEngineValves().iterator().next();
assertThat(valve).isInstanceOf(RemoteIpValve.class);
RemoteIpValve remoteIpValve = (RemoteIpValve) valve;
assertThat(remoteIpValve.getProtocolHeader()).isEqualTo("x-my-protocol-header");
assertThat(remoteIpValve.getProtocolHeaderHttpsValue()).isEqualTo("On");
assertThat(remoteIpValve.getRemoteIpHeader()).isEqualTo("x-my-remote-ip-header");
assertThat(remoteIpValve.getPortHeader()).isEqualTo("x-my-forward-port");
assertThat(remoteIpValve.getInternalProxies()).isEqualTo("192.168.0.1");
}
@Test
public void customStaticResourceAllowCaching() {
bind("server.tomcat.resource.allow-caching=false");
customizeAndRunServer((server) -> {
Tomcat tomcat = server.getTomcat();
Context context = (Context) tomcat.getHost().findChildren()[0];
assertThat(context.getResources().isCachingAllowed()).isFalse();
});
}
@Test
public void customStaticResourceCacheTtl() {
bind("server.tomcat.resource.cache-ttl=10000");
customizeAndRunServer((server) -> {
Tomcat tomcat = server.getTomcat();
Context context = (Context) tomcat.getHost().findChildren()[0];
assertThat(context.getResources().getCacheTtl()).isEqualTo(10000L);
});
}
@Test
public void deduceUseForwardHeaders() {
this.environment.setProperty("DYNO", "-");
testRemoteIpValveConfigured();
}
@Test
public void defaultRemoteIpValve() {
// Since 1.1.7 you need to specify at least the protocol
bind("server.tomcat.protocol-header=X-Forwarded-Proto", "server.tomcat.remote-ip-header=X-Forwarded-For");
testRemoteIpValveConfigured();
}
@Test
public void setUseForwardHeaders() {
// Since 1.3.0 no need to explicitly set header names if use-forward-header=true
this.serverProperties.setUseForwardHeaders(true);
testRemoteIpValveConfigured();
}
private void testRemoteIpValveConfigured() {
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(factory.getEngineValves()).hasSize(1);
Valve valve = factory.getEngineValves().iterator().next();
assertThat(valve).isInstanceOf(RemoteIpValve.class);
RemoteIpValve remoteIpValve = (RemoteIpValve) valve;
assertThat(remoteIpValve.getProtocolHeader()).isEqualTo("X-Forwarded-Proto");
assertThat(remoteIpValve.getProtocolHeaderHttpsValue()).isEqualTo("https");
assertThat(remoteIpValve.getRemoteIpHeader()).isEqualTo("X-Forwarded-For");
String expectedInternalProxies = "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|" // 10/8
+ "192\\.168\\.\\d{1,3}\\.\\d{1,3}|" // 192.168/16
+ "169\\.254\\.\\d{1,3}\\.\\d{1,3}|" // 169.254/16
+ "127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|" // 127/8
+ "172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|" // 172.16/12
+ "172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|" + "172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3}|" //
+ "0:0:0:0:0:0:0:1|::1";
assertThat(remoteIpValve.getInternalProxies()).isEqualTo(expectedInternalProxies);
}
@Test
public void defaultBackgroundProcessorDelay() {
TomcatWebServer server = customizeAndGetServer();
assertThat(server.getTomcat().getEngine().getBackgroundProcessorDelay()).isEqualTo(10);
}
@Test
public void disableRemoteIpValve() {
bind("server.tomcat.remote-ip-header=", "server.tomcat.protocol-header=");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(factory.getEngineValves()).isEmpty();
}
@Test
public void errorReportValveIsConfiguredToNotReportStackTraces() {
TomcatWebServer server = customizeAndGetServer();
Valve[] valves = server.getTomcat().getHost().getPipeline().getValves();
assertThat(valves).hasAtLeastOneElementOfType(ErrorReportValve.class);
for (Valve valve : valves) {
if (valve instanceof ErrorReportValve) {
ErrorReportValve errorReportValve = (ErrorReportValve) valve;
assertThat(errorReportValve.isShowReport()).isFalse();
assertThat(errorReportValve.isShowServerInfo()).isFalse();
}
}
}
@Test
public void testCustomizeMinSpareThreads() {
bind("server.tomcat.min-spare-threads=10");
assertThat(this.serverProperties.getTomcat().getMinSpareThreads()).isEqualTo(10);
}
@Test
public void accessLogBufferingCanBeDisabled() {
bind("server.tomcat.accesslog.enabled=true", "server.tomcat.accesslog.buffered=false");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()).isBuffered()).isFalse();
}
@Test
public void accessLogCanBeEnabled() {
bind("server.tomcat.accesslog.enabled=true");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(factory.getEngineValves()).hasSize(1);
assertThat(factory.getEngineValves()).first().isInstanceOf(AccessLogValve.class);
}
@Test
public void accessLogFileDateFormatByDefault() {
bind("server.tomcat.accesslog.enabled=true");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()).getFileDateFormat())
.isEqualTo(".yyyy-MM-dd");
}
@Test
public void accessLogFileDateFormatCanBeRedefined() {
bind("server.tomcat.accesslog.enabled=true", "server.tomcat.accesslog.file-date-format=yyyy-MM-dd.HH");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()).getFileDateFormat())
.isEqualTo("yyyy-MM-dd.HH");
}
@Test
public void accessLogIsBufferedByDefault() {
bind("server.tomcat.accesslog.enabled=true");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()).isBuffered()).isTrue();
}
@Test
public void accessLogIsDisabledByDefault() {
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(factory.getEngineValves()).isEmpty();
}
private void bind(String... inlinedProperties) {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, inlinedProperties);
new Binder(ConfigurationPropertySources.get(this.environment)).bind("server",
Bindable.ofInstance(this.serverProperties));
}
private void customizeAndRunServer(Consumer<TomcatWebServer> consumer) {
TomcatWebServer server = customizeAndGetServer();
server.start();
try {
consumer.accept(server);
}
finally {
server.stop();
}
}
private TomcatWebServer customizeAndGetServer() {
TomcatServletWebServerFactory factory = customizeAndGetFactory();
return (TomcatWebServer) factory.getWebServer();
}
private TomcatServletWebServerFactory customizeAndGetFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0);
this.customizer.customize(factory);
return factory;
}
}