17
17
package org .springframework .boot .autoconfigure .web ;
18
18
19
19
import java .io .File ;
20
+ import java .io .IOException ;
20
21
import java .net .InetAddress ;
22
+ import java .net .URI ;
21
23
import java .nio .charset .Charset ;
22
24
import java .util .Collections ;
23
25
import java .util .EnumSet ;
29
31
import javax .servlet .ServletException ;
30
32
import javax .servlet .SessionCookieConfig ;
31
33
import javax .servlet .SessionTrackingMode ;
34
+ import javax .servlet .http .HttpServlet ;
35
+ import javax .servlet .http .HttpServletRequest ;
36
+ import javax .servlet .http .HttpServletResponse ;
32
37
38
+ import io .undertow .UndertowOptions ;
33
39
import org .apache .catalina .Context ;
34
40
import org .apache .catalina .Valve ;
41
+ import org .apache .catalina .connector .Connector ;
35
42
import org .apache .catalina .core .StandardContext ;
43
+ import org .apache .catalina .core .StandardEngine ;
36
44
import org .apache .catalina .startup .Tomcat ;
37
45
import org .apache .catalina .valves .AccessLogValve ;
38
46
import org .apache .catalina .valves .ErrorReportValve ;
39
47
import org .apache .catalina .valves .RemoteIpValve ;
40
48
import org .apache .coyote .AbstractProtocol ;
49
+ import org .eclipse .jetty .server .HttpChannel ;
50
+ import org .eclipse .jetty .server .Request ;
41
51
import org .junit .Before ;
42
52
import org .junit .Test ;
43
53
import org .mockito .ArgumentCaptor ;
49
59
import org .springframework .boot .context .embedded .AbstractEmbeddedServletContainerFactory ;
50
60
import org .springframework .boot .context .embedded .ConfigurableEmbeddedServletContainer ;
51
61
import org .springframework .boot .context .embedded .EmbeddedServletContainer ;
62
+ import org .springframework .boot .context .embedded .jetty .JettyEmbeddedServletContainer ;
52
63
import org .springframework .boot .context .embedded .jetty .JettyEmbeddedServletContainerFactory ;
53
64
import org .springframework .boot .context .embedded .tomcat .TomcatEmbeddedServletContainer ;
54
65
import org .springframework .boot .context .embedded .tomcat .TomcatEmbeddedServletContainerFactory ;
55
66
import org .springframework .boot .context .embedded .undertow .UndertowEmbeddedServletContainerFactory ;
56
67
import org .springframework .boot .web .servlet .ServletContextInitializer ;
68
+ import org .springframework .http .HttpEntity ;
69
+ import org .springframework .http .HttpHeaders ;
70
+ import org .springframework .http .MediaType ;
71
+ import org .springframework .http .client .ClientHttpResponse ;
57
72
import org .springframework .mock .env .MockEnvironment ;
73
+ import org .springframework .util .LinkedMultiValueMap ;
74
+ import org .springframework .util .MultiValueMap ;
75
+ import org .springframework .web .client .ResponseErrorHandler ;
76
+ import org .springframework .web .client .RestTemplate ;
58
77
59
78
import static org .assertj .core .api .Assertions .assertThat ;
60
79
import static org .mockito .Matchers .anyBoolean ;
@@ -256,13 +275,6 @@ public void errorReportValveIsConfiguredToNotReportStackTraces() {
256
275
}
257
276
}
258
277
259
- @ Test
260
- public void redirectContextRootIsNotConfiguredByDefault () throws Exception {
261
- bindProperties (new HashMap <String , String >());
262
- ServerProperties .Tomcat tomcat = this .properties .getTomcat ();
263
- assertThat (tomcat .getRedirectContextRoot ()).isNull ();
264
- }
265
-
266
278
@ Test
267
279
public void redirectContextRootCanBeConfigured () throws Exception {
268
280
Map <String , String > map = new HashMap <String , String >();
@@ -274,6 +286,10 @@ public void redirectContextRootCanBeConfigured() throws Exception {
274
286
Context context = (Context ) ((TomcatEmbeddedServletContainer ) factory
275
287
.getEmbeddedServletContainer ()).getTomcat ().getHost ().findChildren ()[0 ];
276
288
assertThat (context .getMapperContextRootRedirectEnabled ()).isTrue ();
289
+ this .properties .customize (factory );
290
+ context = (Context ) ((TomcatEmbeddedServletContainer ) factory
291
+ .getEmbeddedServletContainer ()).getTomcat ().getHost ().findChildren ()[0 ];
292
+ assertThat (context .getMapperContextRootRedirectEnabled ()).isFalse ();
277
293
}
278
294
279
295
@ Test
@@ -517,7 +533,7 @@ public void defaultTomcatBackgroundProcessorDelay() throws Exception {
517
533
this .properties .customize (factory );
518
534
EmbeddedServletContainer container = factory .getEmbeddedServletContainer ();
519
535
assertThat (((TomcatEmbeddedServletContainer ) container ).getTomcat ().getEngine ()
520
- .getBackgroundProcessorDelay ()).isEqualTo (30 );
536
+ .getBackgroundProcessorDelay ()).isEqualTo (10 );
521
537
container .stop ();
522
538
}
523
539
@@ -819,6 +835,158 @@ public void skipNullElementsForUndertow() throws Exception {
819
835
verify (container , never ()).setAccessLogEnabled (anyBoolean ());
820
836
}
821
837
838
+ @ Test
839
+ public void tomcatAcceptCountMatchesProtocolDefault () throws Exception {
840
+ assertThat (this .properties .getTomcat ().getAcceptCount ())
841
+ .isEqualTo (getDefaultProtocol ().getAcceptCount ());
842
+ }
843
+
844
+ @ Test
845
+ public void tomcatMaxConnectionsMatchesProtocolDefault () throws Exception {
846
+ assertThat (this .properties .getTomcat ().getMaxConnections ())
847
+ .isEqualTo (getDefaultProtocol ().getMaxConnections ());
848
+ }
849
+
850
+ @ Test
851
+ public void tomcatMaxThreadsMatchesProtocolDefault () throws Exception {
852
+ assertThat (this .properties .getTomcat ().getMaxThreads ())
853
+ .isEqualTo (getDefaultProtocol ().getMaxThreads ());
854
+ }
855
+
856
+ @ Test
857
+ public void tomcatMinSpareThreadsMatchesProtocolDefault () throws Exception {
858
+ assertThat (this .properties .getTomcat ().getMinSpareThreads ())
859
+ .isEqualTo (getDefaultProtocol ().getMinSpareThreads ());
860
+ }
861
+
862
+ @ Test
863
+ public void tomcatMaxHttpPostSizeMatchesConnectorDefault () throws Exception {
864
+ assertThat (this .properties .getTomcat ().getMaxHttpPostSize ())
865
+ .isEqualTo (getDefaultConnector ().getMaxPostSize ());
866
+ }
867
+
868
+ @ Test
869
+ public void tomcatBackgroundProcessorDelayMatchesEngineDefault () {
870
+ assertThat (this .properties .getTomcat ().getBackgroundProcessorDelay ())
871
+ .isEqualTo (new StandardEngine ().getBackgroundProcessorDelay ());
872
+ }
873
+
874
+ @ Test
875
+ public void tomcatUriEncodingMatchesConnectorDefault () throws Exception {
876
+ assertThat (this .properties .getTomcat ().getUriEncoding ().name ())
877
+ .isEqualTo (getDefaultConnector ().getURIEncoding ());
878
+ }
879
+
880
+ @ Test
881
+ public void tomcatRedirectContextRootMatchesDefault () {
882
+ assertThat (this .properties .getTomcat ().getRedirectContextRoot ())
883
+ .isEqualTo (new StandardContext ().getMapperContextRootRedirectEnabled ());
884
+ }
885
+
886
+ @ Test
887
+ public void tomcatAccessLogRenameOnRotateMatchesDefault () {
888
+ assertThat (this .properties .getTomcat ().getAccesslog ().isRenameOnRotate ())
889
+ .isEqualTo (new AccessLogValve ().isRenameOnRotate ());
890
+ }
891
+
892
+ @ Test
893
+ public void tomcatAccessLogRequestAttributesEnabledMatchesDefault () {
894
+ assertThat (
895
+ this .properties .getTomcat ().getAccesslog ().isRequestAttributesEnabled ())
896
+ .isEqualTo (new AccessLogValve ().getRequestAttributesEnabled ());
897
+ }
898
+
899
+ @ Test
900
+ public void jettyMaxHttpPostSizeMatchesDefault () throws Exception {
901
+ JettyEmbeddedServletContainerFactory jettyFactory = new JettyEmbeddedServletContainerFactory (
902
+ 0 );
903
+ JettyEmbeddedServletContainer jetty = (JettyEmbeddedServletContainer ) jettyFactory
904
+ .getEmbeddedServletContainer (new ServletContextInitializer () {
905
+
906
+ @ Override
907
+ public void onStartup (ServletContext servletContext )
908
+ throws ServletException {
909
+ servletContext .addServlet ("formPost" , new HttpServlet () {
910
+
911
+ @ Override
912
+ protected void doPost (HttpServletRequest req ,
913
+ HttpServletResponse resp )
914
+ throws ServletException , IOException {
915
+ req .getParameterMap ();
916
+ }
917
+
918
+ }).addMapping ("/form" );
919
+ }
920
+
921
+ });
922
+ jetty .start ();
923
+ org .eclipse .jetty .server .Connector connector = jetty .getServer ()
924
+ .getConnectors ()[0 ];
925
+ final AtomicReference <Throwable > failure = new AtomicReference <Throwable >();
926
+ connector .addBean (new HttpChannel .Listener () {
927
+
928
+ @ Override
929
+ public void onDispatchFailure (Request request , Throwable ex ) {
930
+ failure .set (ex );
931
+ }
932
+
933
+ });
934
+ try {
935
+ RestTemplate template = new RestTemplate ();
936
+ template .setErrorHandler (new ResponseErrorHandler () {
937
+
938
+ @ Override
939
+ public boolean hasError (ClientHttpResponse response ) throws IOException {
940
+ return false ;
941
+ }
942
+
943
+ @ Override
944
+ public void handleError (ClientHttpResponse response ) throws IOException {
945
+
946
+ }
947
+
948
+ });
949
+ HttpHeaders headers = new HttpHeaders ();
950
+ headers .setContentType (MediaType .APPLICATION_FORM_URLENCODED );
951
+ MultiValueMap <String , Object > body = new LinkedMultiValueMap <String , Object >();
952
+ StringBuilder data = new StringBuilder ();
953
+ for (int i = 0 ; i < 250000 ; i ++) {
954
+ data .append ("a" );
955
+ }
956
+ body .add ("data" , data .toString ());
957
+ HttpEntity <MultiValueMap <String , Object >> entity = new HttpEntity <MultiValueMap <String , Object >>(
958
+ body , headers );
959
+ template .postForEntity (
960
+ URI .create ("http://localhost:" + jetty .getPort () + "/form" ), entity ,
961
+ Void .class );
962
+ assertThat (failure .get ()).isNotNull ();
963
+ String message = failure .get ().getCause ().getMessage ();
964
+ int defaultMaxPostSize = Integer
965
+ .valueOf (message .substring (message .lastIndexOf (' ' )).trim ());
966
+ assertThat (this .properties .getJetty ().getMaxHttpPostSize ())
967
+ .isEqualTo (defaultMaxPostSize );
968
+ }
969
+ finally {
970
+ jetty .stop ();
971
+ }
972
+ }
973
+
974
+ @ Test
975
+ public void undertowMaxHttpPostSizeMatchesDefault () {
976
+ assertThat (this .properties .getUndertow ().getMaxHttpPostSize ())
977
+ .isEqualTo (UndertowOptions .DEFAULT_MAX_ENTITY_SIZE );
978
+ }
979
+
980
+ private Connector getDefaultConnector () throws Exception {
981
+ return new Connector (TomcatEmbeddedServletContainerFactory .DEFAULT_PROTOCOL );
982
+ }
983
+
984
+ private AbstractProtocol <?> getDefaultProtocol () throws Exception {
985
+ return (AbstractProtocol <?>) Class
986
+ .forName (TomcatEmbeddedServletContainerFactory .DEFAULT_PROTOCOL )
987
+ .newInstance ();
988
+ }
989
+
822
990
private void bindProperties (Map <String , String > map ) {
823
991
new RelaxedDataBinder (this .properties , "server" )
824
992
.bind (new MutablePropertyValues (map ));
0 commit comments