Search in sources :

Example 6 with ClientHttpResponse

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

the class AbstractServletWebServerFactoryTests method mimeType.

@Test
void mimeType() throws Exception {
    FileCopyUtils.copy("test", new FileWriter(new File(this.tempDir, "test.xxcss")));
    AbstractServletWebServerFactory factory = getFactory();
    factory.setRegisterDefaultServlet(true);
    factory.setDocumentRoot(this.tempDir);
    MimeMappings mimeMappings = new MimeMappings();
    mimeMappings.add("xxcss", "text/css");
    factory.setMimeMappings(mimeMappings);
    this.webServer = factory.getWebServer();
    this.webServer.start();
    ClientHttpResponse response = getClientResponse(getLocalUrl("/test.xxcss"));
    assertThat(response.getHeaders().getContentType().toString()).isEqualTo("text/css");
    response.close();
}
Also used : FileWriter(java.io.FileWriter) MimeMappings(cn.taketoday.framework.web.server.MimeMappings) File(java.io.File) ClientHttpResponse(cn.taketoday.http.client.ClientHttpResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 7 with ClientHttpResponse

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

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

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

the class AbstractServletWebServerFactoryTests method serverHeaderIsDisabledByDefault.

@Test
void serverHeaderIsDisabledByDefault() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    this.webServer = factory.getWebServer(exampleServletRegistration());
    this.webServer.start();
    ClientHttpResponse response = getClientResponse(getLocalUrl("/hello"));
    assertThat(response.getHeaders().getFirst("server")).isNull();
}
Also used : ClientHttpResponse(cn.taketoday.http.client.ClientHttpResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 10 with ClientHttpResponse

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

the class AbstractServletWebServerFactoryTests method sessionCookieSameSiteAttributeCanBeConfiguredAndOnlyAffectsSessionCookiesWhenUsingCustomName.

@ParameterizedTest
@EnumSource(SameSite.class)
void sessionCookieSameSiteAttributeCanBeConfiguredAndOnlyAffectsSessionCookiesWhenUsingCustomName(SameSite sameSite) throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    factory.getSession().getCookie().setName("THESESSION");
    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("THESESSION").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