Search in sources :

Example 1 with ClientHttpRequestInitializer

use of cn.taketoday.http.client.ClientHttpRequestInitializer 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 2 with ClientHttpRequestInitializer

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

the class HttpAccessor method createRequest.

/**
 * Create a new {@link ClientHttpRequest} via this template's {@link ClientHttpRequestFactory}.
 *
 * @param url the URL to connect to
 * @param method the HTTP method to execute (GET, POST, etc)
 * @return the created request
 * @throws IOException in case of I/O errors
 * @see #getRequestFactory()
 * @see ClientHttpRequestFactory#createRequest(URI, HttpMethod)
 */
protected ClientHttpRequest createRequest(URI url, HttpMethod method) throws IOException {
    ClientHttpRequest request = getRequestFactory().createRequest(url, method);
    List<ClientHttpRequestInitializer> requestInitializers = getHttpRequestInitializers();
    if (!requestInitializers.isEmpty()) {
        for (ClientHttpRequestInitializer initializer : requestInitializers) {
            initializer.initialize(request);
        }
    }
    if (isDebugEnabled) {
        logger.debug("HTTP {} {}", method.name(), url);
    }
    return request;
}
Also used : ClientHttpRequestInitializer(cn.taketoday.http.client.ClientHttpRequestInitializer) ClientHttpRequest(cn.taketoday.http.client.ClientHttpRequest)

Example 3 with ClientHttpRequestInitializer

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

the class RestTemplateTests method clientHttpRequestInitializerAndRequestInterceptorAreBothApplied.

@Test
void clientHttpRequestInitializerAndRequestInterceptorAreBothApplied() throws Exception {
    ClientHttpRequestInitializer initializer = request -> request.getHeaders().add("MyHeader", "MyInitializerValue");
    ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
        request.getHeaders().add("MyHeader", "MyInterceptorValue");
        return execution.execute(request, body);
    };
    template.setHttpRequestInitializers(Collections.singletonList(initializer));
    template.setInterceptors(interceptor);
    MediaType contentType = MediaType.TEXT_PLAIN;
    given(converter.canWrite(String.class, contentType)).willReturn(true);
    HttpHeaders requestHeaders = HttpHeaders.create();
    mockSentRequest(POST, "https://example.com", requestHeaders);
    mockResponseStatus(HttpStatus.OK);
    HttpHeaders entityHeaders = HttpHeaders.create();
    entityHeaders.setContentType(contentType);
    HttpEntity<String> entity = new HttpEntity<>("Hello World", entityHeaders);
    template.exchange("https://example.com", POST, entity, Void.class);
    assertThat(requestHeaders.get("MyHeader")).contains("MyInterceptorValue", "MyInitializerValue");
    verify(response).close();
}
Also used : ClientHttpRequestInitializer(cn.taketoday.http.client.ClientHttpRequestInitializer) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) MediaType.parseMediaType(cn.taketoday.http.MediaType.parseMediaType) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) PUT(cn.taketoday.http.HttpMethod.PUT) DELETE(cn.taketoday.http.HttpMethod.DELETE) HttpHeaders(cn.taketoday.http.HttpHeaders) GenericHttpMessageConverter(cn.taketoday.http.converter.GenericHttpMessageConverter) ByteArrayInputStream(java.io.ByteArrayInputStream) BDDMockito.given(org.mockito.BDDMockito.given) Map(java.util.Map) MockWebServer(okhttp3.mockwebserver.MockWebServer) DefaultUriBuilderFactory(cn.taketoday.web.util.DefaultUriBuilderFactory) ClientHttpRequestInitializer(cn.taketoday.http.client.ClientHttpRequestInitializer) URI(java.net.URI) PATCH(cn.taketoday.http.HttpMethod.PATCH) EnumSet(java.util.EnumSet) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) OPTIONS(cn.taketoday.http.HttpMethod.OPTIONS) Set(java.util.Set) ResponseEntity(cn.taketoday.http.ResponseEntity) ClientHttpResponse(cn.taketoday.http.client.ClientHttpResponse) StreamUtils(cn.taketoday.util.StreamUtils) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) POST(cn.taketoday.http.HttpMethod.POST) GET(cn.taketoday.http.HttpMethod.GET) Entry(java.util.Map.Entry) HttpStatus(cn.taketoday.http.HttpStatus) MockResponse(okhttp3.mockwebserver.MockResponse) ClientHttpRequestInterceptor(cn.taketoday.http.client.ClientHttpRequestInterceptor) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HashMap(java.util.HashMap) HttpMethod(cn.taketoday.http.HttpMethod) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) TypeReference(cn.taketoday.core.TypeReference) ClientHttpRequestFactory(cn.taketoday.http.client.ClientHttpRequestFactory) ClientHttpRequest(cn.taketoday.http.client.ClientHttpRequest) IOException(java.io.IOException) SimpleClientHttpRequestFactory(cn.taketoday.http.client.SimpleClientHttpRequestFactory) HttpInputMessage(cn.taketoday.http.HttpInputMessage) Mockito.verify(org.mockito.Mockito.verify) HEAD(cn.taketoday.http.HttpMethod.HEAD) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) HttpEntity(cn.taketoday.http.HttpEntity) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) HttpHeaders(cn.taketoday.http.HttpHeaders) HttpEntity(cn.taketoday.http.HttpEntity) MediaType.parseMediaType(cn.taketoday.http.MediaType.parseMediaType) MediaType(cn.taketoday.http.MediaType) ClientHttpRequestInterceptor(cn.taketoday.http.client.ClientHttpRequestInterceptor) Test(org.junit.jupiter.api.Test)

Example 4 with ClientHttpRequestInitializer

use of cn.taketoday.http.client.ClientHttpRequestInitializer in project today-framework 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 5 with ClientHttpRequestInitializer

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

the class HttpAccessor method createRequest.

/**
 * Create a new {@link ClientHttpRequest} via this template's {@link ClientHttpRequestFactory}.
 *
 * @param url the URL to connect to
 * @param method the HTTP method to execute (GET, POST, etc)
 * @return the created request
 * @throws IOException in case of I/O errors
 * @see #getRequestFactory()
 * @see ClientHttpRequestFactory#createRequest(URI, HttpMethod)
 */
protected ClientHttpRequest createRequest(URI url, HttpMethod method) throws IOException {
    ClientHttpRequest request = getRequestFactory().createRequest(url, method);
    List<ClientHttpRequestInitializer> requestInitializers = getHttpRequestInitializers();
    if (!requestInitializers.isEmpty()) {
        for (ClientHttpRequestInitializer initializer : requestInitializers) {
            initializer.initialize(request);
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("HTTP {} {}", method.name(), url);
    }
    return request;
}
Also used : ClientHttpRequestInitializer(cn.taketoday.http.client.ClientHttpRequestInitializer) ClientHttpRequest(cn.taketoday.http.client.ClientHttpRequest)

Aggregations

ClientHttpRequest (cn.taketoday.http.client.ClientHttpRequest)6 ClientHttpRequestInitializer (cn.taketoday.http.client.ClientHttpRequestInitializer)6 HttpHeaders (cn.taketoday.http.HttpHeaders)4 HttpMethod (cn.taketoday.http.HttpMethod)4 MediaType (cn.taketoday.http.MediaType)4 ClientHttpRequestFactory (cn.taketoday.http.client.ClientHttpRequestFactory)4 ClientHttpRequestInterceptor (cn.taketoday.http.client.ClientHttpRequestInterceptor)4 SimpleClientHttpRequestFactory (cn.taketoday.http.client.SimpleClientHttpRequestFactory)4 HttpMessageConverter (cn.taketoday.http.converter.HttpMessageConverter)4 URI (java.net.URI)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 Set (java.util.Set)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)4 Test (org.junit.jupiter.api.Test)4 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)4 Mockito.mock (org.mockito.Mockito.mock)4 Mockito.verify (org.mockito.Mockito.verify)4 TypeReference (cn.taketoday.core.TypeReference)2