Search in sources :

Example 1 with ExampleServlet

use of cn.taketoday.test.web.servlet.ExampleServlet in project today-infrastructure by TAKETODAY.

the class AbstractServletWebServerFactoryTests method sslGetScheme.

@Test
void sslGetScheme() throws Exception {
    // gh-2232
    AbstractServletWebServerFactory factory = getFactory();
    factory.setSsl(getSsl(null, "password", "src/test/resources/test.jks"));
    this.webServer = factory.getWebServer(new ServletRegistrationBean<>(new ExampleServlet(true, false), "/hello"));
    this.webServer.start();
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
    assertThat(getResponse(getLocalUrl("https", "/hello"), requestFactory)).contains("scheme=https");
}
Also used : ServletRegistrationBean(cn.taketoday.framework.web.servlet.ServletRegistrationBean) HttpClient(org.apache.http.client.HttpClient) HttpComponentsClientHttpRequestFactory(cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) ExampleServlet(cn.taketoday.test.web.servlet.ExampleServlet) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 2 with ExampleServlet

use of cn.taketoday.test.web.servlet.ExampleServlet in project today-infrastructure by TAKETODAY.

the class AbstractServletWebServerFactoryTests method errorServletRegistration.

@SuppressWarnings("serial")
private ServletContextInitializer errorServletRegistration() {
    ServletRegistrationBean<ExampleServlet> bean = new ServletRegistrationBean<>(new ExampleServlet() {

        @Override
        public void service(ServletRequest request, ServletResponse response) {
            throw new RuntimeException("Planned");
        }
    }, "/bang");
    bean.setName("error");
    return bean;
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ServletRegistrationBean(cn.taketoday.framework.web.servlet.ServletRegistrationBean) ExampleServlet(cn.taketoday.test.web.servlet.ExampleServlet)

Example 3 with ExampleServlet

use of cn.taketoday.test.web.servlet.ExampleServlet in project today-infrastructure by TAKETODAY.

the class AbstractServletWebServerFactoryTests method sslKeyAlias.

@Test
void sslKeyAlias() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    Ssl ssl = getSsl(null, "password", "test-alias", "src/test/resources/test.jks");
    factory.setSsl(ssl);
    ServletRegistrationBean<ExampleServlet> registration = new ServletRegistrationBean<>(new ExampleServlet(true, false), "/hello");
    this.webServer = factory.getWebServer(registration);
    this.webServer.start();
    TrustStrategy trustStrategy = new SerialNumberValidatingTrustSelfSignedStrategy("3a3aaec8");
    SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, trustStrategy).build();
    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(new SSLConnectionSocketFactory(sslContext)).build();
    String response = getResponse(getLocalUrl("https", "/hello"), new HttpComponentsClientHttpRequestFactory(httpClient));
    assertThat(response).contains("scheme=https");
}
Also used : TrustStrategy(org.apache.http.ssl.TrustStrategy) SSLContext(javax.net.ssl.SSLContext) Ssl(cn.taketoday.framework.web.server.Ssl) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ExampleServlet(cn.taketoday.test.web.servlet.ExampleServlet) ServletRegistrationBean(cn.taketoday.framework.web.servlet.ServletRegistrationBean) HttpClient(org.apache.http.client.HttpClient) HttpComponentsClientHttpRequestFactory(cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 4 with ExampleServlet

use of cn.taketoday.test.web.servlet.ExampleServlet in project today-infrastructure by TAKETODAY.

the class AbstractServletWebServerFactoryTests method testRestrictedSSLProtocolsAndCipherSuites.

protected void testRestrictedSSLProtocolsAndCipherSuites(String[] protocols, String[] ciphers) throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    factory.setSsl(getSsl(null, "password", "src/test/resources/restricted.jks", null, protocols, ciphers));
    this.webServer = factory.getWebServer(new ServletRegistrationBean<>(new ExampleServlet(true, false), "/hello"));
    this.webServer.start();
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
    HttpClient httpClient = this.httpClientBuilder.get().setSSLSocketFactory(socketFactory).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
    assertThat(getResponse(getLocalUrl("https", "/hello"), requestFactory)).contains("scheme=https");
}
Also used : ServletRegistrationBean(cn.taketoday.framework.web.servlet.ServletRegistrationBean) HttpClient(org.apache.http.client.HttpClient) HttpComponentsClientHttpRequestFactory(cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) ExampleServlet(cn.taketoday.test.web.servlet.ExampleServlet) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy)

Example 5 with ExampleServlet

use of cn.taketoday.test.web.servlet.ExampleServlet in project today-infrastructure by TAKETODAY.

the class AbstractServletWebServerFactoryTests method serverHeaderCanBeCustomizedWhenUsingSsl.

@Test
void serverHeaderCanBeCustomizedWhenUsingSsl() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    factory.setServerHeader("MyServer");
    factory.setSsl(getSsl(null, "password", "src/test/resources/test.jks"));
    this.webServer = factory.getWebServer(new ServletRegistrationBean<>(new ExampleServlet(true, false), "/hello"));
    this.webServer.start();
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
    HttpClient httpClient = this.httpClientBuilder.get().setSSLSocketFactory(socketFactory).setRetryHandler(new DefaultHttpRequestRetryHandler(10, false)).build();
    ClientHttpResponse response = getClientResponse(getLocalUrl("https", "/hello"), HttpMethod.GET, new HttpComponentsClientHttpRequestFactory(httpClient));
    assertThat(response.getHeaders().get("Server")).containsExactly("MyServer");
}
Also used : ServletRegistrationBean(cn.taketoday.framework.web.servlet.ServletRegistrationBean) HttpClient(org.apache.http.client.HttpClient) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) HttpComponentsClientHttpRequestFactory(cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) ClientHttpResponse(cn.taketoday.http.client.ClientHttpResponse) ExampleServlet(cn.taketoday.test.web.servlet.ExampleServlet) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Aggregations

ServletRegistrationBean (cn.taketoday.framework.web.servlet.ServletRegistrationBean)26 ExampleServlet (cn.taketoday.test.web.servlet.ExampleServlet)26 Test (org.junit.jupiter.api.Test)16 HttpComponentsClientHttpRequestFactory (cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 HttpClient (org.apache.http.client.HttpClient)12 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)12 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)12 TrustSelfSignedStrategy (org.apache.http.conn.ssl.TrustSelfSignedStrategy)10 Ssl (cn.taketoday.framework.web.server.Ssl)6 ClientHttpResponse (cn.taketoday.http.client.ClientHttpResponse)4 ServletRequest (jakarta.servlet.ServletRequest)4 ServletResponse (jakarta.servlet.ServletResponse)4 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)4 Compression (cn.taketoday.framework.web.server.Compression)2 ErrorPage (cn.taketoday.framework.web.server.ErrorPage)2 AbstractServletWebServerFactory (cn.taketoday.framework.web.servlet.server.AbstractServletWebServerFactory)2 HttpSession (jakarta.servlet.http.HttpSession)2 File (java.io.File)2