Search in sources :

Example 21 with ExampleServlet

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

the class AbstractServletWebServerFactoryTests method serverHeaderIsDisabledByDefaultWhenUsingSsl.

@Test
void serverHeaderIsDisabledByDefaultWhenUsingSsl() throws Exception {
    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 = this.httpClientBuilder.get().setSSLSocketFactory(socketFactory).build();
    ClientHttpResponse response = getClientResponse(getLocalUrl("https", "/hello"), HttpMethod.GET, new HttpComponentsClientHttpRequestFactory(httpClient));
    assertThat(response.getHeaders().get("Server")).isNullOrEmpty();
}
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) 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)

Example 22 with ExampleServlet

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

the class AbstractServletWebServerFactoryTests method sslDisabled.

@Test
void sslDisabled() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    Ssl ssl = getSsl(null, "password", "classpath:test.jks");
    ssl.setEnabled(false);
    factory.setSsl(ssl);
    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);
    assertThatExceptionOfType(SSLException.class).isThrownBy(() -> getResponse(getLocalUrl("https", "/hello"), requestFactory));
}
Also used : ServletRegistrationBean(cn.taketoday.framework.web.servlet.ServletRegistrationBean) HttpClient(org.apache.http.client.HttpClient) HttpComponentsClientHttpRequestFactory(cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory) Ssl(cn.taketoday.framework.web.server.Ssl) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) SSLException(javax.net.ssl.SSLException) 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 23 with ExampleServlet

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

the class AbstractServletWebServerFactoryTests method assertForwardHeaderIsUsed.

protected void assertForwardHeaderIsUsed(ServletWebServerFactory factory) throws IOException, URISyntaxException {
    this.webServer = factory.getWebServer(new ServletRegistrationBean<>(new ExampleServlet(true, false), "/hello"));
    this.webServer.start();
    assertThat(getResponse(getLocalUrl("/hello"), "X-Forwarded-For:140.211.11.130")).contains("remoteaddr=140.211.11.130");
}
Also used : ServletRegistrationBean(cn.taketoday.framework.web.servlet.ServletRegistrationBean) ExampleServlet(cn.taketoday.test.web.servlet.ExampleServlet)

Example 24 with ExampleServlet

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

the class AbstractServletWebServerFactoryTests method sslWithInvalidAliasFailsDuringStartup.

@Test
void sslWithInvalidAliasFailsDuringStartup() {
    AbstractServletWebServerFactory factory = getFactory();
    Ssl ssl = getSsl(null, "password", "test-alias-404", "src/test/resources/test.jks");
    factory.setSsl(ssl);
    ServletRegistrationBean<ExampleServlet> registration = new ServletRegistrationBean<>(new ExampleServlet(true, false), "/hello");
    ThrowingCallable call = () -> factory.getWebServer(registration).start();
    assertThatSslWithInvalidAliasCallFails(call);
}
Also used : ServletRegistrationBean(cn.taketoday.framework.web.servlet.ServletRegistrationBean) ThrowingCallable(org.assertj.core.api.ThrowableAssert.ThrowingCallable) Ssl(cn.taketoday.framework.web.server.Ssl) ExampleServlet(cn.taketoday.test.web.servlet.ExampleServlet) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 25 with ExampleServlet

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

the class AbstractServletWebServerFactoryTests method sessionServletRegistration.

protected final ServletContextInitializer sessionServletRegistration() {
    ServletRegistrationBean<ExampleServlet> bean = new ServletRegistrationBean<>(new ExampleServlet() {

        @Override
        public void service(ServletRequest request, ServletResponse response) throws IOException {
            HttpSession session = ((HttpServletRequest) request).getSession(true);
            long value = System.currentTimeMillis();
            Object existing = session.getAttribute("boot");
            session.setAttribute("boot", value);
            PrintWriter writer = response.getWriter();
            writer.append(String.valueOf(existing)).append(":").append(String.valueOf(value));
        }
    }, "/session");
    bean.setName("session");
    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) HttpSession(jakarta.servlet.http.HttpSession) Assertions.assertThatIOException(org.assertj.core.api.Assertions.assertThatIOException) IOException(java.io.IOException) ExampleServlet(cn.taketoday.test.web.servlet.ExampleServlet) PrintWriter(java.io.PrintWriter)

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