Search in sources :

Example 16 with ClientHttpResponse

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

Example 17 with ClientHttpResponse

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

the class AbstractServletWebServerFactoryTests method customServerHeader.

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

Example 18 with ClientHttpResponse

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

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

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

the class ServletWebServerMvcIntegrationTests method doTest.

private void doTest(AnnotationConfigServletWebServerApplicationContext context, String resourcePath) throws Exception {
    SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
    ClientHttpRequest request = clientHttpRequestFactory.createRequest(new URI("http://localhost:" + context.getWebServer().getPort() + resourcePath), HttpMethod.GET);
    try (ClientHttpResponse response = request.execute()) {
        assertThat(response.getBody()).hasContent("Hello World");
    }
}
Also used : SimpleClientHttpRequestFactory(cn.taketoday.http.client.SimpleClientHttpRequestFactory) ClientHttpRequest(cn.taketoday.http.client.ClientHttpRequest) URI(java.net.URI) ClientHttpResponse(cn.taketoday.http.client.ClientHttpResponse)

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