Search in sources :

Example 1 with ClientHttpRequestFactory

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

the class RestTemplateBuilderTests method requestFactoryShouldApply.

@Test
void requestFactoryShouldApply() {
    ClientHttpRequestFactory requestFactory = mock(ClientHttpRequestFactory.class);
    RestTemplate template = this.builder.requestFactory(() -> requestFactory).build();
    assertThat(template.getRequestFactory()).isSameAs(requestFactory);
}
Also used : InterceptingClientHttpRequestFactory(cn.taketoday.http.client.InterceptingClientHttpRequestFactory) HttpComponentsClientHttpRequestFactory(cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory) OkHttp3ClientHttpRequestFactory(cn.taketoday.http.client.OkHttp3ClientHttpRequestFactory) ClientHttpRequestFactory(cn.taketoday.http.client.ClientHttpRequestFactory) SimpleClientHttpRequestFactory(cn.taketoday.http.client.SimpleClientHttpRequestFactory) BufferingClientHttpRequestFactory(cn.taketoday.http.client.BufferingClientHttpRequestFactory) RestTemplate(cn.taketoday.web.client.RestTemplate) Test(org.junit.jupiter.api.Test)

Example 2 with ClientHttpRequestFactory

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

the class RestTemplateBuilderTests method customizerShouldBeAppliedAtTheEnd.

@Test
void customizerShouldBeAppliedAtTheEnd() {
    ResponseErrorHandler errorHandler = mock(ResponseErrorHandler.class);
    ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    this.builder.interceptors(this.interceptor).messageConverters(this.messageConverter).rootUri("http://localhost:8080").errorHandler(errorHandler).basicAuthentication("spring", "boot").requestFactory(() -> requestFactory).customizers((restTemplate) -> {
        assertThat(restTemplate.getInterceptors()).hasSize(1);
        assertThat(restTemplate.getMessageConverters()).contains(this.messageConverter);
        assertThat(restTemplate.getUriTemplateHandler()).isInstanceOf(RootUriTemplateHandler.class);
        assertThat(restTemplate.getErrorHandler()).isEqualTo(errorHandler);
        ClientHttpRequestFactory actualRequestFactory = restTemplate.getRequestFactory();
        assertThat(actualRequestFactory).isInstanceOf(InterceptingClientHttpRequestFactory.class);
        ClientHttpRequestInitializer initializer = restTemplate.getHttpRequestInitializers().get(0);
        assertThat(initializer).isInstanceOf(RestTemplateBuilderClientHttpRequestInitializer.class);
    }).build();
}
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) ClientHttpRequestInitializer(cn.taketoday.http.client.ClientHttpRequestInitializer) InterceptingClientHttpRequestFactory(cn.taketoday.http.client.InterceptingClientHttpRequestFactory) HttpComponentsClientHttpRequestFactory(cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory) OkHttp3ClientHttpRequestFactory(cn.taketoday.http.client.OkHttp3ClientHttpRequestFactory) ClientHttpRequestFactory(cn.taketoday.http.client.ClientHttpRequestFactory) SimpleClientHttpRequestFactory(cn.taketoday.http.client.SimpleClientHttpRequestFactory) BufferingClientHttpRequestFactory(cn.taketoday.http.client.BufferingClientHttpRequestFactory) ResponseErrorHandler(cn.taketoday.web.client.ResponseErrorHandler) HttpComponentsClientHttpRequestFactory(cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory) InterceptingClientHttpRequestFactory(cn.taketoday.http.client.InterceptingClientHttpRequestFactory) Test(org.junit.jupiter.api.Test)

Example 3 with ClientHttpRequestFactory

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

the class RestTemplateBuilder method configure.

/**
 * Configure the provided {@link RestTemplate} instance using this builder.
 *
 * @param <T> the type of rest template
 * @param restTemplate the {@link RestTemplate} to configure
 * @return the rest template instance
 * @see RestTemplateBuilder#build()
 * @see RestTemplateBuilder#build(Class)
 */
public <T extends RestTemplate> T configure(T restTemplate) {
    ClientHttpRequestFactory requestFactory = buildRequestFactory();
    if (requestFactory != null) {
        restTemplate.setRequestFactory(requestFactory);
    }
    addClientHttpRequestInitializer(restTemplate);
    if (CollectionUtils.isNotEmpty(messageConverters)) {
        restTemplate.setMessageConverters(new ArrayList<>(messageConverters));
    }
    if (uriTemplateHandler != null) {
        restTemplate.setUriTemplateHandler(uriTemplateHandler);
    }
    if (errorHandler != null) {
        restTemplate.setErrorHandler(errorHandler);
    }
    if (rootUri != null) {
        RootUriTemplateHandler.addTo(restTemplate, rootUri);
    }
    restTemplate.getInterceptors().addAll(interceptors);
    if (CollectionUtils.isNotEmpty(customizers)) {
        for (RestTemplateCustomizer customizer : customizers) {
            customizer.customize(restTemplate);
        }
    }
    return restTemplate;
}
Also used : HttpComponentsClientHttpRequestFactory(cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory) ClientHttpRequestFactory(cn.taketoday.http.client.ClientHttpRequestFactory) SimpleClientHttpRequestFactory(cn.taketoday.http.client.SimpleClientHttpRequestFactory)

Example 4 with ClientHttpRequestFactory

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

the class RestTemplateBuilderTestsOkHttp3Tests method readTimeoutCanBeConfiguredOnOkHttpRequestFactory.

@Test
void readTimeoutCanBeConfiguredOnOkHttpRequestFactory() {
    ClientHttpRequestFactory requestFactory = this.builder.requestFactory(OkHttp3ClientHttpRequestFactory.class).setReadTimeout(Duration.ofMillis(1234)).build().getRequestFactory();
    assertThat(requestFactory).extracting("client", InstanceOfAssertFactories.type(OkHttpClient.class)).extracting(OkHttpClient::readTimeoutMillis).isEqualTo(1234);
}
Also used : OkHttp3ClientHttpRequestFactory(cn.taketoday.http.client.OkHttp3ClientHttpRequestFactory) ClientHttpRequestFactory(cn.taketoday.http.client.ClientHttpRequestFactory) OkHttp3ClientHttpRequestFactory(cn.taketoday.http.client.OkHttp3ClientHttpRequestFactory) OkHttpClient(okhttp3.OkHttpClient) Test(org.junit.jupiter.api.Test)

Example 5 with ClientHttpRequestFactory

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

the class RestTemplateBuilderTests method readTimeoutCanBeConfiguredOnOkHttp3RequestFactory.

@Test
void readTimeoutCanBeConfiguredOnOkHttp3RequestFactory() {
    ClientHttpRequestFactory requestFactory = this.builder.requestFactory(OkHttp3ClientHttpRequestFactory.class).setReadTimeout(Duration.ofMillis(1234)).build().getRequestFactory();
    assertThat(requestFactory).isInstanceOf(OkHttp3ClientHttpRequestFactory.class);
    assertThat(requestFactory).extracting("client").extracting("readTimeoutMillis").isEqualTo(1234);
}
Also used : OkHttp3ClientHttpRequestFactory(cn.taketoday.http.client.OkHttp3ClientHttpRequestFactory) InterceptingClientHttpRequestFactory(cn.taketoday.http.client.InterceptingClientHttpRequestFactory) HttpComponentsClientHttpRequestFactory(cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory) OkHttp3ClientHttpRequestFactory(cn.taketoday.http.client.OkHttp3ClientHttpRequestFactory) ClientHttpRequestFactory(cn.taketoday.http.client.ClientHttpRequestFactory) SimpleClientHttpRequestFactory(cn.taketoday.http.client.SimpleClientHttpRequestFactory) BufferingClientHttpRequestFactory(cn.taketoday.http.client.BufferingClientHttpRequestFactory) Test(org.junit.jupiter.api.Test)

Aggregations

ClientHttpRequestFactory (cn.taketoday.http.client.ClientHttpRequestFactory)32 OkHttp3ClientHttpRequestFactory (cn.taketoday.http.client.OkHttp3ClientHttpRequestFactory)28 Test (org.junit.jupiter.api.Test)28 HttpComponentsClientHttpRequestFactory (cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory)26 InterceptingClientHttpRequestFactory (cn.taketoday.http.client.InterceptingClientHttpRequestFactory)26 SimpleClientHttpRequestFactory (cn.taketoday.http.client.SimpleClientHttpRequestFactory)26 BufferingClientHttpRequestFactory (cn.taketoday.http.client.BufferingClientHttpRequestFactory)24 RequestConfig (org.apache.http.client.config.RequestConfig)6 RestTemplate (cn.taketoday.web.client.RestTemplate)4 OkHttpClient (okhttp3.OkHttpClient)4 HttpHeaders (cn.taketoday.http.HttpHeaders)2 HttpMethod (cn.taketoday.http.HttpMethod)2 MediaType (cn.taketoday.http.MediaType)2 ClientHttpRequest (cn.taketoday.http.client.ClientHttpRequest)2 ClientHttpRequestInitializer (cn.taketoday.http.client.ClientHttpRequestInitializer)2 ClientHttpRequestInterceptor (cn.taketoday.http.client.ClientHttpRequestInterceptor)2 HttpMessageConverter (cn.taketoday.http.converter.HttpMessageConverter)2 ResourceHttpMessageConverter (cn.taketoday.http.converter.ResourceHttpMessageConverter)2 StringHttpMessageConverter (cn.taketoday.http.converter.StringHttpMessageConverter)2 ReflectionTestUtils (cn.taketoday.util.ReflectionTestUtils)2