Search in sources :

Example 81 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse in project spring-boot by spring-projects.

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(org.springframework.http.client.ClientHttpResponse) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 82 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse in project spring-boot by spring-projects.

the class AbstractServletWebServerFactoryTests method cannotReadClassPathFiles.

@Test
void cannotReadClassPathFiles() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    this.webServer = factory.getWebServer(exampleServletRegistration());
    this.webServer.start();
    ClientHttpResponse response = getClientResponse(getLocalUrl("/org/springframework/boot/SpringApplication.class"));
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}
Also used : ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 83 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse in project spring-boot by spring-projects.

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(org.springframework.boot.web.servlet.ServletRegistrationBean) HttpClient(org.apache.http.client.HttpClient) 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 84 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse in project spring-boot by spring-projects.

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(org.springframework.http.client.ClientHttpResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 85 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse in project spring-boot by spring-projects.

the class RestTemplateExchangeTagsTests method outcomeTagIsInformationalWhenResponseIs1xx.

@Test
void outcomeTagIsInformationalWhenResponseIs1xx() {
    ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(), HttpStatus.CONTINUE);
    Tag tag = RestTemplateExchangeTags.outcome(response);
    assertThat(tag.getValue()).isEqualTo("INFORMATIONAL");
}
Also used : Tag(io.micrometer.core.instrument.Tag) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)142 IOException (java.io.IOException)38 Test (org.junit.Test)38 HttpHeaders (org.springframework.http.HttpHeaders)35 ByteArrayInputStream (java.io.ByteArrayInputStream)23 URI (java.net.URI)23 ResponseErrorHandler (org.springframework.web.client.ResponseErrorHandler)23 Test (org.junit.jupiter.api.Test)21 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)20 RestTemplate (org.springframework.web.client.RestTemplate)20 DefaultResponseErrorHandler (org.springframework.web.client.DefaultResponseErrorHandler)18 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 MockClientHttpResponse (org.springframework.mock.http.client.MockClientHttpResponse)9 ResponseExtractor (org.springframework.web.client.ResponseExtractor)9 Tag (io.micrometer.core.instrument.Tag)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 Invocation (org.apache.servicecomb.core.Invocation)6