-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
TomcatWebServerFactoryCustomizerTests.java
547 lines (479 loc) · 20.6 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
* Copyright 2012-2022 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.Locale;
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.ajp.AbstractAjpProtocol;
import org.apache.coyote.http11.AbstractHttp11Protocol;
import org.apache.coyote.http2.Http2Protocol;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.ServerProperties.ForwardHeadersStrategy;
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.boot.web.server.WebServer;
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 Andrew McGhie
* @author Rafiullah Hamedy
* @author Victor Mandujano
* @author Parviz Rozikov
*/
class TomcatWebServerFactoryCustomizerTests {
private MockEnvironment environment;
private ServerProperties serverProperties;
private TomcatWebServerFactoryCustomizer customizer;
@BeforeEach
void setup() {
this.environment = new MockEnvironment();
this.serverProperties = new ServerProperties();
ConfigurationPropertySources.attach(this.environment);
this.customizer = new TomcatWebServerFactoryCustomizer(this.environment, this.serverProperties);
}
@Test
void defaultsAreConsistent() {
customizeAndRunServer((server) -> assertThat(
((AbstractHttp11Protocol<?>) server.getTomcat().getConnector().getProtocolHandler())
.getMaxSwallowSize())
.isEqualTo(this.serverProperties.getTomcat().getMaxSwallowSize().toBytes()));
}
@Test
void customAcceptCount() {
bind("server.tomcat.accept-count=10");
customizeAndRunServer((server) -> assertThat(
((AbstractProtocol<?>) server.getTomcat().getConnector().getProtocolHandler()).getAcceptCount())
.isEqualTo(10));
}
@Test
void customProcessorCache() {
bind("server.tomcat.processor-cache=100");
customizeAndRunServer((server) -> assertThat(
((AbstractProtocol<?>) server.getTomcat().getConnector().getProtocolHandler()).getProcessorCache())
.isEqualTo(100));
}
@Test
void customKeepAliveTimeout() {
bind("server.tomcat.keep-alive-timeout=30ms");
customizeAndRunServer((server) -> assertThat(
((AbstractProtocol<?>) server.getTomcat().getConnector().getProtocolHandler()).getKeepAliveTimeout())
.isEqualTo(30));
}
@Test
void defaultKeepAliveTimeoutWithHttp2() {
bind("server.http2.enabled=true");
customizeAndRunServer((server) -> assertThat(
((Http2Protocol) server.getTomcat().getConnector().findUpgradeProtocols()[0]).getKeepAliveTimeout())
.isEqualTo(20000L));
}
@Test
void customKeepAliveTimeoutWithHttp2() {
bind("server.tomcat.keep-alive-timeout=30s", "server.http2.enabled=true");
customizeAndRunServer((server) -> assertThat(
((Http2Protocol) server.getTomcat().getConnector().findUpgradeProtocols()[0]).getKeepAliveTimeout())
.isEqualTo(30000L));
}
@Test
void customMaxKeepAliveRequests() {
bind("server.tomcat.max-keep-alive-requests=-1");
customizeAndRunServer((server) -> assertThat(
((AbstractHttp11Protocol<?>) server.getTomcat().getConnector().getProtocolHandler())
.getMaxKeepAliveRequests()).isEqualTo(-1));
}
@Test
void defaultMaxKeepAliveRequests() {
customizeAndRunServer((server) -> assertThat(
((AbstractHttp11Protocol<?>) server.getTomcat().getConnector().getProtocolHandler())
.getMaxKeepAliveRequests()).isEqualTo(100));
}
@Test
void unlimitedProcessorCache() {
bind("server.tomcat.processor-cache=-1");
customizeAndRunServer((server) -> assertThat(
((AbstractProtocol<?>) server.getTomcat().getConnector().getProtocolHandler()).getProcessorCache())
.isEqualTo(-1));
}
@Test
void customBackgroundProcessorDelay() {
bind("server.tomcat.background-processor-delay=5");
TomcatWebServer server = customizeAndGetServer();
assertThat(server.getTomcat().getEngine().getBackgroundProcessorDelay()).isEqualTo(5);
}
@Test
void customDisableMaxHttpFormPostSize() {
bind("server.tomcat.max-http-form-post-size=-1");
customizeAndRunServer((server) -> assertThat(server.getTomcat().getConnector().getMaxPostSize()).isEqualTo(-1));
}
@Test
void customMaxConnections() {
bind("server.tomcat.max-connections=5");
customizeAndRunServer((server) -> assertThat(
((AbstractProtocol<?>) server.getTomcat().getConnector().getProtocolHandler()).getMaxConnections())
.isEqualTo(5));
}
@Test
void customMaxHttpFormPostSize() {
bind("server.tomcat.max-http-form-post-size=10000");
customizeAndRunServer(
(server) -> assertThat(server.getTomcat().getConnector().getMaxPostSize()).isEqualTo(10000));
}
@Test
void customMaxHttpHeaderSize() {
bind("server.max-http-header-size=1KB");
customizeAndRunServer((server) -> assertThat(
((AbstractHttp11Protocol<?>) server.getTomcat().getConnector().getProtocolHandler())
.getMaxHttpHeaderSize()).isEqualTo(DataSize.ofKilobytes(1).toBytes()));
}
@Test
void customMaxHttpHeaderSizeIgnoredIfNegative() {
bind("server.max-http-header-size=-1");
customizeAndRunServer((server) -> assertThat(
((AbstractHttp11Protocol<?>) server.getTomcat().getConnector().getProtocolHandler())
.getMaxHttpHeaderSize()).isEqualTo(DataSize.ofKilobytes(8).toBytes()));
}
@Test
void customMaxHttpHeaderSizeIgnoredIfZero() {
bind("server.max-http-header-size=0");
customizeAndRunServer((server) -> assertThat(
((AbstractHttp11Protocol<?>) server.getTomcat().getConnector().getProtocolHandler())
.getMaxHttpHeaderSize()).isEqualTo(DataSize.ofKilobytes(8).toBytes()));
}
@Test
void customMaxSwallowSize() {
bind("server.tomcat.max-swallow-size=10MB");
customizeAndRunServer((server) -> assertThat(
((AbstractHttp11Protocol<?>) server.getTomcat().getConnector().getProtocolHandler())
.getMaxSwallowSize()).isEqualTo(DataSize.ofMegabytes(10).toBytes()));
}
@Test
void customRemoteIpValve() {
bind("server.tomcat.remoteip.remote-ip-header=x-my-remote-ip-header",
"server.tomcat.remoteip.protocol-header=x-my-protocol-header",
"server.tomcat.remoteip.internal-proxies=192.168.0.1",
"server.tomcat.remoteip.host-header=x-my-forward-host",
"server.tomcat.remoteip.port-header=x-my-forward-port",
"server.tomcat.remoteip.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.getHostHeader()).isEqualTo("x-my-forward-host");
assertThat(remoteIpValve.getPortHeader()).isEqualTo("x-my-forward-port");
assertThat(remoteIpValve.getInternalProxies()).isEqualTo("192.168.0.1");
}
@Test
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
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
void customRelaxedPathChars() {
bind("server.tomcat.relaxed-path-chars=|,^");
customizeAndRunServer((server) -> assertThat(
((AbstractHttp11Protocol<?>) server.getTomcat().getConnector().getProtocolHandler())
.getRelaxedPathChars()).isEqualTo("|^"));
}
@Test
void customRelaxedQueryChars() {
bind("server.tomcat.relaxed-query-chars=^ , | ");
customizeAndRunServer((server) -> assertThat(
((AbstractHttp11Protocol<?>) server.getTomcat().getConnector().getProtocolHandler())
.getRelaxedQueryChars()).isEqualTo("^|"));
}
@Test
void deduceUseForwardHeaders() {
this.environment.setProperty("DYNO", "-");
testRemoteIpValveConfigured();
}
@Test
void defaultUseForwardHeaders() {
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(factory.getEngineValves()).hasSize(0);
}
@Test
void forwardHeadersWhenStrategyIsNativeShouldConfigureValve() {
this.serverProperties.setForwardHeadersStrategy(ServerProperties.ForwardHeadersStrategy.NATIVE);
testRemoteIpValveConfigured();
}
@Test
void forwardHeadersWhenStrategyIsNoneShouldNotConfigureValve() {
this.environment.setProperty("DYNO", "-");
this.serverProperties.setForwardHeadersStrategy(ServerProperties.ForwardHeadersStrategy.NONE);
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(factory.getEngineValves()).hasSize(0);
}
@Test
void defaultRemoteIpValve() {
// Since 1.1.7 you need to specify at least the protocol
bind("server.tomcat.remoteip.protocol-header=X-Forwarded-Proto",
"server.tomcat.remoteip.remote-ip-header=X-Forwarded-For");
testRemoteIpValveConfigured();
}
@Test
void setUseNativeForwardHeadersStrategy() {
this.serverProperties.setForwardHeadersStrategy(ForwardHeadersStrategy.NATIVE);
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");
assertThat(remoteIpValve.getHostHeader()).isEqualTo("X-Forwarded-Host");
assertThat(remoteIpValve.getPortHeader()).isEqualTo("X-Forwarded-Port");
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
void defaultBackgroundProcessorDelay() {
TomcatWebServer server = customizeAndGetServer();
assertThat(server.getTomcat().getEngine().getBackgroundProcessorDelay()).isEqualTo(10);
}
@Test
void disableRemoteIpValve() {
bind("server.tomcat.remoteip.remote-ip-header=", "server.tomcat.remoteip.protocol-header=");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(factory.getEngineValves()).isEmpty();
}
@Test
void testCustomizeRejectIllegalHeader() {
bind("server.tomcat.reject-illegal-header=false");
customizeAndRunServer((server) -> assertThat(
((AbstractHttp11Protocol<?>) server.getTomcat().getConnector().getProtocolHandler())
.getRejectIllegalHeader()).isFalse());
}
@Test
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
void testCustomizeMinSpareThreads() {
bind("server.tomcat.threads.min-spare=10");
assertThat(this.serverProperties.getTomcat().getThreads().getMinSpare()).isEqualTo(10);
}
@Test
void customConnectionTimeout() {
bind("server.tomcat.connection-timeout=30s");
customizeAndRunServer((server) -> assertThat(
((AbstractProtocol<?>) server.getTomcat().getConnector().getProtocolHandler()).getConnectionTimeout())
.isEqualTo(30000));
}
@Test
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
void accessLogCanBeEnabled() {
bind("server.tomcat.accesslog.enabled=true");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(factory.getEngineValves()).hasSize(1);
assertThat(factory.getEngineValves()).first().isInstanceOf(AccessLogValve.class);
}
@Test
void accessLogFileDateFormatByDefault() {
bind("server.tomcat.accesslog.enabled=true");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()).getFileDateFormat())
.isEqualTo(".yyyy-MM-dd");
}
@Test
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
void accessLogIsBufferedByDefault() {
bind("server.tomcat.accesslog.enabled=true");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()).isBuffered()).isTrue();
}
@Test
void accessLogIsDisabledByDefault() {
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(factory.getEngineValves()).isEmpty();
}
@Test
void accessLogMaxDaysDefault() {
bind("server.tomcat.accesslog.enabled=true");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()).getMaxDays())
.isEqualTo(this.serverProperties.getTomcat().getAccesslog().getMaxDays());
}
@Test
void accessLogConditionCanBeSpecified() {
bind("server.tomcat.accesslog.enabled=true", "server.tomcat.accesslog.conditionIf=foo",
"server.tomcat.accesslog.conditionUnless=bar");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()).getConditionIf()).isEqualTo("foo");
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()).getConditionUnless())
.isEqualTo("bar");
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()).getCondition())
.describedAs("value of condition should equal conditionUnless - provided for backwards compatibility")
.isEqualTo("bar");
}
@Test
void accessLogEncodingIsNullWhenNotSpecified() {
bind("server.tomcat.accesslog.enabled=true");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()).getEncoding()).isNull();
}
@Test
void accessLogEncodingCanBeSpecified() {
bind("server.tomcat.accesslog.enabled=true", "server.tomcat.accesslog.encoding=UTF-8");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()).getEncoding()).isEqualTo("UTF-8");
}
@Test
void accessLogWithDefaultLocale() {
bind("server.tomcat.accesslog.enabled=true");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()).getLocale())
.isEqualTo(Locale.getDefault().toString());
}
@Test
void accessLogLocaleCanBeSpecified() {
String locale = "en_AU".equals(Locale.getDefault().toString()) ? "en_US" : "en_AU";
bind("server.tomcat.accesslog.enabled=true", "server.tomcat.accesslog.locale=" + locale);
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()).getLocale()).isEqualTo(locale);
}
@Test
void accessLogCheckExistsDefault() {
bind("server.tomcat.accesslog.enabled=true");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()).isCheckExists()).isFalse();
}
@Test
void accessLogCheckExistsSpecified() {
bind("server.tomcat.accesslog.enabled=true", "server.tomcat.accesslog.check-exists=true");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()).isCheckExists()).isTrue();
}
@Test
void accessLogMaxDaysCanBeRedefined() {
bind("server.tomcat.accesslog.enabled=true", "server.tomcat.accesslog.max-days=20");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()).getMaxDays()).isEqualTo(20);
}
@Test
void accessLogDoesNotUseIpv6CanonicalFormatByDefault() {
bind("server.tomcat.accesslog.enabled=true");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()).getIpv6Canonical()).isFalse();
}
@Test
void accessLogWithIpv6CanonicalSet() {
bind("server.tomcat.accesslog.enabled=true", "server.tomcat.accesslog.ipv6-canonical=true");
TomcatServletWebServerFactory factory = customizeAndGetFactory();
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()).getIpv6Canonical()).isTrue();
}
@Test
void ajpConnectorCanBeCustomized() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0);
factory.setProtocol("AJP/1.3");
factory.addConnectorCustomizers(
(connector) -> ((AbstractAjpProtocol<?>) connector.getProtocolHandler()).setSecretRequired(false));
this.customizer.customize(factory);
WebServer server = factory.getWebServer();
server.start();
server.stop();
}
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);
factory.setHttp2(this.serverProperties.getHttp2());
this.customizer.customize(factory);
return factory;
}
}