21
21
import java .util .HashMap ;
22
22
import java .util .Map ;
23
23
24
+ import org .apache .catalina .Valve ;
25
+ import org .apache .catalina .valves .RemoteIpValve ;
24
26
import org .junit .Test ;
25
27
import org .springframework .beans .MutablePropertyValues ;
26
28
import org .springframework .boot .bind .RelaxedDataBinder ;
27
29
import org .springframework .boot .context .embedded .ConfigurableEmbeddedServletContainer ;
30
+ import org .springframework .boot .context .embedded .tomcat .TomcatEmbeddedServletContainerFactory ;
28
31
32
+ import static org .hamcrest .core .IsInstanceOf .instanceOf ;
29
33
import static org .junit .Assert .assertEquals ;
30
34
import static org .junit .Assert .assertFalse ;
35
+ import static org .junit .Assert .assertThat ;
31
36
import static org .mockito .Mockito .mock ;
32
37
import static org .mockito .Mockito .never ;
33
38
import static org .mockito .Mockito .verify ;
37
42
*
38
43
* @author Dave Syer
39
44
* @author Stephane Nicoll
45
+ * @author Andy Wilkinson
40
46
*/
41
47
public class ServerPropertiesTests {
42
48
@@ -84,13 +90,15 @@ public void testTomcatBinding() throws Exception {
84
90
map .put ("server.tomcat.access_log_pattern" , "%h %t '%r' %s %b" );
85
91
map .put ("server.tomcat.protocol_header" , "X-Forwarded-Protocol" );
86
92
map .put ("server.tomcat.remote_ip_header" , "Remote-Ip" );
87
- new RelaxedDataBinder ( this . properties , "server" ). bind ( new MutablePropertyValues (
88
- map ) );
93
+ map . put ( "server.tomcat.internal_proxies" , "10 \\ . \\ d{1,3} \\ . \\ d{1,3} \\ . \\ d{1,3}" );
94
+ bindProperties ( map );
89
95
assertEquals ("%h %t '%r' %s %b" , this .properties .getTomcat ()
90
96
.getAccessLogPattern ());
91
97
assertEquals ("Remote-Ip" , this .properties .getTomcat ().getRemoteIpHeader ());
92
98
assertEquals ("X-Forwarded-Protocol" , this .properties .getTomcat ()
93
99
.getProtocolHeader ());
100
+ assertEquals ("10\\ .\\ d{1,3}\\ .\\ d{1,3}\\ .\\ d{1,3}" , this .properties .getTomcat ()
101
+ .getInternalProxies ());
94
102
}
95
103
96
104
@ Test
@@ -112,18 +120,74 @@ public void testCustomizeTomcatPort() throws Exception {
112
120
public void testCustomizeUriEncoding () throws Exception {
113
121
Map <String , String > map = new HashMap <String , String >();
114
122
map .put ("server.tomcat.uriEncoding" , "US-ASCII" );
115
- new RelaxedDataBinder (this .properties , "server" ).bind (new MutablePropertyValues (
116
- map ));
123
+ bindProperties (map );
117
124
assertEquals ("US-ASCII" , this .properties .getTomcat ().getUriEncoding ());
118
125
}
119
126
120
127
@ Test
121
128
public void testCustomizeTomcatHeaderSize () throws Exception {
122
129
Map <String , String > map = new HashMap <String , String >();
123
130
map .put ("server.tomcat.maxHttpHeaderSize" , "9999" );
131
+ bindProperties (map );
132
+ assertEquals (9999 , this .properties .getTomcat ().getMaxHttpHeaderSize ());
133
+ }
134
+
135
+ @ Test
136
+ public void disableTomcatRemoteIpValve () throws Exception {
137
+ Map <String , String > map = new HashMap <String , String >();
138
+ map .put ("server.tomcat.remote_ip_header" , "" );
139
+ map .put ("server.tomcat.protocol_header" , "" );
140
+ bindProperties (map );
141
+
142
+ TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory ();
143
+ this .properties .customize (container );
144
+
145
+ assertEquals (0 , container .getValves ().size ());
146
+ }
147
+
148
+ @ Test
149
+ public void defaultTomcatRemoteIpValve () throws Exception {
150
+ TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory ();
151
+ this .properties .customize (container );
152
+
153
+ assertEquals (1 , container .getValves ().size ());
154
+ Valve valve = container .getValves ().iterator ().next ();
155
+ assertThat (valve , instanceOf (RemoteIpValve .class ));
156
+ RemoteIpValve remoteIpValve = (RemoteIpValve ) valve ;
157
+ assertEquals ("x-forwarded-proto" , remoteIpValve .getProtocolHeader ());
158
+ assertEquals ("x-forwarded-for" , remoteIpValve .getRemoteIpHeader ());
159
+
160
+ String expectedInternalProxies = "10\\ .\\ d{1,3}\\ .\\ d{1,3}\\ .\\ d{1,3}|" // 10/8
161
+ + "192\\ .168\\ .\\ d{1,3}\\ .\\ d{1,3}|" // 192.168/16
162
+ + "169\\ .254\\ .\\ d{1,3}\\ .\\ d{1,3}|" // 169.254/16
163
+ + "127\\ .\\ d{1,3}\\ .\\ d{1,3}\\ .\\ d{1,3}" ; // 127/8
164
+
165
+ assertEquals (expectedInternalProxies , remoteIpValve .getInternalProxies ());
166
+ }
167
+
168
+ @ Test
169
+ public void customTomcatRemoteIpValve () throws Exception {
170
+ Map <String , String > map = new HashMap <String , String >();
171
+ map .put ("server.tomcat.remote_ip_header" , "x-my-remote-ip-header" );
172
+ map .put ("server.tomcat.protocol_header" , "x-my-protocol-header" );
173
+ map .put ("server.tomcat.internal_proxies" , "192.168.0.1" );
174
+ bindProperties (map );
175
+
176
+ TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory ();
177
+ this .properties .customize (container );
178
+
179
+ assertEquals (1 , container .getValves ().size ());
180
+ Valve valve = container .getValves ().iterator ().next ();
181
+ assertThat (valve , instanceOf (RemoteIpValve .class ));
182
+ RemoteIpValve remoteIpValve = (RemoteIpValve ) valve ;
183
+ assertEquals ("x-my-protocol-header" , remoteIpValve .getProtocolHeader ());
184
+ assertEquals ("x-my-remote-ip-header" , remoteIpValve .getRemoteIpHeader ());
185
+ assertEquals ("192.168.0.1" , remoteIpValve .getInternalProxies ());
186
+ }
187
+
188
+ private void bindProperties (Map <String , String > map ) {
124
189
new RelaxedDataBinder (this .properties , "server" ).bind (new MutablePropertyValues (
125
190
map ));
126
- assertEquals (9999 , this .properties .getTomcat ().getMaxHttpHeaderSize ());
127
191
}
128
192
129
193
}
0 commit comments