Samples cleanup

- Modifying dependencies to starter-web with tomcat exclusion plus
  alternative servlet container instead of manual dependency on
  spring-webmvc as it is the preferrable way to use alternative servlet
  container
- Previously RestTemplate with ssl was configured manually in tests - now
  it rellies on autoconfiguration - changed this for multi-connector test
  and added test to ensure that ssl autoconfiguration is working
- Most samples with alterntative servlet containers used some kind of
  service reading property and returning default since it wasn't
  configured - removed it, since it is not specific to using alternative
  servlet containers.

See gh-10548
This commit is contained in:
Ivan Sopov
2017-11-08 16:13:15 +03:00
committed by Stephane Nicoll
parent 792de8f42a
commit d8fa71bc97
63 changed files with 91 additions and 377 deletions

View File

@@ -16,18 +16,8 @@
package sample.tomcat.multiconnector;
import java.io.IOException;
import java.net.HttpURLConnection;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.catalina.Service;
import org.apache.catalina.connector.Connector;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import sample.tomcat.multiconnector.SampleTomcatTwoConnectorsApplicationTests.Ports;
@@ -36,18 +26,16 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
import org.springframework.boot.web.server.AbstractConfigurableWebServerFactory;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.Assertions.assertThat;
@@ -59,88 +47,37 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
@Import(Ports.class)
public class SampleTomcatTwoConnectorsApplicationTests {
@LocalServerPort
private int port;
@Autowired
private ApplicationContext context;
@BeforeClass
public static void setUp() {
try {
// setup ssl context to ignore certificate errors
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain,
String authType) throws java.security.cert.CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain,
String authType) throws java.security.cert.CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLContext.setDefault(ctx);
}
catch (Exception ex) {
ex.printStackTrace();
}
private Ports ports;
@Autowired
private TestRestTemplate restTemplate;
@Autowired
private AbstractConfigurableWebServerFactory webServerFactory;
@Test
public void testSsl() {
assertThat(this.webServerFactory.getSsl().isEnabled()).isTrue();
}
@Test
public void testHello() throws Exception {
RestTemplate template = new RestTemplate();
final MySimpleClientHttpRequestFactory factory = new MySimpleClientHttpRequestFactory(
(hostname, session) -> true);
template.setRequestFactory(factory);
Ports ports = this.context.getBean(Ports.class);
assertThat(ports.getHttpsPort()).isEqualTo(this.port);
assertThat(ports.getHttpPort()).isNotEqualTo(this.port);
ResponseEntity<String> entity = template.getForEntity(
"http://localhost:" + ports.getHttpPort() + "/hello", String.class);
assertThat(this.ports.getHttpsPort()).isEqualTo(this.port);
assertThat(this.ports.getHttpPort()).isNotEqualTo(this.port);
ResponseEntity<String> entity = this.restTemplate.getForEntity(
"http://localhost:" + this.ports.getHttpPort() + "/hello", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo("hello");
ResponseEntity<String> httpsEntity = template
ResponseEntity<String> httpsEntity = this.restTemplate
.getForEntity("https://localhost:" + this.port + "/hello", String.class);
assertThat(httpsEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(httpsEntity.getBody()).isEqualTo("hello");
}
/**
* Http Request Factory for ignoring SSL hostname errors. Not for production use!
*/
class MySimpleClientHttpRequestFactory extends SimpleClientHttpRequestFactory {
private final HostnameVerifier verifier;
public MySimpleClientHttpRequestFactory(final HostnameVerifier verifier) {
this.verifier = verifier;
}
@Override
protected void prepareConnection(final HttpURLConnection connection,
final String httpMethod) throws IOException {
if (connection instanceof HttpsURLConnection) {
((HttpsURLConnection) connection).setHostnameVerifier(this.verifier);
}
super.prepareConnection(connection, httpMethod);
}
}
@TestConfiguration
static class Ports implements ApplicationListener<WebServerInitializedEvent> {