Search in sources :

Example 1 with ClientHttpResponse

use of cn.taketoday.http.client.ClientHttpResponse in project today-infrastructure by TAKETODAY.

the class AbstractServletWebServerFactoryTests method sessionCookieSameSiteAttributeCanBeConfiguredAndOnlyAffectsSessionCookies.

@ParameterizedTest
@EnumSource(SameSite.class)
void sessionCookieSameSiteAttributeCanBeConfiguredAndOnlyAffectsSessionCookies(SameSite sameSite) throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    factory.getSession().getCookie().setSameSite(sameSite);
    factory.addInitializers(new ServletRegistrationBean<>(new CookieServlet(false), "/"));
    this.webServer = factory.getWebServer();
    this.webServer.start();
    ClientHttpResponse clientResponse = getClientResponse(getLocalUrl("/"));
    List<String> setCookieHeaders = clientResponse.getHeaders().get("Set-Cookie");
    assertThat(setCookieHeaders).satisfiesExactlyInAnyOrder((header) -> assertThat(header).contains("JSESSIONID").contains("SameSite=" + sameSite.attributeValue()), (header) -> assertThat(header).contains("test=test").doesNotContain("SameSite"));
}
Also used : ClientHttpResponse(cn.taketoday.http.client.ClientHttpResponse) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with ClientHttpResponse

use of cn.taketoday.http.client.ClientHttpResponse in project today-infrastructure by TAKETODAY.

the class AbstractServletWebServerFactoryTests method cookieSameSiteSuppliers.

@Test
void cookieSameSiteSuppliers() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    factory.addCookieSameSiteSuppliers(CookieSameSiteSupplier.ofLax().whenHasName("relaxed"));
    factory.addCookieSameSiteSuppliers(CookieSameSiteSupplier.ofNone().whenHasName("empty"));
    factory.addCookieSameSiteSuppliers(CookieSameSiteSupplier.ofStrict().whenHasName("controlled"));
    factory.addInitializers(new ServletRegistrationBean<>(new CookieServlet(true), "/"));
    this.webServer = factory.getWebServer();
    this.webServer.start();
    ClientHttpResponse clientResponse = getClientResponse(getLocalUrl("/"));
    List<String> setCookieHeaders = clientResponse.getHeaders().get("Set-Cookie");
    assertThat(setCookieHeaders).satisfiesExactlyInAnyOrder((header) -> assertThat(header).contains("JSESSIONID").doesNotContain("SameSite"), (header) -> assertThat(header).contains("test=test").doesNotContain("SameSite"), (header) -> assertThat(header).contains("relaxed=test").contains("SameSite=Lax"), (header) -> assertThat(header).contains("empty=test").contains("SameSite=None"), (header) -> assertThat(header).contains("controlled=test").contains("SameSite=Strict"));
}
Also used : ClientHttpResponse(cn.taketoday.http.client.ClientHttpResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 3 with ClientHttpResponse

use of cn.taketoday.http.client.ClientHttpResponse 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)

Example 4 with ClientHttpResponse

use of cn.taketoday.http.client.ClientHttpResponse in project today-framework 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 5 with ClientHttpResponse

use of cn.taketoday.http.client.ClientHttpResponse in project today-framework by TAKETODAY.

the class AbstractServletWebServerFactoryTests method sessionCookieSameSiteAttributeCanBeConfiguredAndOnlyAffectsSessionCookies.

@ParameterizedTest
@EnumSource(SameSite.class)
void sessionCookieSameSiteAttributeCanBeConfiguredAndOnlyAffectsSessionCookies(SameSite sameSite) throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    factory.getSession().getCookie().setSameSite(sameSite);
    factory.addInitializers(new ServletRegistrationBean<>(new CookieServlet(false), "/"));
    this.webServer = factory.getWebServer();
    this.webServer.start();
    ClientHttpResponse clientResponse = getClientResponse(getLocalUrl("/"));
    List<String> setCookieHeaders = clientResponse.getHeaders().get("Set-Cookie");
    assertThat(setCookieHeaders).satisfiesExactlyInAnyOrder((header) -> assertThat(header).contains("JSESSIONID").contains("SameSite=" + sameSite.attributeValue()), (header) -> assertThat(header).contains("test=test").doesNotContain("SameSite"));
}
Also used : ClientHttpResponse(cn.taketoday.http.client.ClientHttpResponse) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ClientHttpResponse (cn.taketoday.http.client.ClientHttpResponse)20 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)18 Test (org.junit.jupiter.api.Test)14 ServletRegistrationBean (cn.taketoday.framework.web.servlet.ServletRegistrationBean)4 HttpComponentsClientHttpRequestFactory (cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory)4 ExampleServlet (cn.taketoday.test.web.servlet.ExampleServlet)4 HttpClient (org.apache.http.client.HttpClient)4 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)4 TrustSelfSignedStrategy (org.apache.http.conn.ssl.TrustSelfSignedStrategy)4 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)4 EnumSource (org.junit.jupiter.params.provider.EnumSource)4 MimeMappings (cn.taketoday.framework.web.server.MimeMappings)2 ClientHttpRequest (cn.taketoday.http.client.ClientHttpRequest)2 SimpleClientHttpRequestFactory (cn.taketoday.http.client.SimpleClientHttpRequestFactory)2 File (java.io.File)2 FileWriter (java.io.FileWriter)2 URI (java.net.URI)2 DefaultHttpRequestRetryHandler (org.apache.http.impl.client.DefaultHttpRequestRetryHandler)2