Search in sources :

Example 1 with ExampleServlet

use of org.springframework.boot.testsupport.web.servlet.ExampleServlet in project spring-boot by spring-projects.

the class SkipSslVerificationHttpRequestFactoryTests method getHttpsUrl.

private String getHttpsUrl() {
    TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0);
    factory.setSsl(getSsl("password", "classpath:test.jks"));
    this.webServer = factory.getWebServer(new ServletRegistrationBean<>(new ExampleServlet(), "/hello"));
    this.webServer.start();
    return "https://localhost:" + this.webServer.getPort() + "/hello";
}
Also used : TomcatServletWebServerFactory(org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory) ServletRegistrationBean(org.springframework.boot.web.servlet.ServletRegistrationBean) ExampleServlet(org.springframework.boot.testsupport.web.servlet.ExampleServlet)

Example 2 with ExampleServlet

use of org.springframework.boot.testsupport.web.servlet.ExampleServlet in project spring-boot by spring-projects.

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(org.springframework.boot.web.servlet.ServletRegistrationBean) HttpClient(org.apache.http.client.HttpClient) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) Ssl(org.springframework.boot.web.server.Ssl) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) SSLException(javax.net.ssl.SSLException) ExampleServlet(org.springframework.boot.testsupport.web.servlet.ExampleServlet) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 3 with ExampleServlet

use of org.springframework.boot.testsupport.web.servlet.ExampleServlet in project spring-boot by spring-projects.

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(org.springframework.boot.web.servlet.ServletRegistrationBean) ExampleServlet(org.springframework.boot.testsupport.web.servlet.ExampleServlet)

Example 4 with ExampleServlet

use of org.springframework.boot.testsupport.web.servlet.ExampleServlet in project spring-boot by spring-projects.

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(org.springframework.boot.web.servlet.ServletRegistrationBean) HttpClient(org.apache.http.client.HttpClient) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) ExampleServlet(org.springframework.boot.testsupport.web.servlet.ExampleServlet) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 5 with ExampleServlet

use of org.springframework.boot.testsupport.web.servlet.ExampleServlet in project spring-boot by spring-projects.

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(org.springframework.boot.web.servlet.ServletRegistrationBean) ExampleServlet(org.springframework.boot.testsupport.web.servlet.ExampleServlet)

Aggregations

ExampleServlet (org.springframework.boot.testsupport.web.servlet.ExampleServlet)14 ServletRegistrationBean (org.springframework.boot.web.servlet.ServletRegistrationBean)14 Test (org.junit.jupiter.api.Test)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)7 HttpClient (org.apache.http.client.HttpClient)6 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)6 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)6 TrustSelfSignedStrategy (org.apache.http.conn.ssl.TrustSelfSignedStrategy)5 Ssl (org.springframework.boot.web.server.Ssl)3 ServletRequest (jakarta.servlet.ServletRequest)2 ServletResponse (jakarta.servlet.ServletResponse)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)2 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)2 HttpSession (jakarta.servlet.http.HttpSession)1 File (java.io.File)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 SSLContext (javax.net.ssl.SSLContext)1