Search in sources :

Example 11 with ExampleServlet

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

the class AbstractServletWebServerFactoryTests method compressionWithoutContentSizeHeader.

@Test
void compressionWithoutContentSizeHeader() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    Compression compression = new Compression();
    compression.setEnabled(true);
    factory.setCompression(compression);
    this.webServer = factory.getWebServer(new ServletRegistrationBean<>(new ExampleServlet(false, true), "/hello"));
    this.webServer.start();
    TestGzipInputStreamFactory inputStreamFactory = new TestGzipInputStreamFactory();
    Map<String, InputStreamFactory> contentDecoderMap = Collections.singletonMap("gzip", inputStreamFactory);
    getResponse(getLocalUrl("/hello"), new HttpComponentsClientHttpRequestFactory(this.httpClientBuilder.get().setContentDecoderRegistry(contentDecoderMap).build()));
    assertThat(inputStreamFactory.wasCompressionUsed()).isTrue();
}
Also used : Compression(cn.taketoday.framework.web.server.Compression) ServletRegistrationBean(cn.taketoday.framework.web.servlet.ServletRegistrationBean) HttpComponentsClientHttpRequestFactory(cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory) InputStreamFactory(org.apache.http.client.entity.InputStreamFactory) ExampleServlet(cn.taketoday.test.web.servlet.ExampleServlet) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 12 with ExampleServlet

use of cn.taketoday.test.web.servlet.ExampleServlet in project today-framework 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)

Example 13 with ExampleServlet

use of cn.taketoday.test.web.servlet.ExampleServlet in project today-framework 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 14 with ExampleServlet

use of cn.taketoday.test.web.servlet.ExampleServlet in project today-framework 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 15 with ExampleServlet

use of cn.taketoday.test.web.servlet.ExampleServlet in project today-framework 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)

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