Merge pull request #7425 from PostalBear/1.4.x

* pr/7425:
  Add additional Tomcat timeout test
  Fix connector used to configure connection timeout
pull/7462/head
Phillip Webb 8 years ago
commit 288398b8b7

@ -809,14 +809,20 @@ public class ServerProperties
} }
private void customizeConnectionTimeout( private void customizeConnectionTimeout(
TomcatEmbeddedServletContainerFactory factory, int connectionTimeout) { TomcatEmbeddedServletContainerFactory factory,
for (Connector connector : factory.getAdditionalTomcatConnectors()) { final int connectionTimeout) {
if (connector.getProtocolHandler() instanceof AbstractProtocol) { factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
AbstractProtocol<?> handler = (AbstractProtocol<?>) connector
.getProtocolHandler(); @Override
handler.setConnectionTimeout(connectionTimeout); public void customize(Connector connector) {
ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractProtocol) {
AbstractProtocol<?> protocol = (AbstractProtocol<?>) handler;
protocol.setConnectionTimeout(connectionTimeout);
}
} }
}
});
} }
private void customizeRemoteIpValve(ServerProperties properties, private void customizeRemoteIpValve(ServerProperties properties,

@ -111,6 +111,14 @@ public class ServerPropertiesTests {
assertThat(this.properties.getServerHeader()).isEqualTo("Custom Server"); assertThat(this.properties.getServerHeader()).isEqualTo("Custom Server");
} }
@Test
public void testConnectionTimeout() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("server.connection-timeout", "60000");
bindProperties(map);
assertThat(this.properties.getConnectionTimeout()).isEqualTo(60000);
}
@Test @Test
public void testServletPathAsMapping() throws Exception { public void testServletPathAsMapping() throws Exception {
RelaxedDataBinder binder = new RelaxedDataBinder(this.properties, "server"); RelaxedDataBinder binder = new RelaxedDataBinder(this.properties, "server");

@ -1,2 +1,3 @@
server.compression.enabled: true server.compression.enabled: true
server.compression.min-response-size: 1 server.compression.min-response-size: 1
server.connection-timeout=5000

@ -20,13 +20,18 @@ import java.io.ByteArrayInputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import org.apache.coyote.AbstractProtocol;
import org.apache.coyote.ProtocolHandler;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
@ -52,6 +57,9 @@ public class SampleTomcatApplicationTests {
@Autowired @Autowired
private TestRestTemplate restTemplate; private TestRestTemplate restTemplate;
@Autowired
private ApplicationContext applicationContext;
@Test @Test
public void testHome() throws Exception { public void testHome() throws Exception {
ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class); ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
@ -78,4 +86,15 @@ public class SampleTomcatApplicationTests {
} }
} }
@Test
public void testTimeout() throws Exception {
EmbeddedWebApplicationContext context = (EmbeddedWebApplicationContext) this.applicationContext;
TomcatEmbeddedServletContainer embeddedServletContainer = (TomcatEmbeddedServletContainer) context
.getEmbeddedServletContainer();
ProtocolHandler protocolHandler = embeddedServletContainer.getTomcat()
.getConnector().getProtocolHandler();
int timeout = ((AbstractProtocol<?>) protocolHandler).getConnectionTimeout();
assertThat(timeout).isEqualTo(5000);
}
} }

Loading…
Cancel
Save