Search in sources :

Example 26 with RestTemplate

use of cn.taketoday.web.client.RestTemplate in project today-framework by TAKETODAY.

the class AsyncIntegrationTests method basicTest.

@ParameterizedHttpServerTest
void basicTest(HttpServer httpServer) throws Exception {
    startServer(httpServer);
    URI url = new URI("http://localhost:" + port);
    ResponseEntity<String> response = new RestTemplate().exchange(RequestEntity.get(url).build(), String.class);
    assertThat(response.getBody()).isEqualTo("hello");
}
Also used : RestTemplate(cn.taketoday.web.client.RestTemplate) URI(java.net.URI)

Example 27 with RestTemplate

use of cn.taketoday.web.client.RestTemplate in project today-framework by TAKETODAY.

the class CookieIntegrationTests method basicTest.

@ParameterizedHttpServerTest
public void basicTest(HttpServer httpServer) throws Exception {
    startServer(httpServer);
    URI url = new URI("http://localhost:" + port);
    String header = "SID=31d4d96e407aad42; lang=en-US";
    ResponseEntity<Void> response = new RestTemplate().exchange(RequestEntity.get(url).header("Cookie", header).build(), Void.class);
    Map<String, List<HttpCookie>> requestCookies = this.cookieHandler.requestCookies;
    assertThat(requestCookies.size()).isEqualTo(2);
    List<HttpCookie> list = requestCookies.get("SID");
    assertThat(list.size()).isEqualTo(1);
    assertThat(list.iterator().next().getValue()).isEqualTo("31d4d96e407aad42");
    list = requestCookies.get("lang");
    assertThat(list.size()).isEqualTo(1);
    assertThat(list.iterator().next().getValue()).isEqualTo("en-US");
    List<String> headerValues = response.getHeaders().get("Set-Cookie");
    assertThat(headerValues.size()).isEqualTo(2);
    List<String> cookie0 = splitCookie(headerValues.get(0));
    assertThat(cookie0.remove("SID=31d4d96e407aad42")).as("SID").isTrue();
    assertThat(cookie0.stream().map(String::toLowerCase)).containsExactlyInAnyOrder("path=/", "secure", "httponly");
    List<String> cookie1 = splitCookie(headerValues.get(1));
    assertThat(cookie1.remove("lang=en-US")).as("lang").isTrue();
    assertThat(cookie1.stream().map(String::toLowerCase)).containsExactlyInAnyOrder("path=/", "domain=example.com");
}
Also used : RestTemplate(cn.taketoday.web.client.RestTemplate) ArrayList(java.util.ArrayList) List(java.util.List) URI(java.net.URI) HttpCookie(cn.taketoday.http.HttpCookie)

Example 28 with RestTemplate

use of cn.taketoday.web.client.RestTemplate in project today-framework by TAKETODAY.

the class ServerHttpsRequestIntegrationTests method startServer.

@BeforeEach
void startServer() throws Exception {
    this.server.setHandler(new CheckRequestHandler());
    this.server.afterPropertiesSet();
    this.server.start();
    // Set dynamically chosen port
    this.port = this.server.getPort();
    SSLContextBuilder builder = new SSLContextBuilder();
    builder.loadTrustMaterial(new TrustSelfSignedStrategy());
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(builder.build(), NoopHostnameVerifier.INSTANCE);
    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpclient);
    this.restTemplate = new RestTemplate(requestFactory);
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RestTemplate(cn.taketoday.web.client.RestTemplate) HttpComponentsClientHttpRequestFactory(cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 29 with RestTemplate

use of cn.taketoday.web.client.RestTemplate in project today-framework by TAKETODAY.

the class ZeroCopyIntegrationTests method zeroCopy.

@ParameterizedHttpServerTest
void zeroCopy(HttpServer httpServer) throws Exception {
    assumeTrue(httpServer instanceof ReactorHttpServer || httpServer instanceof UndertowHttpServer, "Zero-copy does not support Servlet");
    startServer(httpServer);
    URI url = new URI("http://localhost:" + port);
    RequestEntity<?> request = RequestEntity.get(url).build();
    ResponseEntity<byte[]> response = new RestTemplate().exchange(request, byte[].class);
    assertThat(response.hasBody()).isTrue();
    assertThat(response.getHeaders().getContentLength()).isEqualTo(springLogoResource.contentLength());
    assertThat(response.getBody().length).isEqualTo(springLogoResource.contentLength());
    assertThat(response.getHeaders().getContentType()).isEqualTo(MediaType.IMAGE_PNG);
}
Also used : ReactorHttpServer(cn.taketoday.web.testfixture.http.server.reactive.bootstrap.ReactorHttpServer) RestTemplate(cn.taketoday.web.client.RestTemplate) UndertowHttpServer(cn.taketoday.web.testfixture.http.server.reactive.bootstrap.UndertowHttpServer) URI(java.net.URI)

Example 30 with RestTemplate

use of cn.taketoday.web.client.RestTemplate in project today-framework by TAKETODAY.

the class ErrorHandlerIntegrationTests method responseBodyError.

@ParameterizedHttpServerTest
void responseBodyError(HttpServer httpServer) throws Exception {
    startServer(httpServer);
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setErrorHandler(NO_OP_ERROR_HANDLER);
    URI url = new URI("http://localhost:" + port + "/response-body-error");
    ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
}
Also used : RestTemplate(cn.taketoday.web.client.RestTemplate) URI(java.net.URI)

Aggregations

RestTemplate (cn.taketoday.web.client.RestTemplate)100 Test (org.junit.jupiter.api.Test)66 URI (java.net.URI)30 StringHttpMessageConverter (cn.taketoday.http.converter.StringHttpMessageConverter)14 ClientHttpRequest (cn.taketoday.http.client.ClientHttpRequest)12 SimpleClientHttpRequestFactory (cn.taketoday.http.client.SimpleClientHttpRequestFactory)12 BufferingClientHttpRequestFactory (cn.taketoday.http.client.BufferingClientHttpRequestFactory)10 ClientHttpRequestInterceptor (cn.taketoday.http.client.ClientHttpRequestInterceptor)10 HttpComponentsClientHttpRequestFactory (cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory)10 HttpMessageConverter (cn.taketoday.http.converter.HttpMessageConverter)10 ResourceHttpMessageConverter (cn.taketoday.http.converter.ResourceHttpMessageConverter)10 UriTemplateHandler (cn.taketoday.web.util.UriTemplateHandler)10 HttpHeaders (cn.taketoday.http.HttpHeaders)8 ClientHttpRequestFactory (cn.taketoday.http.client.ClientHttpRequestFactory)8 InterceptingClientHttpRequestFactory (cn.taketoday.http.client.InterceptingClientHttpRequestFactory)8 OkHttp3ClientHttpRequestFactory (cn.taketoday.http.client.OkHttp3ClientHttpRequestFactory)8 ResponseErrorHandler (cn.taketoday.web.client.ResponseErrorHandler)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 InOrder (org.mockito.InOrder)8 HttpMethod (cn.taketoday.http.HttpMethod)6