Search in sources :

Example 16 with RestTemplate

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

the class RestTemplateBuilderTests method interceptorsShouldReplaceExisting.

@Test
void interceptorsShouldReplaceExisting() {
    RestTemplate template = this.builder.interceptors(mock(ClientHttpRequestInterceptor.class)).interceptors(Collections.singleton(this.interceptor)).build();
    assertThat(template.getInterceptors()).containsOnly(this.interceptor);
}
Also used : RestTemplate(cn.taketoday.web.client.RestTemplate) ClientHttpRequestInterceptor(cn.taketoday.http.client.ClientHttpRequestInterceptor) Test(org.junit.jupiter.api.Test)

Example 17 with RestTemplate

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

the class RestTemplateBuilderTests method detectRequestFactoryWhenFalseShouldDisableDetection.

@Test
void detectRequestFactoryWhenFalseShouldDisableDetection() {
    RestTemplate restTemplate = this.builder.detectRequestFactory(false).build();
    assertThat(restTemplate.getRequestFactory()).isInstanceOf(SimpleClientHttpRequestFactory.class);
}
Also used : RestTemplate(cn.taketoday.web.client.RestTemplate) Test(org.junit.jupiter.api.Test)

Example 18 with RestTemplate

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

the class RestTemplateBuilderTests method additionalMessageConvertersShouldAddToExisting.

@Test
void additionalMessageConvertersShouldAddToExisting() {
    HttpMessageConverter<?> resourceConverter = new ResourceHttpMessageConverter();
    RestTemplate template = this.builder.messageConverters(resourceConverter).additionalMessageConverters(this.messageConverter).build();
    assertThat(template.getMessageConverters()).containsOnly(resourceConverter, this.messageConverter);
}
Also used : ResourceHttpMessageConverter(cn.taketoday.http.converter.ResourceHttpMessageConverter) RestTemplate(cn.taketoday.web.client.RestTemplate) Test(org.junit.jupiter.api.Test)

Example 19 with RestTemplate

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

the class RestTemplateBuilderTests method additionalRequestCustomizersAddsCustomizers.

@Test
void additionalRequestCustomizersAddsCustomizers() {
    RestTemplate template = this.builder.requestCustomizers((request) -> request.getHeaders().add("spring", "framework")).additionalRequestCustomizers((request) -> request.getHeaders().add("for", "java")).build();
    ClientHttpRequest request = createRequest(template);
    assertThat(request.getHeaders()).contains(entry("spring", Collections.singletonList("framework"))).contains(entry("for", Collections.singletonList("java")));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) UriTemplateHandler(cn.taketoday.web.util.UriTemplateHandler) Arrays(java.util.Arrays) Mock(org.mockito.Mock) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RequestConfig(org.apache.http.client.config.RequestConfig) Mockito.spy(org.mockito.Mockito.spy) Supplier(java.util.function.Supplier) InterceptingClientHttpRequestFactory(cn.taketoday.http.client.InterceptingClientHttpRequestFactory) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) HttpMethod(cn.taketoday.http.HttpMethod) HttpComponentsClientHttpRequestFactory(cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory) HttpHeaders(cn.taketoday.http.HttpHeaders) OkHttp3ClientHttpRequestFactory(cn.taketoday.http.client.OkHttp3ClientHttpRequestFactory) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Duration(java.time.Duration) ClientHttpRequestInitializer(cn.taketoday.http.client.ClientHttpRequestInitializer) ReflectionTestUtils(cn.taketoday.util.ReflectionTestUtils) URI(java.net.URI) ResponseErrorHandler(cn.taketoday.web.client.ResponseErrorHandler) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ClientHttpRequestFactory(cn.taketoday.http.client.ClientHttpRequestFactory) InOrder(org.mockito.InOrder) ClientHttpRequest(cn.taketoday.http.client.ClientHttpRequest) Set(java.util.Set) SimpleClientHttpRequestFactory(cn.taketoday.http.client.SimpleClientHttpRequestFactory) Assertions.entry(org.assertj.core.api.Assertions.entry) StandardCharsets(java.nio.charset.StandardCharsets) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) ResourceHttpMessageConverter(cn.taketoday.http.converter.ResourceHttpMessageConverter) Mockito.inOrder(org.mockito.Mockito.inOrder) RestTemplate(cn.taketoday.web.client.RestTemplate) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) BufferingClientHttpRequestFactory(cn.taketoday.http.client.BufferingClientHttpRequestFactory) ClientHttpRequestInterceptor(cn.taketoday.http.client.ClientHttpRequestInterceptor) Mockito.mock(org.mockito.Mockito.mock) RestTemplate(cn.taketoday.web.client.RestTemplate) ClientHttpRequest(cn.taketoday.http.client.ClientHttpRequest) Test(org.junit.jupiter.api.Test)

Example 20 with RestTemplate

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

the class ErrorHandlerIntegrationTests method handlingError.

@ParameterizedHttpServerTest
void handlingError(HttpServer httpServer) throws Exception {
    startServer(httpServer);
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setErrorHandler(NO_OP_ERROR_HANDLER);
    URI url = new URI("http://localhost:" + port + "/handling-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